You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Push::SubscriptionsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :push }
  4. before_action :require_user!
  5. before_action :set_web_push_subscription
  6. before_action :check_web_push_subscription, only: [:show, :update]
  7. def create
  8. @web_subscription&.destroy!
  9. @web_subscription = ::Web::PushSubscription.create!(
  10. endpoint: subscription_params[:endpoint],
  11. key_p256dh: subscription_params[:keys][:p256dh],
  12. key_auth: subscription_params[:keys][:auth],
  13. data: data_params,
  14. user_id: current_user.id,
  15. access_token_id: doorkeeper_token.id
  16. )
  17. render json: @web_subscription, serializer: REST::WebPushSubscriptionSerializer
  18. end
  19. def show
  20. render json: @web_subscription, serializer: REST::WebPushSubscriptionSerializer
  21. end
  22. def update
  23. @web_subscription.update!(data: data_params)
  24. render json: @web_subscription, serializer: REST::WebPushSubscriptionSerializer
  25. end
  26. def destroy
  27. @web_subscription&.destroy!
  28. render_empty
  29. end
  30. private
  31. def set_web_push_subscription
  32. @web_subscription = ::Web::PushSubscription.find_by(access_token_id: doorkeeper_token.id)
  33. end
  34. def check_web_push_subscription
  35. not_found if @web_subscription.nil?
  36. end
  37. def subscription_params
  38. params.require(:subscription).permit(:endpoint, keys: [:auth, :p256dh])
  39. end
  40. def data_params
  41. return {} if params[:data].blank?
  42. params.require(:data).permit(alerts: [:follow, :follow_request, :favourite, :reblog, :mention, :poll, :status])
  43. end
  44. end