Skip to content

A platform for community discussion. Free, open, simple.

UX: Notify only newly added mentions on post edits (#44087)

Bryce Huhtala1cb76a9fd1d6
diff --git a/app/jobs/regular/post_alert.rb b/app/jobs/regular/post_alert.rb
index b3aa1c1f00b..492ad338768 100644
--- a/app/jobs/regular/post_alert.rb
+++ b/app/jobs/regular/post_alert.rb
@@ -7,7 +7,11 @@ module Jobs
if post&.topic && post.raw.present?
opts = args[:options] || {}
new_record = true == args[:new_record]
- PostAlerter.new(opts).after_save_post(post, new_record)
+ PostAlerter.new(opts).after_save_post(
+ post,
+ new_record,
+ added_mentions: args[:added_mentions],
+ )
end
end
end
diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb
index 6242ad61634..837947bb2d7 100644
--- a/app/services/post_alerter.rb
+++ b/app/services/post_alerter.rb
@@ -138,7 +138,7 @@ class PostAlerter
(post.post_type == Post.types[:whisper] && post.action_code.nil?)
end
- def after_save_post(post, new_record = false)
+ def after_save_post(post, new_record = false, added_mentions: nil)
notified = [post.user, post.last_editor].uniq
DiscourseEvent.trigger(:post_alerter_before_mentions, post, new_record, notified)
@@ -160,12 +160,18 @@ class PostAlerter
end
if mentioned_users
+ if added_mentions
+ mentioned_users =
+ mentioned_users.select { |user| added_mentions.include?(user.username_lower) }
+ end
mentioned_users = only_allowed_users(mentioned_users, post)
mentioned_users = mentioned_users - pm_watching_users(post)
notified += notify_users(mentioned_users - notified, :mentioned, post, mentioned_opts)
end
expand_group_mentions(mentioned_groups, post) do |group, users|
+ next if added_mentions && !added_mentions.include?(group.name.downcase)
+
users = only_allowed_users(users, post)
to_notify =
DiscoursePluginRegistry.apply_modifier(
@@ -178,7 +184,7 @@ class PostAlerter
notify_users(to_notify, :group_mentioned, post, mentioned_opts.merge(group: group))
end
- if mentioned_here
+ if mentioned_here && (!added_mentions || added_mentions.include?(SiteSetting.here_mention))
users = expand_here_mention(post, exclude_ids: notified.map(&:id))
users = only_allowed_users(users, post)
notified += notify_users(users - notified, :mentioned, post, mentioned_opts)
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index e7e35d31566..9fe7a7210ba 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -430,7 +430,7 @@ class PostRevisor
Topic.reset_highest(@topic.id) unless only_user_id_changed
post_process_post
- alert_users
+ alert_users(old_raw)
publish_changes
grant_badge
@@ -876,9 +876,17 @@ class PostRevisor
DiscourseEvent.trigger(:post_edited, @post, topic_changed?, self)
end
- def alert_users
+ def alert_users(old_raw)
return if @editor.id == Discourse::SYSTEM_USER_ID
- Jobs.enqueue(:post_alert, post_id: @post.id)
+
+ added_mentions =
+ if old_raw == @post.raw
+ []
+ else
+ @post.raw_mentions - PostAnalyzer.new(old_raw, @post.topic_id).raw_mentions
+ end
+
+ Jobs.enqueue(:post_alert, post_id: @post.id, added_mentions: added_mentions)
end
def publish_changes
diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb
index fe8712e3ef7..d362990f078 100644
--- a/spec/services/post_alerter_spec.rb
+++ b/spec/services/post_alerter_spec.rb
@@ -854,6 +854,39 @@ RSpec.describe PostAlerter do
expect(evil_trout.notifications.last.notification_type).to eq(Notification.types[:mentioned])
end
+ it "retains group mention records on unrelated revisions" do
+ Jobs.run_immediately!
+ post = create_post_with_alerts(raw: "Hello @group")
+
+ expect { post.revise(post.user, raw: "Hello again @group") }.not_to change(
+ evil_trout.notifications,
+ :count,
+ )
+ expect(GroupMention.where(post_id: post.id).pluck(:group_id)).to contain_exactly(group.id)
+ end
+
+ it "notifies only members of a newly added group on revision" do
+ Jobs.run_immediately!
+ post = create_post_with_alerts(raw: "Hello @group")
+ evil_trout.notifications.destroy_all
+
+ another_group =
+ Fabricate(:group, name: "another-group", mentionable_level: Group::ALIAS_LEVELS[:everyone])
+ another_group.add(coding_horror)
+
+ expect { post.revise(post.user, raw: "Hello @group and @another-group") }.to not_change(
+ evil_trout.notifications,
+ :count,
+ ).and change(coding_horror.notifications, :count).by(1)
+ expect(coding_horror.notifications.last.notification_type).to eq(
+ Notification.types[:group_mentioned],
+ )
+ expect(GroupMention.where(post_id: post.id).pluck(:group_id)).to contain_exactly(
+ group.id,
+ another_group.id,
+ )
+ end
+
it "triggers :before_create_notifications_for_users" do
events = DiscourseEvent.track_events { post }
expect(events).to include(
@@ -883,6 +916,45 @@ RSpec.describe PostAlerter do
}.not_to change(evil_trout.notifications, :count)
end
+ it "retains the existing notification when a mention is removed" do
+ post = mention_post
+ notification = evil_trout.notifications.last
+
+ expect(post.revise(post.user, raw: "Hello everyone")).to eq(true)
+
+ expect(evil_trout.notifications.reload).to contain_exactly(notification)
+ end
+
+ it "notifies only a newly mentioned user when the previous notification is gone" do
+ post = create_post_with_alerts(user: user, raw: "Hello @eviltrout")
+ evil_trout.notifications.destroy_all
+
+ expect { post.revise(post.user, raw: "Hello @eviltrout and @codinghorror") }.to not_change(
+ evil_trout.notifications,
+ :count,
+ ).and change(coding_horror.notifications, :count).by(1)
+ expect(coding_horror.notifications.last.notification_type).to eq(
+ Notification.types[:mentioned],
+ )
+ end
+
+ it "notifies a user mentioned directly after a group mention" do
+ mentionable_group =
+ Fabricate(
+ :group,
+ name: "mentionable-group",
+ mentionable_level: Group::ALIAS_LEVELS[:everyone],
+ )
+ mentionable_group.add(evil_trout)
+ post = create_post_with_alerts(user: user, raw: "Hello @mentionable-group")
+
+ expect { post.revise(post.user, raw: "Hello @mentionable-group and @eviltrout") }.to change(
+ evil_trout.notifications,
+ :count,
+ ).by(1)
+ expect(evil_trout.notifications.last.notification_type).to eq(Notification.types[:mentioned])
+ end
+
it "doesn't notify the user who created the topic in regular mode" do
topic.notify_regular!(user)
mention_post