mirror of
https://github.com/Retrospring/retrospring.git
synced 2025-01-31 10:29:07 +01:00
Deduplicate notification sending logic and replace placeholder string
This commit is contained in:
parent
2da4767623
commit
93d4af3f0d
6 changed files with 34 additions and 16 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Inbox < ApplicationRecord
|
class Inbox < ApplicationRecord
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :question
|
belongs_to :question
|
||||||
|
@ -24,4 +26,15 @@ class Inbox < ApplicationRecord
|
||||||
self.question.destroy if self.question.can_be_removed?
|
self.question.destroy if self.question.can_be_removed?
|
||||||
self.destroy
|
self.destroy
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -9,6 +9,7 @@ class User < ApplicationRecord
|
||||||
include User::BanMethods
|
include User::BanMethods
|
||||||
include User::InboxMethods
|
include User::InboxMethods
|
||||||
include User::QuestionMethods
|
include User::QuestionMethods
|
||||||
|
include User::PushNotificationMethods
|
||||||
include User::ReactionMethods
|
include User::ReactionMethods
|
||||||
include User::RelationshipMethods
|
include User::RelationshipMethods
|
||||||
include User::TimelineMethods
|
include User::TimelineMethods
|
||||||
|
|
15
app/models/user/push_notification_methods.rb
Normal file
15
app/models/user/push_notification_methods.rb
Normal file
|
@ -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 MuteRule.where(user: f).any? { |rule| rule.applies_to? question }
|
||||||
next if user.muting?(question.user)
|
next if user.muting?(question.user)
|
||||||
|
|
||||||
Inbox.create(user_id: f.id, question_id: question_id, new: true)
|
inbox = Inbox.create(user_id: f.id, question_id: question_id, new: true)
|
||||||
|
f.push_notification(webpush_app, inbox)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
logger.info "failed to ask question: #{e.message}"
|
logger.info "failed to ask question: #{e.message}"
|
||||||
|
|
|
@ -58,6 +58,8 @@ en:
|
||||||
title: Failed to subscribe to push notifications
|
title: Failed to subscribe to push notifications
|
||||||
body: Please try again later
|
body: Please try again later
|
||||||
setup_fail: Failed to set up push notifications. Please try again later.
|
setup_fail: Failed to set up push notifications. Please try again later.
|
||||||
|
inbox:
|
||||||
|
title: New question from %{user}
|
||||||
report:
|
report:
|
||||||
confirm:
|
confirm:
|
||||||
title: "Are you sure you want to report this %{type}?"
|
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)
|
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.web_push_subscriptions.each do |s|
|
target_user.push_notification(webpush_app, inbox)
|
||||||
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
|
|
||||||
|
|
||||||
{
|
{
|
||||||
status: 201,
|
status: 201,
|
||||||
|
|
Loading…
Reference in a new issue