FIX: update TopicEmbed's title and user correctly

This commit is contained in:
Kyle Zhao 2018-08-21 18:19:03 +08:00
parent 0f07494b12
commit baf413d527
2 changed files with 35 additions and 13 deletions

View File

@ -71,16 +71,28 @@ class TopicEmbed < ActiveRecord::Base
else
absolutize_urls(url, contents)
post = embed.post
# Update the topic if it changed
if post && post.topic && content_sha1 != embed.content_sha1
post_revision_args = {
raw: absolutize_urls(url, contents),
user_id: user.id,
title: title,
}
post.revise(user, post_revision_args, skip_validations: true, bypass_rate_limiter: true)
embed.update_column(:content_sha1, content_sha1)
# Update the topic if it changed
if post&.topic
revision = {}
if post.user != user
PostOwnerChanger.new(
post_ids: [post.id],
topic_id: post.topic_id,
new_owner: user,
acting_user: Discourse.system_user
).change_owner!
# make sure the post returned has the right author
post.reload
end
revision[:raw] = absolutize_urls(url, contents) if content_sha1 != embed.content_sha1
revision[:title] = title if title != post.topic.title
post.revise(user, revision, skip_validations: true, bypass_rate_limiter: true) unless revision.empty?
embed.update_column(:content_sha1, content_sha1) if revision[:raw]
end
end

View File

@ -40,14 +40,24 @@ describe TopicEmbed do
expect(post.topic.category).to eq(embeddable_host.category)
end
it "Supports updating the post" do
new_user = Fabricate(:user)
it "Supports updating the post title" do
post = TopicEmbed.import(user, url, "I am a new title", contents)
post = TopicEmbed.import(new_user, url, "I am a new title", "muhahaha new contents!")
expect(post.topic.title).to eq("I am a new title")
end
it "Supports updating the post content" do
post = TopicEmbed.import(user, url, title, "muhahaha new contents!")
expect(post.cooked).to match(/new contents/)
expect(post.topic.title).to eq("I am a new title")
end
it "Supports updating the post author" do
new_user = Fabricate(:user)
post = TopicEmbed.import(new_user, url, title, contents)
expect(post.user).to eq(new_user)
expect(post.topic.user).to eq(new_user)
end
it "Should leave uppercase Feed Entry URL untouched in content" do