FIX: Send quote notifications to correct users when prioritizing full names (#17030)

This commit is contained in:
Isaac Janzen 2022-06-09 11:52:28 -05:00 committed by GitHub
parent 3f569f1185
commit 6ae761604a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 6 deletions

View File

@ -583,8 +583,11 @@ class PostAlerter
# TODO: Move to post-analyzer? # TODO: Move to post-analyzer?
# Returns a list of users who were quoted in the post # Returns a list of users who were quoted in the post
def extract_quoted_users(post) def extract_quoted_users(post)
usernames = post.raw.scan(/\[quote=\"([^,]+),.+\"\]/).uniq.map { |q| q.first.strip.downcase } usernames = if SiteSetting.display_name_on_posts && !SiteSetting.prioritize_username_in_ux
post.raw.scan(/username:([[:alnum:]]*)"(?=\])/)
else
post.raw.scan(/\[quote=\"([^,]+),.+\"\]/)
end.uniq.map { |q| q.first.strip.downcase }
User.where.not(id: post.user_id).where(username_lower: usernames) User.where.not(id: post.user_id).where(username_lower: usernames)
end end

View File

@ -348,7 +348,7 @@ describe PostAlerter do
fab!(:topic) { Fabricate(:topic, category: category) } fab!(:topic) { Fabricate(:topic, category: category) }
it 'does not notify for muted users' do it 'does not notify for muted users' do
post = Fabricate(:post, raw: '[quote="EvilTrout, post:1"]whatup[/quote]', topic: topic) post = Fabricate(:post, raw: '[quote="Eviltrout, post:1"]whatup[/quote]', topic: topic)
MutedUser.create!(user_id: evil_trout.id, muted_user_id: post.user_id) MutedUser.create!(user_id: evil_trout.id, muted_user_id: post.user_id)
expect { expect {
@ -366,7 +366,7 @@ describe PostAlerter do
end end
it 'does not notify for users with new reply notification' do it 'does not notify for users with new reply notification' do
post = Fabricate(:post, raw: '[quote="EvilTrout, post:1"]whatup[/quote]', topic: topic) post = Fabricate(:post, raw: '[quote="eviltRout, post:1"]whatup[/quote]', topic: topic)
notification = Notification.create!(topic: post.topic, notification = Notification.create!(topic: post.topic,
post_number: post.post_number, post_number: post.post_number,
read: false, read: false,
@ -388,7 +388,7 @@ describe PostAlerter do
expect { expect {
2.times do 2.times do
create_post_with_alerts( create_post_with_alerts(
raw: '[quote="EvilTrout, post:1"]whatup[/quote]', raw: '[quote="eviltrout, post:1"]whatup[/quote]',
topic: topic topic: topic
) )
end end
@ -410,12 +410,77 @@ describe PostAlerter do
end end
it "triggers :before_create_notifications_for_users" do it "triggers :before_create_notifications_for_users" do
post = Fabricate(:post, raw: '[quote="EvilTrout, post:1"]whatup[/quote]') post = Fabricate(:post, raw: '[quote="eviltrout, post:1"]whatup[/quote]')
events = DiscourseEvent.track_events do events = DiscourseEvent.track_events do
PostAlerter.post_created(post) PostAlerter.post_created(post)
end end
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[evil_trout], post]) expect(events).to include(event_name: :before_create_notifications_for_users, params: [[evil_trout], post])
end end
context "notifications when prioritizing full names" do
before do
SiteSetting.prioritize_username_in_ux = false
SiteSetting.display_name_on_posts = true
end
it 'sends to correct user' do
quote = <<~MD
[quote="#{evil_trout.name}, post:1, username:#{evil_trout.username}"]whatup[/quote]
MD
expect {
create_post_with_alerts(
raw: quote,
topic: topic
)
}.to change(evil_trout.notifications, :count).by(1)
end
it 'sends to correct users when nested quotes with multiple users' do
quote = <<~MD
[quote="#{evil_trout.name}, post:1, username:#{evil_trout.username}"]this [quote="#{walterwhite.name}, post:2, username:#{walterwhite.username}"]whatup[/quote][/quote]
MD
expect {
create_post_with_alerts(
raw: quote,
topic: topic
)
}.to change(evil_trout.notifications, :count).by(1)
.and change(walterwhite.notifications, :count).by(1)
end
it 'sends to correct users when multiple quotes' do
user = Fabricate(:user)
quote = <<~MD
[quote="#{evil_trout.name}, post:1, username:#{evil_trout.username}"]"username:#{user.username}" [/quote]/n [quote="#{walterwhite.name}, post:2, username:#{walterwhite.username}"]whatup[/quote]
MD
expect {
create_post_with_alerts(
raw: quote,
topic: topic
)
}.to change(evil_trout.notifications, :count).by(1)
.and change(walterwhite.notifications, :count).by(1)
.and change(user.notifications, :count).by(0)
end
it "sends to correct user when user has a full name that matches another user's username" do
user_with_matching_full_name = Fabricate(:user, name: evil_trout.username)
quote = <<~MD
[quote="#{user_with_matching_full_name.name}, post:1, username:#{user_with_matching_full_name.username}"]this [/quote]
MD
expect {
create_post_with_alerts(
raw: quote,
topic: topic
)
}.to change(user_with_matching_full_name.notifications, :count).by(1)
.and change(evil_trout.notifications, :count).by(0)
end
end
end end
context 'linked' do context 'linked' do