FIX: FirstReplyByEmail badge wasn't granted

DEPRECATED: PostProcess badge trigger
This commit is contained in:
Régis Hanol 2016-08-10 19:24:01 +02:00
parent d763ce08c0
commit e55e2aff94
9 changed files with 41 additions and 37 deletions

View File

@ -1,6 +1,7 @@
module Jobs
class GrantFirstReplyByEmail < Jobs::Onceoff
def execute_onceoff(args)
return unless SiteSetting.enable_badges
to_award = {}

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -542,6 +542,7 @@ describe CookedPostProcessor do
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
@ -550,10 +551,10 @@ describe CookedPostProcessor do
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