2022-09-11 20:12:42 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Ajax::WebPushController < AjaxController
|
|
|
|
def key
|
|
|
|
certificate = Rpush::Webpush::App.find_by(name: "webpush").certificate
|
|
|
|
|
2022-10-20 19:02:12 +02:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:success] = true
|
2022-09-11 20:12:42 +02:00
|
|
|
@response[:key] = JSON.parse(certificate)["public_key"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def subscribe
|
|
|
|
WebPushSubscription.create!(
|
|
|
|
user: current_user,
|
|
|
|
subscription: params[:subscription]
|
|
|
|
)
|
|
|
|
|
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:success] = true
|
|
|
|
end
|
2022-10-21 22:37:52 +02:00
|
|
|
|
|
|
|
def unsubscribe
|
|
|
|
params.permit(:endpoint)
|
|
|
|
|
|
|
|
if params.key?(:endpoint)
|
|
|
|
current_user.web_push_subscriptions.where("subscription ->> 'endpoint' = ?", params[:endpoint]).destroy
|
|
|
|
else
|
|
|
|
current_user.web_push_subscriptions.destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:success] = true
|
|
|
|
end
|
2022-09-11 20:12:42 +02:00
|
|
|
end
|