FIX: ensure triggered badges are never triggered if filter is missing

This commit is contained in:
Sam 2014-08-27 18:02:13 +10:00
parent f48b78c0bb
commit 87d2be3ecf
2 changed files with 20 additions and 7 deletions

View File

@ -36,7 +36,7 @@ class Badge < ActiveRecord::Base
module Queries
Reader = <<SQL
SELECT id user_id, current_timestamp granted_at, NULL post_id
SELECT id user_id, current_timestamp granted_at
FROM users
WHERE id IN
(
@ -53,9 +53,9 @@ class Badge < ActiveRecord::Base
SQL
ReadGuidelines = <<SQL
SELECT user_id, read_faq granted_at, NULL post_id
SELECT user_id, read_faq granted_at
FROM user_stats
WHERE read_faq IS NOT NULL
WHERE read_faq IS NOT NULL AND (user_id IN (:user_ids) OR :backfill)
SQL
FirstQuote = <<SQL
@ -145,7 +145,7 @@ SQL
SQL
Autobiographer = <<SQL
SELECT u.id user_id, current_timestamp granted_at, NULL post_id
SELECT u.id user_id, current_timestamp granted_at
FROM users u
JOIN user_profiles up on u.id = up.user_id
WHERE bio_raw IS NOT NULL AND LENGTH(TRIM(bio_raw)) > #{Badge::AutobiographerMinBioLength} AND
@ -166,7 +166,7 @@ SQL
def self.trust_level(level)
# we can do better with dates, but its hard work figuring this out historically
"
SELECT u.id user_id, current_timestamp granted_at, NULL post_id FROM users u
SELECT u.id user_id, current_timestamp granted_at FROM users u
WHERE trust_level >= #{level.to_i} AND (
:backfill OR u.id IN (:user_ids)
)

View File

@ -214,8 +214,21 @@ class BadgeGranter
builder = SqlBuilder.new(sql)
builder.where("ub.badge_id IS NULL AND q.user_id <> -1")
builder.where("q.post_id in (:post_ids)") if post_ids
builder.where("q.user_id in (:user_ids)") if user_ids
if (post_ids || user_ids) && !badge.query.include?(":backfill")
Rails.logger.warn "Your triggered badge query for #{badge.name} does not include the :backfill param, skipping!"
return
end
if (post_ids && !badge.query.include?(":post_ids"))
Rails.logger.warn "Your triggered badge query for #{badge.name} does not include the :post_ids param, skipping!"
return
end
if (user_ids && !badge.query.include?(":user_ids"))
Rails.logger.warn "Your triggered badge query for #{badge.name} does not include the :user_ids param, skipping!"
return
end
builder.map_exec(OpenStruct, id: badge.id,
multiple_grant: badge.multiple_grant,