logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

push_subscriptions_controller.rb (1641B)


  1. # frozen_string_literal: true
  2. class Api::Web::PushSubscriptionsController < Api::BaseController
  3. respond_to :json
  4. before_action :require_user!
  5. protect_from_forgery with: :exception
  6. def create
  7. active_session = current_session
  8. unless active_session.web_push_subscription.nil?
  9. active_session.web_push_subscription.destroy!
  10. active_session.update!(web_push_subscription: nil)
  11. end
  12. # Mobile devices do not support regular notifications, so we enable push notifications by default
  13. alerts_enabled = active_session.detection.device.mobile? || active_session.detection.device.tablet?
  14. data = {
  15. alerts: {
  16. follow: alerts_enabled,
  17. favourite: alerts_enabled,
  18. reblog: alerts_enabled,
  19. mention: alerts_enabled,
  20. },
  21. }
  22. data.deep_merge!(data_params) if params[:data]
  23. web_subscription = ::Web::PushSubscription.create!(
  24. endpoint: subscription_params[:endpoint],
  25. key_p256dh: subscription_params[:keys][:p256dh],
  26. key_auth: subscription_params[:keys][:auth],
  27. data: data
  28. )
  29. active_session.update!(web_push_subscription: web_subscription)
  30. render json: web_subscription.as_payload
  31. end
  32. def update
  33. params.require([:id])
  34. web_subscription = ::Web::PushSubscription.find(params[:id])
  35. web_subscription.update!(data: data_params)
  36. render json: web_subscription.as_payload
  37. end
  38. private
  39. def subscription_params
  40. @subscription_params ||= params.require(:subscription).permit(:endpoint, keys: [:auth, :p256dh])
  41. end
  42. def data_params
  43. @data_params ||= params.require(:data).permit(:alerts)
  44. end
  45. end