Skip to content

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

FIX: Award 'First Onebox' badge just for Oneboxed URLs. (#7974)

Bianca Nenciu7c83d2eeb261
diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb
index ac16eb82c16..46050a69e40 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 = post.post_analyzer.found_oneboxes?
+ @has_oneboxes = @doc.css("aside.onebox").count > 0
@size_cache = {}
@disable_loading_image = !!opts[:disable_loading_image]
@@ -506,13 +506,14 @@ class CookedPostProcessor
map[url] = true
if is_onebox
- @has_oneboxes = true
-
- Oneboxer.onebox(url,
+ onebox = Oneboxer.onebox(url,
invalidate_oneboxes: !!@opts[:invalidate_oneboxes],
user_id: @post&.user_id,
category_id: @post&.topic&.category_id
)
+
+ @has_oneboxes = true if onebox.present?
+ onebox
else
process_inline_onebox(element)
false
diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb
index 1b557930d26..6e5d82bfdf5 100644
--- a/spec/components/cooked_post_processor_spec.rb
+++ b/spec/components/cooked_post_processor_spec.rb
@@ -1238,18 +1238,31 @@ describe CookedPostProcessor do
end
context "onebox" do
- let(:post) { Fabricate(:post, raw: "onebox me:\n\nhttps://www.youtube.com/watch?v=Wji-BZ0oCwg\n") }
-
- before { Oneboxer.stubs(:onebox) }
+ before do
+ Oneboxer.stubs(:onebox).with(anything, anything).returns(nil)
+ Oneboxer.stubs(:onebox).with('https://discourse.org', anything).returns("<aside class=\"onebox whitelistedgeneric\">the rest of the onebox</aside>")
+ end
- it "awards a badge for using an onebox" do
+ it "awards the badge for using an onebox" do
+ post = Fabricate(:post, raw: "onebox me:\n\nhttps://discourse.org\n")
+ cpp = CookedPostProcessor.new(post)
cpp.post_process_oneboxes
cpp.grant_badges
expect(post.user.user_badges.where(badge_id: Badge::FirstOnebox).exists?).to eq(true)
end
- it "doesn't award the badge when the badge is disabled" do
+ it "does not award the badge when link is not oneboxed" do
+ post = Fabricate(:post, raw: "onebox me:\n\nhttp://example.com\n")
+ cpp = CookedPostProcessor.new(post)
+ cpp.post_process_oneboxes
+ cpp.grant_badges
+ expect(post.user.user_badges.where(badge_id: Badge::FirstOnebox).exists?).to eq(false)
+ end
+
+ it "does not award the badge when the badge is disabled" do
Badge.where(id: Badge::FirstOnebox).update_all(enabled: false)
+ post = Fabricate(:post, raw: "onebox me:\n\nhttps://discourse.org\n")
+ cpp = CookedPostProcessor.new(post)
cpp.post_process_oneboxes
cpp.grant_badges
expect(post.user.user_badges.where(badge_id: Badge::FirstOnebox).exists?).to eq(false)