Address review comments from @nilsding

Co-authored-by: nilsding <nilsding@nilsding.org>
This commit is contained in:
Karina Kwiatek 2022-12-25 23:30:06 +00:00
parent 89008364d9
commit 10c224b2fe
5 changed files with 10 additions and 8 deletions

View File

@ -22,14 +22,14 @@ class Ajax::WebPushController < AjaxController
@response[:message] = t(".subscription_count", count: current_user.web_push_subscriptions.count) @response[:message] = t(".subscription_count", count: current_user.web_push_subscriptions.count)
end end
def unsubscribe def unsubscribe # rubocop:disable Metrics/AbcSize
params.permit(:endpoint) params.permit(:endpoint)
removed = if params.key?(:endpoint) removed = if params.key?(:endpoint)
current_user.web_push_subscriptions.where("subscription ->> 'endpoint' = ?", params[:endpoint]).destroy_all current_user.web_push_subscriptions.where("subscription ->> 'endpoint' = ?", params[:endpoint]).destroy_all
else else
current_user.web_push_subscriptions.destroy_all current_user.web_push_subscriptions.destroy_all
end end
count = current_user.web_push_subscriptions.count count = current_user.web_push_subscriptions.count

View File

@ -19,7 +19,7 @@ class QuestionWorker
next if user.muting?(question.user) next if user.muting?(question.user)
inbox = Inbox.create(user_id: f.id, question_id:, new: true) 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 end
rescue StandardError => e rescue StandardError => e
logger.info "failed to ask question: #{e.message}" logger.info "failed to ask question: #{e.message}"

View File

@ -8,6 +8,8 @@ hostname: "justask.rrerr.net"
https: true https: true
email_from: "noreply@justask.rrerr.net" 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", ...) # Name of the "Anonymous" user. (e.g. "Anonymous Coward", "Arno Nym", "Mr. X", ...)
anonymous_name: "Anonymous" anonymous_name: "Anonymous"

View File

@ -7,7 +7,7 @@ class AddWebpushApp < ActiveRecord::Migration[6.1]
vapid_keypair = Webpush.generate_key.to_hash vapid_keypair = Webpush.generate_key.to_hash
app = Rpush::Webpush::App.new app = Rpush::Webpush::App.new
app.name = "webpush" 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.connections = 1
app.save! app.save!
end end

View File

@ -31,7 +31,7 @@ module UseCase
inbox = ::Inbox.create!(user: target_user, question: question, new: true) inbox = ::Inbox.create!(user: target_user, question: question, new: true)
webpush_app = ::Rpush::App.find_by(name: "webpush") 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, status: 201,