From bae227be768ef5791577aa2eb5910d380076e521 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 11 Sep 2022 20:12:42 +0200 Subject: [PATCH] Add endpoints for getting webpush public key and subscribing --- app/controllers/ajax/web_push_controller.rb | 19 +++++++++++++++++++ config/routes.rb | 2 ++ 2 files changed, 21 insertions(+) create mode 100644 app/controllers/ajax/web_push_controller.rb diff --git a/app/controllers/ajax/web_push_controller.rb b/app/controllers/ajax/web_push_controller.rb new file mode 100644 index 00000000..af4e715f --- /dev/null +++ b/app/controllers/ajax/web_push_controller.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class Ajax::WebPushController < AjaxController + def key + certificate = Rpush::Webpush::App.find_by(name: "webpush").certificate + + @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 +end diff --git a/config/routes.rb b/config/routes.rb index 8a753e5b..fe0ef852 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -132,6 +132,8 @@ Rails.application.routes.draw do post "/list_membership", to: "list#membership", as: :list_membership 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", to: "web_push#subscribe", as: :webpush_subscribe end resource :anonymous_block, controller: :anonymous_block, only: %i[create destroy]