Add endpoint for checking subscription status

This commit is contained in:
Karina Kwiatek 2023-01-01 21:34:49 +01:00
parent e0195654b5
commit efad76855e
2 changed files with 18 additions and 0 deletions

View File

@ -11,6 +11,23 @@ class Ajax::WebPushController < AjaxController
@response[:key] = JSON.parse(certificate)["public_key"]
end
def check
params.permit(:endpoint)
found = WebPushSubscription.where("subscription ->> 'endpoint' = ?", params[:endpoint]).first
@response[:status] = if found
if found.failures >= 3
:failed
else
:subscribed
end
else
:unsubscribed
end
@response[:success] = true
end
def subscribe
WebPushSubscription.create!(
user: current_user,

View File

@ -135,6 +135,7 @@ Rails.application.routes.draw do
post "/subscribe", to: "subscription#subscribe", as: :subscribe_answer
post "/unsubscribe", to: "subscription#unsubscribe", as: :unsubscribe_answer
get "/webpush/key", to: "web_push#key", as: :webpush_key
post "/webpush/check", to: "web_push#check", as: :webpush_check
post "/webpush", to: "web_push#subscribe", as: :webpush_subscribe
delete "/webpush", to: "web_push#unsubscribe", as: :webpush_unsubscribe
end