diff --git a/app/controllers/ajax/web_push_controller.rb b/app/controllers/ajax/web_push_controller.rb index 8f0408ad..f48d2174 100644 --- a/app/controllers/ajax/web_push_controller.rb +++ b/app/controllers/ajax/web_push_controller.rb @@ -22,14 +22,14 @@ class Ajax::WebPushController < AjaxController @response[:message] = t(".subscription_count", count: current_user.web_push_subscriptions.count) end - def unsubscribe + def unsubscribe # rubocop:disable Metrics/AbcSize params.permit(:endpoint) removed = if params.key?(:endpoint) - current_user.web_push_subscriptions.where("subscription ->> 'endpoint' = ?", params[:endpoint]).destroy_all - else - current_user.web_push_subscriptions.destroy_all - end + current_user.web_push_subscriptions.where("subscription ->> 'endpoint' = ?", params[:endpoint]).destroy_all + else + current_user.web_push_subscriptions.destroy_all + end count = current_user.web_push_subscriptions.count diff --git a/app/workers/question_worker.rb b/app/workers/question_worker.rb index eea5ea27..3936f77f 100644 --- a/app/workers/question_worker.rb +++ b/app/workers/question_worker.rb @@ -19,7 +19,7 @@ class QuestionWorker next if user.muting?(question.user) inbox = Inbox.create(user_id: f.id, question_id:, new: true) - f.push_notification(webpush_app, inbox) + f.push_notification(webpush_app, inbox) if webpush_app end rescue StandardError => e logger.info "failed to ask question: #{e.message}" diff --git a/config/justask.yml.example b/config/justask.yml.example index 023f6b85..1d627249 100644 --- a/config/justask.yml.example +++ b/config/justask.yml.example @@ -8,6 +8,8 @@ hostname: "justask.rrerr.net" https: true email_from: "noreply@justask.rrerr.net" +# Required by WebPush spec in case of problems with notifications +contact_email: "contact@justask.rrerr.net" # Name of the "Anonymous" user. (e.g. "Anonymous Coward", "Arno Nym", "Mr. X", ...) anonymous_name: "Anonymous" diff --git a/db/migrate/20220909220449_add_webpush_app.rb b/db/migrate/20220909220449_add_webpush_app.rb index e527d64c..efa8a205 100644 --- a/db/migrate/20220909220449_add_webpush_app.rb +++ b/db/migrate/20220909220449_add_webpush_app.rb @@ -7,7 +7,7 @@ class AddWebpushApp < ActiveRecord::Migration[6.1] vapid_keypair = Webpush.generate_key.to_hash app = Rpush::Webpush::App.new app.name = "webpush" - app.certificate = vapid_keypair.merge(subject: "user@example.com").to_json # TODO: put an email address here + app.certificate = vapid_keypair.merge(subject: APP_CONFIG["contact_email"]).to_json app.connections = 1 app.save! end diff --git a/lib/use_case/question/create.rb b/lib/use_case/question/create.rb index abe57516..db92a068 100644 --- a/lib/use_case/question/create.rb +++ b/lib/use_case/question/create.rb @@ -31,7 +31,7 @@ module UseCase inbox = ::Inbox.create!(user: target_user, question: question, new: true) webpush_app = ::Rpush::App.find_by(name: "webpush") - target_user.push_notification(webpush_app, inbox) + target_user.push_notification(webpush_app, inbox) if webpush_app { status: 201,