2017-07-13 23:15:32 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-17 16:23:46 +03:00
|
|
|
class Api::Web::PushSubscriptionsController < Api::Web::BaseController
|
2017-07-13 23:15:32 +03:00
|
|
|
before_action :require_user!
|
2021-04-15 06:00:25 +03:00
|
|
|
before_action :set_push_subscription, only: :update
|
2023-11-15 15:14:51 +02:00
|
|
|
before_action :destroy_previous_subscriptions, only: :create, if: :prior_subscriptions?
|
|
|
|
after_action :update_session_with_subscription, only: :create
|
2017-07-13 23:15:32 +03:00
|
|
|
|
|
|
|
def create
|
2023-11-15 15:14:51 +02:00
|
|
|
@push_subscription = ::Web::PushSubscription.create!(web_push_subscription_params)
|
2017-07-13 23:15:32 +03:00
|
|
|
|
2023-11-15 15:14:51 +02:00
|
|
|
render json: @push_subscription, serializer: REST::WebPushSubscriptionSerializer
|
|
|
|
end
|
2017-07-13 23:15:32 +03:00
|
|
|
|
2023-11-15 15:14:51 +02:00
|
|
|
def update
|
|
|
|
@push_subscription.update!(data: data_params)
|
|
|
|
render json: @push_subscription, serializer: REST::WebPushSubscriptionSerializer
|
|
|
|
end
|
2017-07-24 00:27:23 +03:00
|
|
|
|
2023-11-15 15:14:51 +02:00
|
|
|
private
|
2017-07-24 00:27:23 +03:00
|
|
|
|
2023-11-15 15:14:51 +02:00
|
|
|
def active_session
|
|
|
|
@active_session ||= current_session
|
|
|
|
end
|
2017-12-09 03:31:37 +02:00
|
|
|
|
2023-11-15 15:14:51 +02:00
|
|
|
def destroy_previous_subscriptions
|
|
|
|
active_session.web_push_subscription.destroy!
|
|
|
|
active_session.update!(web_push_subscription: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prior_subscriptions?
|
|
|
|
active_session.web_push_subscription.present?
|
|
|
|
end
|
2017-07-13 23:15:32 +03:00
|
|
|
|
2023-11-15 15:14:51 +02:00
|
|
|
def subscription_data
|
|
|
|
default_subscription_data.tap do |data|
|
|
|
|
data.deep_merge!(data_params) if params[:data]
|
|
|
|
end
|
|
|
|
end
|
2017-07-13 23:15:32 +03:00
|
|
|
|
2023-11-15 15:14:51 +02:00
|
|
|
def default_subscription_data
|
|
|
|
{
|
|
|
|
policy: 'all',
|
|
|
|
alerts: Notification::TYPES.index_with { alerts_enabled },
|
|
|
|
}
|
2017-07-13 23:15:32 +03:00
|
|
|
end
|
|
|
|
|
2023-11-15 15:14:51 +02:00
|
|
|
def alerts_enabled
|
|
|
|
# Mobile devices do not support regular notifications, so we enable push notifications by default
|
|
|
|
active_session.detection.device.mobile? || active_session.detection.device.tablet?
|
2017-07-13 23:15:32 +03:00
|
|
|
end
|
2018-04-12 15:45:17 +03:00
|
|
|
|
2023-11-15 15:14:51 +02:00
|
|
|
def update_session_with_subscription
|
|
|
|
active_session.update!(web_push_subscription: @push_subscription)
|
|
|
|
end
|
2018-04-12 15:45:17 +03:00
|
|
|
|
2021-04-15 06:00:25 +03:00
|
|
|
def set_push_subscription
|
|
|
|
@push_subscription = ::Web::PushSubscription.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2018-04-12 15:45:17 +03:00
|
|
|
def subscription_params
|
|
|
|
@subscription_params ||= params.require(:subscription).permit(:endpoint, keys: [:auth, :p256dh])
|
|
|
|
end
|
|
|
|
|
2023-11-15 15:14:51 +02:00
|
|
|
def web_push_subscription_params
|
|
|
|
{
|
|
|
|
access_token_id: active_session.access_token_id,
|
|
|
|
data: subscription_data,
|
|
|
|
endpoint: subscription_params[:endpoint],
|
|
|
|
key_auth: subscription_params[:keys][:auth],
|
|
|
|
key_p256dh: subscription_params[:keys][:p256dh],
|
|
|
|
user_id: active_session.user_id,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-04-12 15:45:17 +03:00
|
|
|
def data_params
|
2022-02-23 17:45:22 +02:00
|
|
|
@data_params ||= params.require(:data).permit(:policy, alerts: Notification::TYPES)
|
2018-04-12 15:45:17 +03:00
|
|
|
end
|
2017-07-13 23:15:32 +03:00
|
|
|
end
|