2017-07-13 22:15:32 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-17 15:23:46 +02:00
|
|
|
class Api::Web::PushSubscriptionsController < Api::Web::BaseController
|
2017-07-13 22:15:32 +02:00
|
|
|
before_action :require_user!
|
2021-04-15 05:00:25 +02:00
|
|
|
before_action :set_push_subscription, only: :update
|
2017-07-13 22:15:32 +02:00
|
|
|
|
|
|
|
def create
|
|
|
|
active_session = current_session
|
|
|
|
|
|
|
|
unless active_session.web_push_subscription.nil?
|
|
|
|
active_session.web_push_subscription.destroy!
|
|
|
|
active_session.update!(web_push_subscription: nil)
|
|
|
|
end
|
|
|
|
|
2017-07-23 23:27:23 +02:00
|
|
|
# Mobile devices do not support regular notifications, so we enable push notifications by default
|
|
|
|
alerts_enabled = active_session.detection.device.mobile? || active_session.detection.device.tablet?
|
|
|
|
|
|
|
|
data = {
|
2021-04-15 05:00:25 +02:00
|
|
|
policy: 'all',
|
|
|
|
|
2017-07-23 23:27:23 +02:00
|
|
|
alerts: {
|
|
|
|
follow: alerts_enabled,
|
2021-04-15 05:00:25 +02:00
|
|
|
follow_request: alerts_enabled,
|
2017-07-23 23:27:23 +02:00
|
|
|
favourite: alerts_enabled,
|
|
|
|
reblog: alerts_enabled,
|
|
|
|
mention: alerts_enabled,
|
2019-05-28 00:26:08 +02:00
|
|
|
poll: alerts_enabled,
|
2020-09-21 17:48:55 +02:00
|
|
|
status: alerts_enabled,
|
2022-02-11 22:20:19 +01:00
|
|
|
update: alerts_enabled,
|
2017-07-23 23:27:23 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:45:17 +02:00
|
|
|
data.deep_merge!(data_params) if params[:data]
|
2017-12-09 02:31:37 +01:00
|
|
|
|
2021-04-15 05:00:25 +02:00
|
|
|
push_subscription = ::Web::PushSubscription.create!(
|
2018-04-12 14:45:17 +02:00
|
|
|
endpoint: subscription_params[:endpoint],
|
|
|
|
key_p256dh: subscription_params[:keys][:p256dh],
|
|
|
|
key_auth: subscription_params[:keys][:auth],
|
2018-05-11 11:49:12 +02:00
|
|
|
data: data,
|
|
|
|
user_id: active_session.user_id,
|
|
|
|
access_token_id: active_session.access_token_id
|
2017-07-13 22:15:32 +02:00
|
|
|
)
|
|
|
|
|
2021-04-15 05:00:25 +02:00
|
|
|
active_session.update!(web_push_subscription: push_subscription)
|
2017-07-13 22:15:32 +02:00
|
|
|
|
2021-04-15 05:00:25 +02:00
|
|
|
render json: push_subscription, serializer: REST::WebPushSubscriptionSerializer
|
2017-07-13 22:15:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2021-04-15 05:00:25 +02:00
|
|
|
@push_subscription.update!(data: data_params)
|
|
|
|
render json: @push_subscription, serializer: REST::WebPushSubscriptionSerializer
|
2017-07-13 22:15:32 +02:00
|
|
|
end
|
2018-04-12 14:45:17 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-04-15 05:00:25 +02:00
|
|
|
def set_push_subscription
|
|
|
|
@push_subscription = ::Web::PushSubscription.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2018-04-12 14:45:17 +02:00
|
|
|
def subscription_params
|
|
|
|
@subscription_params ||= params.require(:subscription).permit(:endpoint, keys: [:auth, :p256dh])
|
|
|
|
end
|
|
|
|
|
|
|
|
def data_params
|
2022-02-11 22:20:19 +01:00
|
|
|
@data_params ||= params.require(:data).permit(:policy, alerts: [
|
|
|
|
:follow,
|
|
|
|
:follow_request,
|
|
|
|
:favourite,
|
|
|
|
:reblog,
|
|
|
|
:mention,
|
|
|
|
:poll,
|
|
|
|
:status,
|
|
|
|
:update,
|
|
|
|
])
|
2018-04-12 14:45:17 +02:00
|
|
|
end
|
2017-07-13 22:15:32 +02:00
|
|
|
end
|