A platform for community discussion. Free, open, simple.
FIX: Improve Onebox detection (#8019)
Bianca Nenciu0d22beb81db3
diff --git a/app/models/post_analyzer.rb b/app/models/post_analyzer.rbindex a9540320f14..cfc64e5308e 100644--- a/app/models/post_analyzer.rb+++ b/app/models/post_analyzer.rb@@ -9,10 +9,11 @@ class PostAnalyzer@raw = raw@topic_id = topic_id@onebox_urls = []+ @found_oneboxes = falseenddef found_oneboxes?- @onebox_urls.present?+ @found_oneboxesenddef has_oneboxes?@@ -36,7 +37,9 @@ class PostAnalyzerresult = Oneboxer.apply(cooked) do |url|@onebox_urls << urlOneboxer.invalidate(url) if opts[:invalidate_oneboxes]- Oneboxer.cached_onebox(url)+ onebox = Oneboxer.cached_onebox(url)+ @found_oneboxes = true if onebox.present?+ oneboxendcooked = result.to_html if result.changed?@@ -126,7 +129,7 @@ class PostAnalyzer# How many links are present in the postdef link_count- raw_links.size+ raw_links.size + @onebox_urls.sizeenddef cooked_strippeddiff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rbindex 46050a69e40..f709b3d9cff 100644--- a/lib/cooked_post_processor.rb+++ b/lib/cooked_post_processor.rb@@ -28,7 +28,7 @@ class CookedPostProcessor@cooking_options = @cooking_options.symbolize_keys@doc = Nokogiri::HTML::fragment(post.cook(post.raw, @cooking_options))- @has_oneboxes = @doc.css("aside.onebox").count > 0+ @has_oneboxes = post.post_analyzer.found_oneboxes?@size_cache = {}@disable_loading_image = !!opts[:disable_loading_image]diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rbindex 89199058c7d..1b35acd3d88 100644--- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb+++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb@@ -268,6 +268,7 @@ describe DiscourseNarrativeBot::NewUserNarrative dodescribe 'onebox tutorial' dobefore do+ Oneboxer.stubs(:cached_onebox).with('https://en.wikipedia.org/wiki/ROT13').returns('oneboxed Wikipedia')narrative.set_data(user, state: :tutorial_onebox, topic_id: topic.id)enddiff --git a/spec/models/post_analyzer_spec.rb b/spec/models/post_analyzer_spec.rbindex ed3000b8913..14afb3f80ec 100644--- a/spec/models/post_analyzer_spec.rb+++ b/spec/models/post_analyzer_spec.rb@@ -16,7 +16,7 @@ describe PostAnalyzer dobefore { Oneboxer.stubs(:onebox) }it 'fetches the cached onebox for any urls in the post' do- Oneboxer.expects(:cached_onebox).with url+ Oneboxer.expects(:cached_onebox).with(url).returns('something')post_analyzer.cook(raw, options)expect(post_analyzer.found_oneboxes?).to be(true)end