From 93d4af3f0d4bc04b499a7b8066c79fdf910848ce Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 11 Sep 2022 22:34:18 +0200 Subject: [PATCH] Deduplicate notification sending logic and replace placeholder string --- app/models/inbox.rb | 13 +++++++++++++ app/models/user.rb | 1 + app/models/user/push_notification_methods.rb | 15 +++++++++++++++ app/workers/question_worker.rb | 11 ++--------- config/locales/frontend.en.yml | 2 ++ lib/use_case/question/create.rb | 8 +------- 6 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 app/models/user/push_notification_methods.rb diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 5c84a055..73dbc433 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Inbox < ApplicationRecord belongs_to :user belongs_to :question @@ -24,4 +26,15 @@ class Inbox < ApplicationRecord self.question.destroy if self.question.can_be_removed? self.destroy end + + def as_push_notification + { + type: :inbox, + title: I18n.t( + "frontend.push_notifications.inbox.title", + user: question.author_is_anonymous ? user.profile.display_name : question.author.profile.safe_name + ), + body: question.content, + } + end end diff --git a/app/models/user.rb b/app/models/user.rb index 4b97f7d1..a5263e0c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,6 +9,7 @@ class User < ApplicationRecord include User::BanMethods include User::InboxMethods include User::QuestionMethods + include User::PushNotificationMethods include User::ReactionMethods include User::RelationshipMethods include User::TimelineMethods diff --git a/app/models/user/push_notification_methods.rb b/app/models/user/push_notification_methods.rb new file mode 100644 index 00000000..ea166b43 --- /dev/null +++ b/app/models/user/push_notification_methods.rb @@ -0,0 +1,15 @@ +module User::PushNotificationMethods + def push_notification(app, resource) + raise ArgumentError("Resource must respond to `as_push_notification`") unless resource.respond_to? :as_push_notification + + web_push_subscriptions.each do |s| + n = Rpush::Webpush::Notification.new + n.app = app + n.registration_ids = [s.subscription.symbolize_keys] + n.data = { + message: resource.as_push_notification + } + n.save! + end + end +end diff --git a/app/workers/question_worker.rb b/app/workers/question_worker.rb index be82dfb4..47a6a5b4 100644 --- a/app/workers/question_worker.rb +++ b/app/workers/question_worker.rb @@ -18,15 +18,8 @@ class QuestionWorker next if MuteRule.where(user: f).any? { |rule| rule.applies_to? question } next if user.muting?(question.user) - Inbox.create(user_id: f.id, question_id: question_id, new: true) - - f.web_push_subscriptions.each do |s| - n = Rpush::Webpush::Notification.new - n.app = webpush_app - n.registration_ids = [s.subscription.symbolize_keys] - n.data = { message: { title: "New question notif title", body: question.content }.to_json } - n.save! - end + inbox = Inbox.create(user_id: f.id, question_id: question_id, new: true) + f.push_notification(webpush_app, inbox) end rescue StandardError => e logger.info "failed to ask question: #{e.message}" diff --git a/config/locales/frontend.en.yml b/config/locales/frontend.en.yml index 3ca04e3c..19e8ea79 100644 --- a/config/locales/frontend.en.yml +++ b/config/locales/frontend.en.yml @@ -58,6 +58,8 @@ en: title: Failed to subscribe to push notifications body: Please try again later setup_fail: Failed to set up push notifications. Please try again later. + inbox: + title: New question from %{user} report: confirm: title: "Are you sure you want to report this %{type}?" diff --git a/lib/use_case/question/create.rb b/lib/use_case/question/create.rb index 4426c680..a0a55450 100644 --- a/lib/use_case/question/create.rb +++ b/lib/use_case/question/create.rb @@ -31,13 +31,7 @@ module UseCase inbox = ::Inbox.create!(user: target_user, question: question, new: true) webpush_app = Rpush::App.find_by(name: "webpush") - target_user.web_push_subscriptions.each do |s| - n = Rpush::Webpush::Notification.new - n.app = webpush_app - n.registration_ids = [s.subscription.symbolize_keys] - n.data = { message: { title: "New question notif title", body: question.content }.to_json } - n.save! - end + target_user.push_notification(webpush_app, inbox) { status: 201,