A platform for community discussion. Free, open, simple.
DEV: Add modifiers for plugins to customize push notification translation arguments (#25889)
Mark VanLandinghamb426f85a81ef
diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rbindex 82597c550ba..08b833e90ed 100644--- a/app/services/post_alerter.rb+++ b/app/services/post_alerter.rb@@ -38,6 +38,12 @@ class PostAlerterDiscourseEvent.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])enddiff --git a/app/services/push_notification_pusher.rb b/app/services/push_notification_pusher.rbindex d3f396cc358..b0fdbebec8c 100644--- a/app/services/push_notification_pusher.rb+++ b/app/services/push_notification_pusher.rb@@ -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,diff --git a/plugins/chat/app/jobs/regular/chat/notify_watching.rb b/plugins/chat/app/jobs/regular/chat/notify_watching.rbindex e21e6603cae..0acf217c590 100644--- a/plugins/chat/app/jobs/regular/chat/notify_watching.rb+++ b/plugins/chat/app/jobs/regular/chat/notify_watching.rb@@ -60,6 +60,11 @@ module Jobstranslation_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,diff --git a/plugins/chat/spec/jobs/regular/chat/notify_watching_spec.rb b/plugins/chat/spec/jobs/regular/chat/notify_watching_spec.rbindex e66f1d8adcb..18aecd0f3b8 100644--- a/plugins/chat/spec/jobs/regular/chat/notify_watching_spec.rb+++ b/plugins/chat/spec/jobs/regular/chat/notify_watching_spec.rb@@ -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" dobefore { membership2.update!(muted: true) }diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rbindex b007a7108a8..ff4cf400020 100644--- a/spec/services/post_alerter_spec.rb+++ b/spec/services/post_alerter_spec.rb@@ -1386,6 +1386,59 @@ RSpec.describe PostAlerter doenddescribe ".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" doevil_trout.update_columns(suspended_till: 1.year.from_now)diff --git a/spec/services/push_notification_pusher_spec.rb b/spec/services/push_notification_pusher_spec.rbindex 97263e58702..b9b45512b1f 100644--- a/spec/services/push_notification_pusher_spec.rb+++ b/spec/services/push_notification_pusher_spec.rb@@ -207,5 +207,35 @@ RSpec.describe PushNotificationPusher do)endend++ 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+ endendend