2014-03-05 07:52:20 -05:00
|
|
|
class BadgeGranter
|
|
|
|
|
|
|
|
def initialize(badge, user, opts={})
|
|
|
|
@badge, @user, @opts = badge, user, opts
|
|
|
|
@granted_by = opts[:granted_by] || Discourse.system_user
|
2014-06-17 02:29:49 -04:00
|
|
|
@post_id = opts[:post_id]
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.grant(badge, user, opts={})
|
|
|
|
BadgeGranter.new(badge, user, opts).grant
|
|
|
|
end
|
|
|
|
|
|
|
|
def grant
|
|
|
|
return if @granted_by and !Guardian.new(@granted_by).can_grant_badges?(@user)
|
|
|
|
|
2014-07-04 03:40:44 -04:00
|
|
|
find_by = { badge_id: @badge.id, user_id: @user.id }
|
|
|
|
|
|
|
|
if @badge.multiple_grant?
|
|
|
|
find_by[:post_id] = @post_id
|
|
|
|
end
|
|
|
|
|
|
|
|
user_badge = UserBadge.find_by(find_by)
|
2014-03-05 07:52:20 -05:00
|
|
|
|
2014-06-27 15:02:09 -04:00
|
|
|
if user_badge.nil? || (@badge.multiple_grant? && @post_id.nil?)
|
2014-04-14 01:58:27 -04:00
|
|
|
UserBadge.transaction do
|
|
|
|
user_badge = UserBadge.create!(badge: @badge, user: @user,
|
2014-06-17 02:29:49 -04:00
|
|
|
granted_by: @granted_by,
|
|
|
|
granted_at: Time.now,
|
|
|
|
post_id: @post_id)
|
2014-03-05 07:52:20 -05:00
|
|
|
|
2014-04-14 01:58:27 -04:00
|
|
|
if @granted_by != Discourse.system_user
|
|
|
|
StaffActionLogger.new(@granted_by).log_badge_grant(user_badge)
|
|
|
|
end
|
2014-04-16 15:59:45 -04:00
|
|
|
|
2014-05-04 14:15:38 -04:00
|
|
|
if SiteSetting.enable_badges?
|
2014-06-17 01:59:28 -04:00
|
|
|
notification = @user.notifications.create(notification_type: Notification.types[:granted_badge], data: { badge_id: @badge.id, badge_name: @badge.name }.to_json)
|
|
|
|
user_badge.update_attributes notification_id: notification.id
|
2014-05-04 14:15:38 -04:00
|
|
|
end
|
2014-03-19 15:30:12 -04:00
|
|
|
end
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
user_badge
|
|
|
|
end
|
|
|
|
|
2014-03-19 15:30:12 -04:00
|
|
|
def self.revoke(user_badge, options={})
|
2014-03-05 07:52:20 -05:00
|
|
|
UserBadge.transaction do
|
|
|
|
user_badge.destroy!
|
2014-03-19 15:30:12 -04:00
|
|
|
if options[:revoked_by]
|
|
|
|
StaffActionLogger.new(options[:revoked_by]).log_badge_revoke(user_badge)
|
|
|
|
end
|
2014-04-17 23:10:53 -04:00
|
|
|
|
|
|
|
# If the user's title is the same as the badge name, remove their title.
|
|
|
|
if user_badge.user.title == user_badge.badge.name
|
|
|
|
user_badge.user.title = nil
|
|
|
|
user_badge.user.save!
|
|
|
|
end
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-17 03:07:46 -04:00
|
|
|
def self.update_badges(args)
|
|
|
|
Jobs.enqueue(:update_badges, args)
|
2014-05-04 14:15:38 -04:00
|
|
|
end
|
|
|
|
|
2014-07-03 03:29:44 -04:00
|
|
|
def self.backfill(badge)
|
2014-07-14 03:40:01 -04:00
|
|
|
return unless badge.query.present? && badge.enabled
|
2014-07-03 03:29:44 -04:00
|
|
|
|
|
|
|
post_clause = badge.target_posts ? "AND q.post_id = ub.post_id" : ""
|
|
|
|
post_id_field = badge.target_posts ? "q.post_id" : "NULL"
|
|
|
|
|
|
|
|
sql = "DELETE FROM user_badges
|
|
|
|
WHERE id in (
|
|
|
|
SELECT ub.id
|
|
|
|
FROM user_badges ub
|
|
|
|
LEFT JOIN ( #{badge.query} ) q
|
|
|
|
ON q.user_id = ub.user_id
|
|
|
|
#{post_clause}
|
2014-07-05 04:32:06 -04:00
|
|
|
WHERE ub.badge_id = :id AND q.user_id IS NULL
|
2014-07-03 03:29:44 -04:00
|
|
|
)"
|
|
|
|
|
|
|
|
Badge.exec_sql(sql, id: badge.id)
|
|
|
|
|
|
|
|
sql = "INSERT INTO user_badges(badge_id, user_id, granted_at, granted_by_id, post_id)
|
|
|
|
SELECT :id, q.user_id, q.granted_at, -1, #{post_id_field}
|
|
|
|
FROM ( #{badge.query} ) q
|
|
|
|
LEFT JOIN user_badges ub ON
|
2014-07-05 04:32:06 -04:00
|
|
|
ub.badge_id = :id AND ub.user_id = q.user_id
|
2014-07-03 03:29:44 -04:00
|
|
|
#{post_clause}
|
2014-07-07 03:55:25 -04:00
|
|
|
WHERE ub.badge_id IS NULL AND q.user_id <> -1
|
|
|
|
RETURNING id, user_id, granted_at
|
|
|
|
"
|
2014-07-03 03:29:44 -04:00
|
|
|
|
2014-07-07 03:55:25 -04:00
|
|
|
builder = SqlBuilder.new(sql)
|
|
|
|
builder.map_exec(OpenStruct, id: badge.id).each do |row|
|
|
|
|
|
|
|
|
# old bronze badges do not matter
|
|
|
|
next if badge.badge_type_id == BadgeType::Bronze and row.granted_at < 2.days.ago
|
|
|
|
|
|
|
|
notification = Notification.create!(
|
|
|
|
user_id: row.user_id,
|
|
|
|
notification_type: Notification.types[:granted_badge],
|
|
|
|
data: { badge_id: badge.id, badge_name: badge.name }.to_json )
|
|
|
|
|
|
|
|
Badge.exec_sql("UPDATE user_badges SET notification_id = :notification_id WHERE id = :id",
|
|
|
|
notification_id: notification.id,
|
|
|
|
id: row.id
|
|
|
|
)
|
|
|
|
end
|
2014-07-03 03:29:44 -04:00
|
|
|
|
|
|
|
badge.reset_grant_count!
|
2014-07-01 08:00:31 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|