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]