FIX: Update avatar in oneboxed posts when renaming user

This commit is contained in:
Gerhard Schlager 2018-05-14 13:15:31 +02:00
parent 49392fb105
commit 02cb84847c
2 changed files with 52 additions and 20 deletions

View File

@ -19,24 +19,19 @@ module Jobs
def update_posts
Post.with_deleted.where(post_conditions("posts.id"), post_condition_args).find_each do |post|
if update_raw!(post.raw)
post.update_columns(raw: post.raw, cooked: update_cooked(post.cooked))
end
post.raw = update_raw(post.raw)
post.cooked = update_cooked(post.cooked)
# update without running validations and hooks
post.update_columns(raw: post.raw, cooked: post.cooked)
end
end
def update_revisions
PostRevision.where(post_conditions("post_revisions.post_id"), post_condition_args).find_each do |revision|
changed = false
revision.modifications["raw"]&.each do |raw|
changed |= update_raw!(raw)
end
if changed
revision.modifications["cooked"].map! { |cooked| update_cooked(cooked) }
revision.save!
end
revision.modifications["raw"].map! { |raw| update_raw(raw) }
revision.modifications["cooked"].map! { |cooked| update_cooked(cooked) }
revision.save!
end
end
@ -112,11 +107,9 @@ module Jobs
{ mentioned: UserAction::MENTION, user_id: @user_id }
end
def update_raw!(raw)
changed = false
changed |= raw.gsub!(@raw_mention_regex, "@#{@new_username}")
changed |= raw.gsub!(@raw_quote_regex, "\\1#{@new_username}\\2")
changed
def update_raw(raw)
raw.gsub(@raw_mention_regex, "@#{@new_username}")
.gsub(@raw_quote_regex, "\\1#{@new_username}\\2")
end
# Uses Nokogiri instead of rebake, because it works for posts and revisions

View File

@ -252,7 +252,7 @@ describe UsernameChanger do
let(:quoted_post) { create_post(user: user, topic: topic, post_number: 1, raw: "quoted post") }
let(:avatar_url) { user.avatar_template.gsub("{size}", "40") }
it 'replaces the username in quote tags' do
it 'replaces the username in quote tags and updates avatar' do
post = create_post_and_change_username(raw: <<~RAW)
Lorem ipsum
@ -375,8 +375,47 @@ describe UsernameChanger do
expect(post.revisions[0].modifications["cooked"][0]).to match_html(expected_cooked)
end
end
end
# TODO spec for quotes in revisions
context 'oneboxes' do
let(:quoted_post) { create_post(user: user, topic: topic, post_number: 1, raw: "quoted post") }
let(:avatar_url) { user.avatar_template.gsub("{size}", "40") }
def protocol_relative_url(url)
url.sub(/^https?:/, '')
end
it 'updates avatar for linked topics and posts' do
raw = "#{quoted_post.full_url}\n#{quoted_post.topic.url}"
post = create_post_and_change_username(raw: raw)
expect(post.raw).to eq(raw)
expect(post.cooked).to match_html(<<~HTML)
<p><aside class="quote" data-post="#{quoted_post.post_number}" data-topic="#{quoted_post.topic.id}">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="20" height="20" src="#{avatar_url}" class="avatar">
<a href="#{protocol_relative_url(quoted_post.full_url)}">#{quoted_post.topic.title}</a>
</div>
<blockquote>
quoted post
</blockquote>
</aside>
<br>
<aside class="quote" data-post="#{quoted_post.post_number}" data-topic="#{quoted_post.topic.id}">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="20" height="20" src="#{avatar_url}" class="avatar">
<a href="#{protocol_relative_url(quoted_post.topic.url)}">#{quoted_post.topic.title}</a>
</div>
<blockquote>
quoted post
</blockquote>
</aside>
</p>
HTML
end
end
end