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

This commit is contained in:
Sérgio Saquetim 2024-05-30 12:04:36 -03:00 committed by GitHub
parent 59c1b80203
commit 766231b102
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 1 deletions

View File

@ -61,7 +61,7 @@ class TopicEmbed < ActiveRecord::Base
if embed.blank? if embed.blank?
Topic.transaction do Topic.transaction do
if eh = EmbeddableHost.record_for_url(url) if eh = EmbeddableHost.record_for_url(url)
tags = eh.tags.presence || tags tags = eh.tags.presence&.map(&:name) || tags
user = eh.user.presence || user user = eh.user.presence || user
end end

View File

@ -316,6 +316,36 @@ RSpec.describe TopicEmbed do
end end
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 context "when updating an existing post with new tags and a different user" do
fab!(:tag1) { Fabricate(:tag, name: "interesting") } fab!(:tag1) { Fabricate(:tag, name: "interesting") }
fab!(:tag2) { Fabricate(:tag, name: "article") } fab!(:tag2) { Fabricate(:tag, name: "article") }