Deduplicate notification sending logic and replace placeholder string
This commit is contained in:
parent
2da4767623
commit
93d4af3f0d
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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}"
|
||||
|
|
|
@ -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}?"
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue