A platform for community discussion. Free, open, simple.
FIX: Avoid sending user emails if @ mentioning a staged user in a topic (#26102)
Natalie Tay0b41b236d7a1
diff --git a/app/services/notification_emailer.rb b/app/services/notification_emailer.rbindex 0614a623530..951896d3965 100644--- a/app/services/notification_emailer.rb+++ b/app/services/notification_emailer.rb@@ -100,7 +100,13 @@ class NotificationEmaileruser = notification.userreturn unless user.active? || user.staged?return if SiteSetting.must_approve_users? && !user.approved? && !user.staged?- return if user.staged? && (type == :user_linked || type == :user_quoted)+ if user.staged? &&+ (+ type == :user_linked || type == :user_quoted || type == :user_mentioned ||+ type == :group_mentioned+ )+ return+ endreturn unless EMAILABLE_POST_TYPES.include?(post_type)diff --git a/spec/services/notification_emailer_spec.rb b/spec/services/notification_emailer_spec.rbindex 3f93bb01c48..c4667cedc83 100644--- a/spec/services/notification_emailer_spec.rb+++ b/spec/services/notification_emailer_spec.rb@@ -18,6 +18,7 @@ RSpec.describe NotificationEmailer donotification_type: Notification.types[type],topic: topic,post_number: post.post_number,+ skip_send_email: true,)end@@ -42,7 +43,8 @@ RSpec.describe NotificationEmailer doit "enqueues a job if the user is staged for non-linked and non-quoted types" donotification.user.staged = true- if type == :user_linked || type == :user_quoted+ if type == :user_linked || type == :user_quoted || type == :user_mentioned ||+ type == :group_mentionedexpect_not_enqueued_with(job: :user_email, args: { type: type }) doNotificationEmailer.process_notification(notification, no_delay: no_delay)end@@ -59,7 +61,8 @@ RSpec.describe NotificationEmailer donotification.user.staged = trueSiteSetting.must_approve_users = true- if type == :user_linked || type == :user_quoted+ if type == :user_linked || type == :user_quoted || type == :user_mentioned ||+ type == :group_mentionedexpect_not_enqueued_with(job: :user_email, args: { type: type }) doNotificationEmailer.process_notification(notification, no_delay: no_delay)end@@ -281,7 +284,7 @@ RSpec.describe NotificationEmailer dolet(:no_delay) { true }let(:type) { :user_quoted }- after { DiscoursePluginRegistry.reset! }+ after { DiscoursePluginRegistry.reset_register! :email_notification_filters }it "sends email when all filters return true" doplugin.register_email_notification_filter { |_| true }@@ -301,4 +304,22 @@ RSpec.describe NotificationEmailer doendendend++ context "with a staged user" do+ context "when notification is mentioned or group_mentioned type" do+ it "doesn't enqueue the job to send user email" do+ staged_user = Fabricate(:staged)+ mentioned = create_notification(:mentioned, staged_user)+ group_mentioned = create_notification(:group_mentioned, staged_user)++ expect_not_enqueued_with(job: :user_email) do+ NotificationEmailer.process_notification(mentioned, no_delay: Time.zone.now)+ end++ expect_not_enqueued_with(job: :user_email) do+ NotificationEmailer.process_notification(group_mentioned, no_delay: Time.zone.now)+ end+ end+ end+ endenddiff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rbindex 420a4ec8952..2667926580e 100644--- a/spec/services/post_alerter_spec.rb+++ b/spec/services/post_alerter_spec.rb@@ -112,7 +112,7 @@ RSpec.describe PostAlerter doexpect(Notification.where(user_id: pm.user_id).count).to eq(1)end- it "notifies about private message even if direct mention" do+ it "prioritises 'private_message' type even if direct mention" dopm = Fabricate(:topic, archetype: "private_message", category_id: nil)op =Fabricate(:post, topic: pm, user: pm.user, raw: "Hello @#{user.username}, nice to meet you")