2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-02 12:01:53 +08:00
|
|
|
class Jobs::NotifyReviewable < ::Jobs::Base
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
def execute(args)
|
2020-08-08 00:13:02 +08:00
|
|
|
return unless reviewable = Reviewable.find_by(id: args[:reviewable_id])
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
@contacted = Set.new
|
|
|
|
|
2020-08-08 00:13:02 +08:00
|
|
|
counts = Hash.new(0)
|
2019-01-04 01:03:01 +08:00
|
|
|
|
2020-08-08 00:13:02 +08:00
|
|
|
Reviewable.default_visible.pending.each do |r|
|
|
|
|
counts[:admins] += 1
|
|
|
|
counts[:moderators] += 1 if r.reviewable_by_moderator?
|
|
|
|
counts[r.reviewable_by_group_id] += 1 if r.reviewable_by_group_id
|
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
|
2020-08-08 00:13:02 +08:00
|
|
|
# admins
|
|
|
|
notify(counts[:admins], User.real.admins.pluck(:id))
|
2019-01-04 01:03:01 +08:00
|
|
|
|
2020-08-08 00:13:02 +08:00
|
|
|
# moderators
|
|
|
|
if reviewable.reviewable_by_moderator?
|
|
|
|
notify(counts[:moderators], User.real.moderators.where("id NOT IN (?)", @contacted).pluck(:id))
|
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
|
2020-08-08 00:13:02 +08:00
|
|
|
# category moderators
|
|
|
|
if SiteSetting.enable_category_group_moderation? && (group = reviewable.reviewable_by_group)
|
|
|
|
group.users.includes(:group_users).where("users.id NOT IN (?)", @contacted).find_each do |user|
|
|
|
|
count = user.group_users.map { |gu| counts[gu.group_id] }.sum
|
|
|
|
notify(count, [user.id])
|
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-08 00:13:02 +08:00
|
|
|
protected
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
def notify(count, user_ids)
|
2020-04-28 01:51:25 +08:00
|
|
|
return if user_ids.blank?
|
2019-01-04 01:03:01 +08:00
|
|
|
data = { reviewable_count: count }
|
|
|
|
MessageBus.publish("/reviewable_counts", data, user_ids: user_ids)
|
|
|
|
@contacted += user_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|