From e55e2aff94959aecc28095875f5de351de26162f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 10 Aug 2016 19:24:01 +0200 Subject: [PATCH] FIX: FirstReplyByEmail badge wasn't granted DEPRECATED: PostProcess badge trigger --- .../onceoff/grand_first_reply_by_email.rb | 1 + app/jobs/onceoff/grant_emoji.rb | 2 +- app/jobs/onceoff/grant_onebox.rb | 3 +- app/models/badge.rb | 4 +-- app/models/post.rb | 4 +++ app/services/badge_granter.rb | 24 +++++--------- db/fixtures/006_badges.rb | 6 ++-- lib/cooked_post_processor.rb | 1 + spec/components/cooked_post_processor_spec.rb | 33 +++++++++++-------- 9 files changed, 41 insertions(+), 37 deletions(-) diff --git a/app/jobs/onceoff/grand_first_reply_by_email.rb b/app/jobs/onceoff/grand_first_reply_by_email.rb index 1d97091eb6b..a7d98cdfdbb 100644 --- a/app/jobs/onceoff/grand_first_reply_by_email.rb +++ b/app/jobs/onceoff/grand_first_reply_by_email.rb @@ -1,6 +1,7 @@ module Jobs class GrantFirstReplyByEmail < Jobs::Onceoff + def execute_onceoff(args) return unless SiteSetting.enable_badges to_award = {} diff --git a/app/jobs/onceoff/grant_emoji.rb b/app/jobs/onceoff/grant_emoji.rb index a5feabef16f..e99cdbb2e78 100644 --- a/app/jobs/onceoff/grant_emoji.rb +++ b/app/jobs/onceoff/grant_emoji.rb @@ -9,7 +9,7 @@ module Jobs .select(:id, :created_at, :cooked, :user_id) .visible .public_posts - .where("cooked like '%emoji%'") + .where("cooked LIKE '%emoji%'") .find_in_batches do |group| group.each do |p| doc = Nokogiri::HTML::fragment(p.cooked) diff --git a/app/jobs/onceoff/grant_onebox.rb b/app/jobs/onceoff/grant_onebox.rb index 9ce0f6fd00f..6e4a92d69f4 100644 --- a/app/jobs/onceoff/grant_onebox.rb +++ b/app/jobs/onceoff/grant_onebox.rb @@ -11,9 +11,8 @@ module Jobs .select(:id, :created_at, :raw, :user_id) .visible .public_posts - .where("raw like '%http%'") + .where("raw LIKE '%http%'") .find_in_batches do |group| - group.each do |p| begin # Note we can't use `p.cooked` here because oneboxes have been cooked out diff --git a/app/models/badge.rb b/app/models/badge.rb index 4a2105a1091..9c743f14dd9 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -66,14 +66,14 @@ class Badge < ActiveRecord::Base PostRevision = 2 TrustLevelChange = 4 UserChange = 8 - PostProcessed = 16 + PostProcessed = 16 # deprecated def self.is_none?(trigger) [None].include? trigger end def self.uses_user_ids?(trigger) - [TrustLevelChange, UserChange, PostProcessed].include? trigger + [TrustLevelChange, UserChange].include? trigger end def self.uses_post_ids?(trigger) diff --git a/app/models/post.rb b/app/models/post.rb index 4a513e11e3c..2273f358a01 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -362,6 +362,10 @@ class Post < ActiveRecord::Base post_number == 1 end + def is_reply_by_email? + via_email && post_number.present? && post_number > 1 + end + def is_flagged? post_actions.where(post_action_type_id: PostActionType.flag_types.values, deleted_at: nil).count != 0 end diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rb index 2b462569d14..56186e696b3 100644 --- a/app/services/badge_granter.rb +++ b/app/services/badge_granter.rb @@ -76,7 +76,7 @@ class BadgeGranter end end - def self.queue_badge_grant(type,opt) + def self.queue_badge_grant(type, opt) return unless SiteSetting.enable_badges payload = nil @@ -105,12 +105,6 @@ class BadgeGranter type: "PostAction", post_ids: [action.post_id, action.related_post_id].compact! } - when Badge::Trigger::PostProcessed - user = opt[:user] - payload = { - type: "PostProcessed", - user_ids: [user.id] - } end $redis.lpush queue_key, payload.to_json if payload @@ -128,16 +122,17 @@ class BadgeGranter limit -= 1 end - items = items.group_by{|i| i["type"]} + items = items.group_by { |i| i["type"] } items.each do |type, list| - post_ids = list.map{|i| i["post_ids"]}.flatten.compact.uniq - user_ids = list.map{|i| i["user_ids"]}.flatten.compact.uniq + post_ids = list.flat_map { |i| i["post_ids"] }.compact.uniq + user_ids = list.flat_map { |i| i["user_ids"] }.compact.uniq next unless post_ids.present? || user_ids.present? - find_by_type(type).each{ |badge| + + find_by_type(type).each do |badge| backfill(badge, post_ids: post_ids, user_ids: user_ids) - } + end end end @@ -229,10 +224,10 @@ class BadgeGranter MAX_ITEMS_FOR_DELTA = 200 def self.backfill(badge, opts=nil) return unless SiteSetting.enable_badges - return unless badge.query.present? && badge.enabled + return unless badge.enabled + return unless badge.query.present? post_ids = user_ids = nil - post_ids = opts[:post_ids] if opts user_ids = opts[:user_ids] if opts @@ -331,7 +326,6 @@ class BadgeGranter raise ex end - def self.revoke_ungranted_titles! Badge.exec_sql("UPDATE users SET title = '' WHERE NOT title IS NULL AND diff --git a/db/fixtures/006_badges.rb b/db/fixtures/006_badges.rb index bf99aa53a43..f0d877e9cc7 100644 --- a/db/fixtures/006_badges.rb +++ b/db/fixtures/006_badges.rb @@ -382,7 +382,7 @@ Badge.seed do |b| b.query = nil b.badge_grouping_id = BadgeGrouping::GettingStarted b.default_badge_grouping_id = BadgeGrouping::GettingStarted - b.trigger = Badge::Trigger::PostProcessed + b.trigger = Badge::Trigger::None b.system = true end @@ -396,7 +396,7 @@ Badge.seed do |b| b.query = nil b.badge_grouping_id = BadgeGrouping::GettingStarted b.default_badge_grouping_id = BadgeGrouping::GettingStarted - b.trigger = Badge::Trigger::PostProcessed + b.trigger = Badge::Trigger::None b.system = true end @@ -410,7 +410,7 @@ Badge.seed do |b| b.query = nil b.badge_grouping_id = BadgeGrouping::GettingStarted b.default_badge_grouping_id = BadgeGrouping::GettingStarted - b.trigger = Badge::Trigger::PostProcessed + b.trigger = Badge::Trigger::None b.system = true end diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index f4249cecde9..7ae871b6359 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -51,6 +51,7 @@ class CookedPostProcessor BadgeGranter.grant(Badge.find(Badge::FirstEmoji), @post.user, post_id: @post.id) if has_emoji? BadgeGranter.grant(Badge.find(Badge::FirstOnebox), @post.user, post_id: @post.id) if @has_oneboxes + BadgeGranter.grant(Badge.find(Badge::FirstReplyByEmail), @post.user, post_id: @post.id) if @post.is_reply_by_email? end def keep_reverse_index_up_to_date diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 7d5a5f0b1d7..f31b69484b1 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -541,19 +541,20 @@ describe CookedPostProcessor do end context "extracts links" do - let(:post) { Fabricate(:post, raw: "sam has a blog at https://samsaffron.com") } - it "always re-extracts links on post process" do - TopicLink.destroy_all - CookedPostProcessor.new(post).post_process - expect(TopicLink.count).to eq(1) - end + let(:post) { Fabricate(:post, raw: "sam has a blog at https://samsaffron.com") } + + it "always re-extracts links on post process" do + TopicLink.destroy_all + CookedPostProcessor.new(post).post_process + expect(TopicLink.count).to eq(1) + end end context "grant badges" do + let(:cpp) { CookedPostProcessor.new(post) } 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) } it "doesn't award a badge when the emoji is in a quote" do cpp.grant_badges @@ -563,7 +564,6 @@ describe CookedPostProcessor do context "emoji in the text" do let(:post) { Fabricate(:post, raw: "time to eat some sweet :candy: mmmm") } - let(:cpp) { CookedPostProcessor.new(post) } it "awards a badge for using an emoji" do cpp.grant_badges @@ -572,13 +572,9 @@ describe CookedPostProcessor do 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) } + let(:post) { Fabricate(:post, raw: "onebox me:\n\nhttps://www.youtube.com/watch?v=Wji-BZ0oCwg\n") } - before do - Oneboxer.stubs(:onebox) - end + before { Oneboxer.stubs(:onebox) } it "awards a badge for using an emoji" do cpp.post_process_oneboxes @@ -587,6 +583,15 @@ describe CookedPostProcessor do end end + context "reply_by_email" do + let(:post) { Fabricate(:post, raw: "This is a **reply** via email ;)", via_email: true, post_number: 2) } + + it "awards a badge for replying via email" do + cpp.grant_badges + expect(post.user.user_badges.where(badge_id: Badge::FirstReplyByEmail).exists?).to eq(true) + end + end + end end