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
|
2024-03-23 05:37:06 +08:00
|
|
|
# this job can take a very long time if there are many mods
|
|
|
|
# do not swamp the queue with it
|
|
|
|
cluster_concurrency 1
|
|
|
|
|
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
|
|
|
|
|
2021-05-26 07:47:35 +08:00
|
|
|
all_updates = Hash.new { |h, k| h[k] = {} }
|
|
|
|
|
|
|
|
if args[:updated_reviewable_ids].present?
|
|
|
|
Reviewable
|
|
|
|
.where(id: args[:updated_reviewable_ids])
|
|
|
|
.each do |r|
|
2021-07-17 00:57:12 +08:00
|
|
|
payload = { last_performing_username: args[:performing_username], status: r.status }
|
2023-01-09 20:20:10 +08:00
|
|
|
|
2021-05-26 07:47:35 +08:00
|
|
|
all_updates[:admins][r.id] = payload
|
|
|
|
all_updates[:moderators][r.id] = payload if r.reviewable_by_moderator?
|
2024-09-04 09:38:46 +08:00
|
|
|
|
|
|
|
if SiteSetting.enable_category_group_moderation? && r.category.present?
|
|
|
|
r
|
|
|
|
.category
|
|
|
|
.moderating_groups
|
|
|
|
.pluck(:id)
|
|
|
|
.each { |group_id| all_updates[group_id][r.id] = payload }
|
|
|
|
end
|
2021-05-26 07:47:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-01 05:58:32 +08:00
|
|
|
DistributedMutex.synchronize("notify_reviewable_job", validity: 120) do
|
2023-05-18 00:16:42 +08:00
|
|
|
notify_users(User.real.admins, all_updates[:admins])
|
2022-09-01 02:15:01 +08:00
|
|
|
|
2023-01-26 04:19:11 +08:00
|
|
|
if reviewable.reviewable_by_moderator?
|
2023-05-18 00:16:42 +08:00
|
|
|
notify_users(
|
|
|
|
User.real.moderators.where("id NOT IN (?)", @contacted),
|
|
|
|
all_updates[:moderators],
|
|
|
|
)
|
2020-08-08 00:13:02 +08:00
|
|
|
end
|
2022-09-01 02:15:01 +08:00
|
|
|
|
2024-09-04 09:38:46 +08:00
|
|
|
if SiteSetting.enable_category_group_moderation? && reviewable.category.present?
|
|
|
|
users =
|
|
|
|
User
|
|
|
|
.includes(:group_users)
|
|
|
|
.joins(:group_users)
|
|
|
|
.joins(
|
|
|
|
"INNER JOIN category_moderation_groups ON category_moderation_groups.group_id = group_users.group_id",
|
|
|
|
)
|
|
|
|
.where("category_moderation_groups.category_id": reviewable.category.id)
|
|
|
|
.where("users.id NOT IN (?)", @contacted)
|
|
|
|
.distinct
|
2023-01-26 04:19:11 +08:00
|
|
|
|
|
|
|
users.find_each do |user|
|
|
|
|
updates = {}
|
2024-09-04 09:38:46 +08:00
|
|
|
user.group_users.each { |gu| updates.merge!(all_updates[gu.group_id]) }
|
2023-01-26 04:19:11 +08:00
|
|
|
|
2023-05-18 00:16:42 +08:00
|
|
|
notify_user(user, updates)
|
2023-01-26 04:19:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@contacted += users.pluck(: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
|
|
|
|
2022-08-03 13:57:59 +08:00
|
|
|
def notify_users(users, updates)
|
|
|
|
users.find_each { |user| notify_user(user, updates) }
|
|
|
|
@contacted += users.pluck(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_user(user, updates)
|
2022-12-01 07:09:57 +08:00
|
|
|
user.publish_reviewable_counts(updates.present? ? { updates: updates } : nil)
|
2022-08-03 13:57:59 +08:00
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|