2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-28 12:20:05 -04:00
|
|
|
module Jobs
|
|
|
|
class GrantAnniversaryBadges < Jobs::Scheduled
|
|
|
|
every 1.day
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
return unless SiteSetting.enable_badges?
|
2017-06-25 21:13:58 -04:00
|
|
|
badge = Badge.find_by(id: Badge::Anniversary, enabled: true)
|
|
|
|
return unless badge
|
2017-04-28 12:20:05 -04:00
|
|
|
|
|
|
|
start_date = args[:start_date] || 1.year.ago
|
|
|
|
end_date = start_date + 1.year
|
|
|
|
|
|
|
|
fmt_end_date = end_date.iso8601(6)
|
|
|
|
fmt_start_date = start_date.iso8601(6)
|
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
user_ids = DB.query_single <<~SQL
|
2017-04-28 12:20:05 -04:00
|
|
|
SELECT u.id AS user_id
|
|
|
|
FROM users AS u
|
|
|
|
INNER JOIN posts AS p ON p.user_id = u.id
|
|
|
|
INNER JOIN topics AS t ON p.topic_id = t.id
|
|
|
|
LEFT OUTER JOIN user_badges AS ub ON ub.user_id = u.id AND
|
|
|
|
ub.badge_id = #{Badge::Anniversary} AND
|
|
|
|
ub.granted_at BETWEEN '#{fmt_start_date}' AND '#{fmt_end_date}'
|
|
|
|
WHERE u.active AND
|
2017-11-13 13:41:36 -05:00
|
|
|
u.silenced_till IS NULL AND
|
2017-04-28 12:20:05 -04:00
|
|
|
NOT p.hidden AND
|
|
|
|
p.deleted_at IS NULL AND
|
|
|
|
t.visible AND
|
|
|
|
t.archetype <> 'private_message' AND
|
|
|
|
p.created_at BETWEEN '#{fmt_start_date}' AND '#{fmt_end_date}' AND
|
|
|
|
u.created_at <= '#{fmt_start_date}'
|
|
|
|
GROUP BY u.id
|
|
|
|
HAVING COUNT(p.id) > 0 AND COUNT(ub.id) = 0
|
|
|
|
SQL
|
|
|
|
|
2017-06-23 03:53:07 -04:00
|
|
|
User.where(id: user_ids).find_each do |user|
|
2017-04-28 12:20:05 -04:00
|
|
|
BadgeGranter.grant(badge, user, created_at: end_date)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|