mirror of
https://github.com/Retrospring/retrospring.git
synced 2025-01-19 04:36:03 +01:00
Add endpoint for checking subscription status
This commit is contained in:
parent
e0195654b5
commit
efad76855e
2 changed files with 18 additions and 0 deletions
|
@ -11,6 +11,23 @@ class Ajax::WebPushController < AjaxController
|
||||||
@response[:key] = JSON.parse(certificate)["public_key"]
|
@response[:key] = JSON.parse(certificate)["public_key"]
|
||||||
end
|
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
|
def subscribe
|
||||||
WebPushSubscription.create!(
|
WebPushSubscription.create!(
|
||||||
user: current_user,
|
user: current_user,
|
||||||
|
|
|
@ -135,6 +135,7 @@ Rails.application.routes.draw do
|
||||||
post "/subscribe", to: "subscription#subscribe", as: :subscribe_answer
|
post "/subscribe", to: "subscription#subscribe", as: :subscribe_answer
|
||||||
post "/unsubscribe", to: "subscription#unsubscribe", as: :unsubscribe_answer
|
post "/unsubscribe", to: "subscription#unsubscribe", as: :unsubscribe_answer
|
||||||
get "/webpush/key", to: "web_push#key", as: :webpush_key
|
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
|
post "/webpush", to: "web_push#subscribe", as: :webpush_subscribe
|
||||||
delete "/webpush", to: "web_push#unsubscribe", as: :webpush_unsubscribe
|
delete "/webpush", to: "web_push#unsubscribe", as: :webpush_unsubscribe
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue