Skip to content

A platform for community discussion. Free, open, simple.

FIX: update TopicEmbed's title and user correctly

Kyle Zhaobaf413d527d1
diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb
index db33afe9cdb..e12f285f59d 100644
--- a/app/models/topic_embed.rb
+++ b/app/models/topic_embed.rb
@@ -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)
+ 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
diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb
index 5d0468fb1af..92d6cfd83b0 100644
--- a/spec/models/topic_embed_spec.rb
+++ b/spec/models/topic_embed_spec.rb
@@ -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