DEV: Allow passing cook_method to TopicEmbed.import to override default (#14209)

DEV: Allow passing cook_method to TopicEmbed.import to override default

This will be used in the rss-polling plugin when we want to have
oneboxes on feed content, like youtube for example.
This commit is contained in:
Rafael dos Santos Silva 2021-09-01 15:46:39 -03:00 committed by GitHub
parent c6f1818b85
commit ef0471d7ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View File

@ -27,10 +27,10 @@ class TopicEmbed < ActiveRecord::Base
end end
# Import an article from a source (RSS/Atom/Other) # Import an article from a source (RSS/Atom/Other)
def self.import(user, url, title, contents, category_id: nil) def self.import(user, url, title, contents, category_id: nil, cook_method: nil)
return unless url =~ /^https?\:\/\// return unless url =~ /^https?\:\/\//
if SiteSetting.embed_truncate if SiteSetting.embed_truncate && cook_method.nil?
contents = first_paragraph_from(contents) contents = first_paragraph_from(contents)
end end
contents ||= '' contents ||= ''
@ -47,7 +47,7 @@ class TopicEmbed < ActiveRecord::Base
Topic.transaction do Topic.transaction do
eh = EmbeddableHost.record_for_url(url) eh = EmbeddableHost.record_for_url(url)
cook_method = if SiteSetting.embed_support_markdown cook_method ||= if SiteSetting.embed_support_markdown
Post.cook_methods[:regular] Post.cook_methods[:regular]
else else
Post.cook_methods[:raw_html] Post.cook_methods[:raw_html]

View File

@ -99,11 +99,22 @@ describe TopicEmbed do
it "creates the topic in the category passed as a parameter" do it "creates the topic in the category passed as a parameter" do
Jobs.run_immediately! Jobs.run_immediately!
SiteSetting.embed_unlisted = false
imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "some random content", category_id: category.id) imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "some random content", category_id: category.id)
expect(imported_post.topic.category).not_to eq(embeddable_host.category) expect(imported_post.topic.category).not_to eq(embeddable_host.category)
expect(imported_post.topic.category).to eq(category) expect(imported_post.topic.category).to eq(category)
end end
it "respects overriding the cook_method when asked" do
Jobs.run_immediately!
SiteSetting.embed_support_markdown = false
stub_request(:get, "https://www.youtube.com/watch?v=K56soYl0U1w")
.to_return(status: 200, body: "", headers: {})
stub_request(:get, "https://www.youtube.com/embed/K56soYl0U1w")
.to_return(status: 200, body: "", headers: {})
imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "https://www.youtube.com/watch?v=K56soYl0U1w", cook_method: Post.cook_methods[:regular])
expect(imported_post.cooked).to match(/onebox|iframe/)
end
end end
context "post creation supports markdown rendering" do context "post creation supports markdown rendering" do