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

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.

diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb
index 380daaf..2b2523a 100644
--- a/app/models/topic_embed.rb
+++ b/app/models/topic_embed.rb
@@ -27,10 +27,10 @@ class TopicEmbed < ActiveRecord::Base
   end
 
   # 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?\:\/\//
 
-    if SiteSetting.embed_truncate
+    if SiteSetting.embed_truncate && cook_method.nil?
       contents = first_paragraph_from(contents)
     end
     contents ||= ''
@@ -47,7 +47,7 @@ class TopicEmbed < ActiveRecord::Base
       Topic.transaction do
         eh = EmbeddableHost.record_for_url(url)
 
-        cook_method = if SiteSetting.embed_support_markdown
+        cook_method ||= if SiteSetting.embed_support_markdown
           Post.cook_methods[:regular]
         else
           Post.cook_methods[:raw_html]
diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb
index decb502..6a4e002 100644
--- a/spec/models/topic_embed_spec.rb
+++ b/spec/models/topic_embed_spec.rb
@@ -99,11 +99,22 @@ describe TopicEmbed do
 
       it "creates the topic in the category passed as a parameter" do
         Jobs.run_immediately!
-        SiteSetting.embed_unlisted = false
         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).to eq(category)
       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
 
     context "post creation supports markdown rendering" do

GitHub sha: ef0471d7aec6c54c0463859d5eb945ebbcbb95a0

This commit appears in #14209 which was approved by eviltrout. It was merged by Falco.