FIX: Avoid sending user emails if @ mentioning a staged user in a topic (#26102)

Avoid sending user emails if @ mentioning a staged user

Some cases, unknowingly mentioning a staged user would invite
them into topics, sending them an email about it.
This commit is contained in:
Natalie Tay 2024-03-13 11:05:34 +08:00 committed by GitHub
parent 63dd08ad59
commit 0b41b236d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 5 deletions

View File

@ -100,7 +100,13 @@ class NotificationEmailer
user = notification.user
return 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
end
return unless EMAILABLE_POST_TYPES.include?(post_type)

View File

@ -18,6 +18,7 @@ RSpec.describe NotificationEmailer do
notification_type: Notification.types[type],
topic: topic,
post_number: post.post_number,
skip_send_email: true,
)
end
@ -42,7 +43,8 @@ RSpec.describe NotificationEmailer do
it "enqueues a job if the user is staged for non-linked and non-quoted types" do
notification.user.staged = true
if type == :user_linked || type == :user_quoted
if type == :user_linked || type == :user_quoted || type == :user_mentioned ||
type == :group_mentioned
expect_not_enqueued_with(job: :user_email, args: { type: type }) do
NotificationEmailer.process_notification(notification, no_delay: no_delay)
end
@ -59,7 +61,8 @@ RSpec.describe NotificationEmailer do
notification.user.staged = true
SiteSetting.must_approve_users = true
if type == :user_linked || type == :user_quoted
if type == :user_linked || type == :user_quoted || type == :user_mentioned ||
type == :group_mentioned
expect_not_enqueued_with(job: :user_email, args: { type: type }) do
NotificationEmailer.process_notification(notification, no_delay: no_delay)
end
@ -281,7 +284,7 @@ RSpec.describe NotificationEmailer do
let(: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" do
plugin.register_email_notification_filter { |_| true }
@ -301,4 +304,22 @@ RSpec.describe NotificationEmailer do
end
end
end
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
end
end

View File

@ -112,7 +112,7 @@ RSpec.describe PostAlerter do
expect(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" do
pm = Fabricate(:topic, archetype: "private_message", category_id: nil)
op =
Fabricate(:post, topic: pm, user: pm.user, raw: "Hello @#{user.username}, nice to meet you")