From efad76855e0940b7cf247cad8ca471e920d37c09 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 1 Jan 2023 21:34:49 +0100 Subject: [PATCH] Add endpoint for checking subscription status --- app/controllers/ajax/web_push_controller.rb | 17 +++++++++++++++++ config/routes.rb | 1 + 2 files changed, 18 insertions(+) diff --git a/app/controllers/ajax/web_push_controller.rb b/app/controllers/ajax/web_push_controller.rb index f48d2174..d6ef3032 100644 --- a/app/controllers/ajax/web_push_controller.rb +++ b/app/controllers/ajax/web_push_controller.rb @@ -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, diff --git a/config/routes.rb b/config/routes.rb index 55300166..0929f60c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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