Add endpoints for getting webpush public key and subscribing

This commit is contained in:
Karina Kwiatek 2022-09-11 20:12:42 +02:00
parent 32ab9267ec
commit bae227be76
2 changed files with 21 additions and 0 deletions

View File

@ -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

View File

@ -132,6 +132,8 @@ Rails.application.routes.draw do
post "/list_membership", to: "list#membership", as: :list_membership post "/list_membership", to: "list#membership", as: :list_membership
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
post "/webpush", to: "web_push#subscribe", as: :webpush_subscribe
end end
resource :anonymous_block, controller: :anonymous_block, only: %i[create destroy] resource :anonymous_block, controller: :anonymous_block, only: %i[create destroy]