FIX: when a post is edited by someone other than the original author and a mention is added, the mention notification is from the person who edited

This commit is contained in:
Neil Lalonde 2016-03-08 15:26:06 -05:00
parent 5771d2aee2
commit 091e7ef3ca
3 changed files with 31 additions and 9 deletions

View file

@ -222,6 +222,10 @@ class Post < ActiveRecord::Base
@acting_user = pu
end
def last_editor
self.last_editor_id ? (User.find_by_id(self.last_editor_id) || user) : user
end
def whitelisted_spam_hosts
hosts = SiteSetting

View file

@ -38,15 +38,24 @@ class PostAlerter
# mentions (users/groups)
mentioned_groups, mentioned_users = extract_mentions(post)
if mentioned_groups || mentioned_users
mentioned_opts = {}
if post.last_editor_id != post.user_id
# Mention comes from an edit by someone else, so notification should say who added the mention.
editor = post.last_editor
mentioned_opts = {user_id: editor.id, original_username: editor.username, display_username: editor.username}
end
expand_group_mentions(mentioned_groups, post) do |group, users|
notify_users(users - notified, :group_mentioned, post, group: group)
notify_users(users - notified, :group_mentioned, post, mentioned_opts.merge({group: group}))
notified += users
end
if mentioned_users
notify_users(mentioned_users - notified, :mentioned, post)
notify_users(mentioned_users - notified, :mentioned, post, mentioned_opts)
notified += mentioned_users
end
end
# replies
reply_to_user = post.reply_notification_target
@ -232,7 +241,7 @@ class PostAlerter
# Make sure the user can see the post
return unless Guardian.new(user).can_see?(post)
notifier_id = opts[:user_id] || post.user_id
notifier_id = opts[:user_id] || post.user_id # xxxxx look at revision history
# apply muting here
return if notifier_id && MutedUser.where(user_id: user.id, muted_user_id: notifier_id)
@ -285,7 +294,7 @@ class PostAlerter
end
original_post = post
original_username = opts[:display_username] || post.username
original_username = opts[:display_username] || post.username # xxxxx need something here too
if collapsed
post = first_unread_post(user, post.topic) || post

View file

@ -251,6 +251,15 @@ describe PostAlerter do
}.not_to change(user.notifications, :count)
end
it "notification comes from editor is mention is added later" do
admin = Fabricate(:admin)
post = create_post_with_alerts(user: user, raw: 'No mention here.')
expect {
post.revise(admin, { raw: "Mention @eviltrout in this edit." })
}.to change(evil_trout.notifications, :count)
n = evil_trout.notifications.last
n.data_hash["original_username"].should == admin.username
end
end
describe ".create_notification" do