diff --git a/app/models/badge.rb b/app/models/badge.rb index 484631b5b2f..f9a175713ed 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -16,6 +16,7 @@ class Badge < ActiveRecord::Base FirstQuote = 15 FirstMention = 40 FirstEmoji = 41 + FirstOnebox = 42 ReadGuidelines = 16 Reader = 17 diff --git a/app/models/post_analyzer.rb b/app/models/post_analyzer.rb index 42328bf7240..f5ff4117b1c 100644 --- a/app/models/post_analyzer.rb +++ b/app/models/post_analyzer.rb @@ -5,6 +5,11 @@ class PostAnalyzer def initialize(raw, topic_id) @raw = raw @topic_id = topic_id + @found_oneboxes = false + end + + def found_oneboxes? + @found_oneboxes end # What we use to cook posts @@ -12,6 +17,7 @@ class PostAnalyzer cooked = PrettyText.cook(*args) result = Oneboxer.apply(cooked, topic_id: @topic_id) do |url, _| + @found_oneboxes = true Oneboxer.invalidate(url) if args.last[:invalidate_oneboxes] Oneboxer.cached_onebox url end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 1889212086a..f6858fb59ba 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2974,7 +2974,11 @@ en: first_mention: name: First Mention description: Mentioned a user in a Post - long_description: This badge is granted the first time you mention someone's @username in your post. Each mention generates a notification to that person, so they know about your post. Just begin typing `@` to mention any user or (if allowed) group – it's a convenient way to bring something to their attention. + long_description: This badge is granted the first time you mention someone's @username in your post. Each mention generates a notification to that person, so they know about your post. Just begin typing `@` to mention any user or (if allowed) group – it's a convenient way to bring something to their attention. + first_onebox: + name: First Onebox + description: Posted a link that was oneboxed + long_description: This badge is granted when you post a link which is expanded with extra information. admin_login: success: "Email Sent" diff --git a/db/fixtures/006_badges.rb b/db/fixtures/006_badges.rb index 3f02e0fe682..b9fc82f80af 100644 --- a/db/fixtures/006_badges.rb +++ b/db/fixtures/006_badges.rb @@ -386,6 +386,20 @@ Badge.seed do |b| b.system = true end +Badge.seed do |b| + b.id = Badge::FirstOnebox + b.default_name = "First Onebox" + b.badge_type_id = BadgeType::Bronze + b.multiple_grant = false + b.target_posts = true + b.show_posts = true + b.query = nil + b.badge_grouping_id = BadgeGrouping::GettingStarted + b.default_badge_grouping_id = BadgeGrouping::GettingStarted + b.trigger = Badge::Trigger::PostProcessed + b.system = true +end + Badge.where("NOT system AND id < 100").each do |badge| new_id = [Badge.maximum(:id) + 1, 100].max old_id = badge.id diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 665caeb813c..b60ad7fe9e3 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -17,7 +17,10 @@ class CookedPostProcessor @cooking_options = post.cooking_options || opts[:cooking_options] || {} @cooking_options[:topic_id] = post.topic_id @cooking_options = @cooking_options.symbolize_keys - @doc = Nokogiri::HTML::fragment(post.cook(post.raw, @cooking_options)) + + analyzer = post.post_analyzer + @doc = Nokogiri::HTML::fragment(analyzer.cook(post.raw, @cooking_options)) + @has_oneboxes = analyzer.found_oneboxes? @size_cache = {} end @@ -46,9 +49,8 @@ class CookedPostProcessor def grant_badges return unless Guardian.new.can_see?(@post) - if has_emoji? - BadgeGranter.grant(Badge.find(Badge::FirstEmoji), @post.user) - end + BadgeGranter.grant(Badge.find(Badge::FirstEmoji), @post.user) if has_emoji? + BadgeGranter.grant(Badge.find(Badge::FirstOnebox), @post.user) if @has_oneboxes end def keep_reverse_index_up_to_date @@ -286,6 +288,7 @@ class CookedPostProcessor # apply oneboxes Oneboxer.apply(@doc, topic_id: @post.topic_id) { |url| + @has_oneboxes = true Oneboxer.onebox(url, args) } diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index a2152e2d8a6..8430d9ff98f 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -96,7 +96,6 @@ module Oneboxer changed = false Oneboxer.each_onebox_link(doc) do |url, element| - if args && args[:topic_id] url = append_source_topic_id(url, args[:topic_id]) end diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 1e1c4963b1f..ad714619d7c 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -488,6 +488,7 @@ describe CookedPostProcessor do end context "grant badges" do + context "emoji inside a quote" do let(:post) { Fabricate(:post, raw: "time to eat some sweet [quote]:candy:[/quote] mmmm") } let(:cpp) { CookedPostProcessor.new(post) } @@ -507,6 +508,23 @@ describe CookedPostProcessor do expect(post.user.user_badges.where(badge_id: Badge::FirstEmoji).exists?).to eq(true) end end + + context "onebox" do + let(:user) { Fabricate(:user) } + let(:post) { Fabricate.build(:post, user: user, raw: "onebox me:\n\nhttps://www.youtube.com/watch?v=Wji-BZ0oCwg\n") } + let(:cpp) { CookedPostProcessor.new(post) } + + before do + Oneboxer.stubs(:onebox) + end + + it "awards a badge for using an emoji" do + cpp.post_process_oneboxes + cpp.grant_badges + expect(post.user.user_badges.where(badge_id: Badge::FirstOnebox).exists?).to eq(true) + end + end + end end diff --git a/spec/models/post_analyzer_spec.rb b/spec/models/post_analyzer_spec.rb index 3940fa73d82..6bfe2f9cb82 100644 --- a/spec/models/post_analyzer_spec.rb +++ b/spec/models/post_analyzer_spec.rb @@ -17,6 +17,7 @@ describe PostAnalyzer do it 'fetches the cached onebox for any urls in the post' do Oneboxer.expects(:cached_onebox).with url post_analyzer.cook(*args) + expect(post_analyzer.found_oneboxes?).to be(true) end it 'does not invalidate the onebox cache' do @@ -153,6 +154,8 @@ describe PostAnalyzer do it "finds links from HTML" do post_analyzer = PostAnalyzer.new(raw_post_two_links_html, default_topic_id) + post_analyzer.cook(raw_post_two_links_html, {}) + expect(post_analyzer.found_oneboxes?).to be(false) expect(post_analyzer.link_count).to eq(2) end end diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb index d6889f7493b..6c2cc79b9d5 100644 --- a/spec/services/post_alerter_spec.rb +++ b/spec/services/post_alerter_spec.rb @@ -221,7 +221,7 @@ describe PostAlerter do create_post(topic_id: topic.id, user: user, raw: "my magic topic\n##{Discourse.base_url}#{post1.url}") user.reload - expect(user.notifications.count).to eq(1) + expect(user.notifications.where(notification_type: Notification.types[:linked]).count).to eq(1) expect(watcher.notifications.count).to eq(1)