mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 11:02:46 +08:00
DEV: Add modifiers for plugins to customize push notification translation arguments (#25889)
This commit is contained in:
parent
eea7af09fd
commit
b426f85a81
|
@ -38,6 +38,12 @@ class PostAlerter
|
|||
DiscourseEvent.trigger(:pre_notification_alert, user, payload)
|
||||
|
||||
if user.allow_live_notifications?
|
||||
payload =
|
||||
DiscoursePluginRegistry.apply_modifier(
|
||||
:post_alerter_live_notification_payload,
|
||||
payload,
|
||||
user,
|
||||
)
|
||||
MessageBus.publish("/notification-alert/#{user.id}", payload, user_ids: [user.id])
|
||||
end
|
||||
|
||||
|
|
|
@ -49,6 +49,10 @@ class PushNotificationPusher
|
|||
"discourse_push_notifications.popup.#{Notification.types[payload[:notification_type]]}"
|
||||
end
|
||||
|
||||
# Payload modifier used to adjust arguments to the translation
|
||||
payload =
|
||||
DiscoursePluginRegistry.apply_modifier(:push_notification_pusher_title_payload, payload)
|
||||
|
||||
I18n.t(
|
||||
translation_key,
|
||||
site_title: SiteSetting.title,
|
||||
|
|
|
@ -60,6 +60,11 @@ module Jobs
|
|||
|
||||
translation_args = { username: @creator.username }
|
||||
translation_args[:channel] = @chat_channel.title(user) unless @is_direct_message_channel
|
||||
translation_args =
|
||||
DiscoursePluginRegistry.apply_modifier(
|
||||
:chat_notification_translation_args,
|
||||
translation_args,
|
||||
)
|
||||
|
||||
payload = {
|
||||
username: @creator.username,
|
||||
|
|
|
@ -62,6 +62,29 @@ RSpec.describe Jobs::Chat::NotifyWatching do
|
|||
)
|
||||
end
|
||||
|
||||
context "with chat_notification_translation_args plugin_modifier" do
|
||||
let(:modifier_block) do
|
||||
Proc.new do |args|
|
||||
args[:username] = "Hijacked"
|
||||
args
|
||||
end
|
||||
end
|
||||
it "Allows for changes to the translation args" do
|
||||
plugin_instance = Plugin::Instance.new
|
||||
plugin_instance.register_modifier(:chat_notification_translation_args, &modifier_block)
|
||||
|
||||
messages = notification_messages_for(user2)
|
||||
|
||||
expect(messages.first.data[:translated_title]).to start_with("Hijacked")
|
||||
ensure
|
||||
DiscoursePluginRegistry.unregister_modifier(
|
||||
plugin_instance,
|
||||
:chat_notification_translation_args,
|
||||
&modifier_block
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "when the channel is muted via membership preferences" do
|
||||
before { membership2.update!(muted: true) }
|
||||
|
||||
|
|
|
@ -1386,6 +1386,59 @@ RSpec.describe PostAlerter do
|
|||
end
|
||||
|
||||
describe ".create_notification_alert" do
|
||||
before { evil_trout.update_columns(last_seen_at: 10.minutes.ago) }
|
||||
|
||||
it "publishes notification to notification-alert MessageBus channel" do
|
||||
messages =
|
||||
MessageBus.track_publish("/notification-alert/#{evil_trout.id}") do
|
||||
PostAlerter.create_notification_alert(
|
||||
user: evil_trout,
|
||||
post: post,
|
||||
notification_type: Notification.types[:mentioned],
|
||||
excerpt: "excerpt",
|
||||
username: "username",
|
||||
)
|
||||
end
|
||||
|
||||
expect(messages.size).to eq(1)
|
||||
expect(messages.first.data[:username]).to eq("username")
|
||||
expect(messages.first.data[:post_url]).to eq(post.url)
|
||||
end
|
||||
|
||||
let(:modifier_block) do
|
||||
Proc.new do |payload|
|
||||
payload[:username] = "gotcha"
|
||||
payload[:post_url] = "stolen_url"
|
||||
payload
|
||||
end
|
||||
end
|
||||
|
||||
it "applies the post_alerter_live_notification_payload modifier" do
|
||||
plugin_instance = Plugin::Instance.new
|
||||
plugin_instance.register_modifier(:post_alerter_live_notification_payload, &modifier_block)
|
||||
|
||||
messages =
|
||||
MessageBus.track_publish("/notification-alert/#{evil_trout.id}") do
|
||||
PostAlerter.create_notification_alert(
|
||||
user: evil_trout,
|
||||
post: post,
|
||||
notification_type: Notification.types[:mentioned],
|
||||
excerpt: "excerpt",
|
||||
username: "username",
|
||||
)
|
||||
end
|
||||
|
||||
expect(messages.size).to eq(1)
|
||||
expect(messages.first.data[:username]).to eq("gotcha")
|
||||
expect(messages.first.data[:post_url]).to eq("stolen_url")
|
||||
ensure
|
||||
DiscoursePluginRegistry.unregister_modifier(
|
||||
plugin_instance,
|
||||
:post_alerter_live_notification_payload,
|
||||
&modifier_block
|
||||
)
|
||||
end
|
||||
|
||||
it "does nothing for suspended users" do
|
||||
evil_trout.update_columns(suspended_till: 1.year.from_now)
|
||||
|
||||
|
|
|
@ -207,5 +207,35 @@ RSpec.describe PushNotificationPusher do
|
|||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe "push_notification_pusher_title_payload modifier" do
|
||||
let(:modifier_block) do
|
||||
Proc.new do |payload|
|
||||
payload[:username] = "super_hijacked"
|
||||
payload
|
||||
end
|
||||
end
|
||||
it "Allows modifications to the payload passed to the translation" do
|
||||
plugin_instance = Plugin::Instance.new
|
||||
plugin_instance.register_modifier(:push_notification_pusher_title_payload, &modifier_block)
|
||||
|
||||
message = execute_push(notification_type: Notification.types[:mentioned], post_number: 2)
|
||||
|
||||
expect(message[:title]).to eq(
|
||||
I18n.t(
|
||||
"discourse_push_notifications.popup.mentioned",
|
||||
site_title: SiteSetting.title,
|
||||
topic: topic_title,
|
||||
username: "super_hijacked",
|
||||
),
|
||||
)
|
||||
ensure
|
||||
DiscoursePluginRegistry.unregister_modifier(
|
||||
plugin_instance,
|
||||
:push_notification_pusher_title_payload,
|
||||
&modifier_block
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user