Skip to content

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

FIX: Prevent crash importing topics on a tagged embeddable host (#27254)

Sérgio Saquetim766231b102b8
diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb
index 0e67c1593bc..07af6848537 100644
--- a/app/models/topic_embed.rb
+++ b/app/models/topic_embed.rb
@@ -61,7 +61,7 @@ class TopicEmbed < ActiveRecord::Base
if embed.blank?
Topic.transaction do
if eh = EmbeddableHost.record_for_url(url)
- tags = eh.tags.presence || tags
+ tags = eh.tags.presence&.map(&:name) || tags
user = eh.user.presence || user
end
diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb
index 10d670253d1..98861813137 100644
--- a/spec/models/topic_embed_spec.rb
+++ b/spec/models/topic_embed_spec.rb
@@ -316,6 +316,36 @@ RSpec.describe TopicEmbed do
end
end
+ context "when the embeddable host specifies the user and tags" do
+ fab!(:tag1) { Fabricate(:tag, name: "interesting") }
+ fab!(:tag2) { Fabricate(:tag, name: "article") }
+ fab!(:embeddable_host) { Fabricate(:embeddable_host, host: "tag-eviltrout.com") }
+
+ let!(:new_user) { Fabricate(:user) }
+ let(:tags) { [tag1.name, tag2.name] }
+ let(:embed_url_with_tags) { "http://tag-eviltrout.com/abcd" }
+
+ let(:imported_post) do
+ # passing user = system and tags = nil to ensure we're getting the user and tags from the embeddable host
+ # and not from the TopicEmbed.import method in the tests
+ TopicEmbed.import(Discourse.system_user, embed_url_with_tags, title, contents, tags: nil)
+ end
+
+ before do
+ embeddable_host.user = new_user
+ embeddable_host.tags = [tag1, tag2]
+ embeddable_host.save!
+ end
+
+ it "assigns the specified user as the author" do
+ expect(imported_post.user).to eq(new_user)
+ end
+
+ it "associates the specified tags with the topic" do
+ expect(imported_post.topic.tags).to contain_exactly(tag1, tag2)
+ end
+ end
+
context "when updating an existing post with new tags and a different user" do
fab!(:tag1) { Fabricate(:tag, name: "interesting") }
fab!(:tag2) { Fabricate(:tag, name: "article") }