FIX: Extend username updates to self-mentions (#20071)

Posts with self-mentions aren't updated with username updates. This happens
because mention `UserAction` entries aren't logged for self-mentions.

This change updates the lookup of `Post` and `PostRevision` with mentions to bypass
`UserAction` entries.
This commit is contained in:
Selase Krakani 2023-02-02 12:33:42 +00:00 committed by GitHub
parent efc74cccaf
commit 2e78045af1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 4 deletions

View File

@ -48,8 +48,7 @@ module Jobs
Post
.with_deleted
.joins(mentioned("posts.id"))
.where("a.user_id = :user_id", user_id: @user_id)
.where("raw ILIKE ?", "%@#{@old_username}%")
.find_each do |post|
update_post(post)
updated_post_ids << post.id
@ -64,8 +63,7 @@ module Jobs
def update_revisions
PostRevision
.joins(mentioned("post_revisions.post_id"))
.where("a.user_id = :user_id", user_id: @user_id)
.where("modifications SIMILAR TO ?", "%(raw|cooked)%@#{@old_username}%")
.find_each { |revision| update_revision(revision) }
PostRevision

View File

@ -327,6 +327,54 @@ RSpec.describe UsernameChanger do
)
end
it "replaces mentions of oneself in posts" do
post = create_post_and_change_username(raw: "Hello @#{user.username}", user: user)
expect(post.raw).to eq("Hello @bar")
expect(post.cooked).to eq(%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a></p>))
end
it "replaces mentions of oneself in revisions" do
revisions = [
{ raw: "Hello Foo" },
{ title: "new topic title" },
{ raw: "Hello @#{user.username}!" },
{ raw: "Hello @#{user.username}!!" },
]
post =
create_post_and_change_username(raw: "Hello @#{user.username}", revisions: revisions)
expect(post.raw).to eq("Hello @bar!!")
expect(post.cooked).to eq(%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a>!!</p>))
expect(post.revisions.count).to eq(4)
expect(post.revisions[0].modifications["raw"][0]).to eq("Hello @bar")
expect(post.revisions[0].modifications["cooked"][0]).to eq(
%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a></p>),
)
expect(post.revisions[0].modifications["cooked"][1]).to eq("<p>Hello Foo</p>")
expect(post.revisions[1].modifications).to include("title")
expect(post.revisions[2].modifications["raw"][0]).to eq("Hello Foo")
expect(post.revisions[2].modifications["raw"][1]).to eq("Hello @bar!")
expect(post.revisions[2].modifications["cooked"][0]).to eq("<p>Hello Foo</p>")
expect(post.revisions[2].modifications["cooked"][1]).to eq(
%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a>!</p>),
)
expect(post.revisions[3].modifications["raw"][0]).to eq("Hello @bar!")
expect(post.revisions[3].modifications["raw"][1]).to eq("Hello @bar!!")
expect(post.revisions[3].modifications["cooked"][0]).to eq(
%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a>!</p>),
)
expect(post.revisions[3].modifications["cooked"][1]).to eq(
%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a>!!</p>),
)
end
context "when using Unicode usernames" do
before { SiteSetting.unicode_usernames = true }
let(:user) { Fabricate(:user, username: "թռչուն") }