2018-05-11 02:49:12 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Api::V1::Push::SubscriptionsController < Api::BaseController
|
|
|
|
before_action -> { doorkeeper_authorize! :push }
|
|
|
|
before_action :require_user!
|
2021-04-14 20:00:25 -07:00
|
|
|
before_action :set_push_subscription
|
|
|
|
before_action :check_push_subscription, only: [:show, :update]
|
2018-05-11 02:49:12 -07:00
|
|
|
|
2023-04-29 21:46:39 -07:00
|
|
|
def show
|
|
|
|
render json: @push_subscription, serializer: REST::WebPushSubscriptionSerializer
|
|
|
|
end
|
|
|
|
|
2018-05-11 02:49:12 -07:00
|
|
|
def create
|
2021-04-14 20:00:25 -07:00
|
|
|
@push_subscription&.destroy!
|
2018-05-11 02:49:12 -07:00
|
|
|
|
2021-04-14 20:00:25 -07:00
|
|
|
@push_subscription = Web::PushSubscription.create!(
|
2018-05-11 02:49:12 -07:00
|
|
|
endpoint: subscription_params[:endpoint],
|
|
|
|
key_p256dh: subscription_params[:keys][:p256dh],
|
|
|
|
key_auth: subscription_params[:keys][:auth],
|
|
|
|
data: data_params,
|
|
|
|
user_id: current_user.id,
|
|
|
|
access_token_id: doorkeeper_token.id
|
|
|
|
)
|
|
|
|
|
2021-04-14 20:00:25 -07:00
|
|
|
render json: @push_subscription, serializer: REST::WebPushSubscriptionSerializer
|
2018-05-11 02:49:12 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2021-04-14 20:00:25 -07:00
|
|
|
@push_subscription.update!(data: data_params)
|
|
|
|
render json: @push_subscription, serializer: REST::WebPushSubscriptionSerializer
|
2018-05-11 02:49:12 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2021-04-14 20:00:25 -07:00
|
|
|
@push_subscription&.destroy!
|
2018-05-11 02:49:12 -07:00
|
|
|
render_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-04-14 20:00:25 -07:00
|
|
|
def set_push_subscription
|
|
|
|
@push_subscription = Web::PushSubscription.find_by(access_token_id: doorkeeper_token.id)
|
2018-05-11 02:49:12 -07:00
|
|
|
end
|
|
|
|
|
2021-04-14 20:00:25 -07:00
|
|
|
def check_push_subscription
|
|
|
|
not_found if @push_subscription.nil?
|
2020-05-03 07:30:36 -07:00
|
|
|
end
|
|
|
|
|
2018-05-11 02:49:12 -07:00
|
|
|
def subscription_params
|
|
|
|
params.require(:subscription).permit(:endpoint, keys: [:auth, :p256dh])
|
|
|
|
end
|
|
|
|
|
|
|
|
def data_params
|
|
|
|
return {} if params[:data].blank?
|
2020-05-03 07:30:36 -07:00
|
|
|
|
2022-06-22 16:44:27 -07:00
|
|
|
params.require(:data).permit(:policy, alerts: Notification::TYPES)
|
2018-05-11 02:49:12 -07:00
|
|
|
end
|
|
|
|
end
|