Add endpoint for checking subscription status
This commit is contained in:
parent
e0195654b5
commit
efad76855e
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue