2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-29 00:20:05 +08:00
|
|
|
module Jobs
|
2019-10-02 12:01:53 +08:00
|
|
|
class GrantAnniversaryBadges < ::Jobs::Scheduled
|
2017-04-29 00:20:05 +08:00
|
|
|
every 1.day
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
return unless SiteSetting.enable_badges?
|
2017-06-26 09:13:58 +08:00
|
|
|
badge = Badge.find_by(id: Badge::Anniversary, enabled: true)
|
|
|
|
return unless badge
|
2017-04-29 00:20:05 +08: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 14:13:14 +08:00
|
|
|
user_ids = DB.query_single <<~SQL
|
2017-04-29 00:20:05 +08: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-14 02:41:36 +08:00
|
|
|
u.silenced_till IS NULL AND
|
2017-04-29 00:20:05 +08: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 15:53:07 +08:00
|
|
|
User
|
|
|
|
.where(id: user_ids)
|
2017-04-29 00:20:05 +08:00
|
|
|
.find_each { |user| BadgeGranter.grant(badge, user, created_at: end_date) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|