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

This commit is contained in:
Bianca Nenciu
2019-08-08 19:45:18 +03:00
committed by Régis Hanol
parent 922c40f87c
commit 7c83d2eeb2
2 changed files with 23 additions and 9 deletions

View File

@ -28,7 +28,7 @@ class CookedPostProcessor
@cooking_options = @cooking_options.symbolize_keys @cooking_options = @cooking_options.symbolize_keys
@doc = Nokogiri::HTML::fragment(post.cook(post.raw, @cooking_options)) @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 = {} @size_cache = {}
@disable_loading_image = !!opts[:disable_loading_image] @disable_loading_image = !!opts[:disable_loading_image]
@ -506,13 +506,14 @@ class CookedPostProcessor
map[url] = true map[url] = true
if is_onebox if is_onebox
@has_oneboxes = true onebox = Oneboxer.onebox(url,
Oneboxer.onebox(url,
invalidate_oneboxes: !!@opts[:invalidate_oneboxes], invalidate_oneboxes: !!@opts[:invalidate_oneboxes],
user_id: @post&.user_id, user_id: @post&.user_id,
category_id: @post&.topic&.category_id category_id: @post&.topic&.category_id
) )
@has_oneboxes = true if onebox.present?
onebox
else else
process_inline_onebox(element) process_inline_onebox(element)
false false

View File

@ -1238,18 +1238,31 @@ describe CookedPostProcessor do
end end
context "onebox" do context "onebox" do
let(:post) { Fabricate(:post, raw: "onebox me:\n\nhttps://www.youtube.com/watch?v=Wji-BZ0oCwg\n") } 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
before { Oneboxer.stubs(:onebox) } it "awards the badge for using an onebox" do
post = Fabricate(:post, raw: "onebox me:\n\nhttps://discourse.org\n")
it "awards a badge for using an onebox" do cpp = CookedPostProcessor.new(post)
cpp.post_process_oneboxes cpp.post_process_oneboxes
cpp.grant_badges cpp.grant_badges
expect(post.user.user_badges.where(badge_id: Badge::FirstOnebox).exists?).to eq(true) expect(post.user.user_badges.where(badge_id: Badge::FirstOnebox).exists?).to eq(true)
end 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) 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.post_process_oneboxes
cpp.grant_badges cpp.grant_badges
expect(post.user.user_badges.where(badge_id: Badge::FirstOnebox).exists?).to eq(false) expect(post.user.user_badges.where(badge_id: Badge::FirstOnebox).exists?).to eq(false)