2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-02 00:01:53 -04:00
|
|
|
class Jobs::NotifyReviewable < ::Jobs::Base
|
2022-08-03 01:57:59 -04:00
|
|
|
# remove all the legacy stuff here when redesigned_user_menu_enabled is
|
|
|
|
# removed
|
2019-01-03 12:03:01 -05:00
|
|
|
def execute(args)
|
2020-08-07 12:13:02 -04:00
|
|
|
return unless reviewable = Reviewable.find_by(id: args[:reviewable_id])
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
@contacted = Set.new
|
|
|
|
|
2021-05-25 19:47:35 -04: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-16 12:57:12 -04:00
|
|
|
payload = {
|
|
|
|
last_performing_username: args[:performing_username],
|
|
|
|
status: r.status
|
|
|
|
}
|
2021-05-25 19:47:35 -04:00
|
|
|
|
|
|
|
all_updates[:admins][r.id] = payload
|
|
|
|
all_updates[:moderators][r.id] = payload if r.reviewable_by_moderator?
|
|
|
|
all_updates[r.reviewable_by_group_id][r.id] = payload if r.reviewable_by_group_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-03 01:57:59 -04:00
|
|
|
counts = Hash.new(0)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-08-31 14:15:01 -04:00
|
|
|
if SiteSetting.enable_experimental_sidebar_hamburger
|
|
|
|
notify_users(
|
|
|
|
User.real.admins,
|
|
|
|
all_updates[:admins]
|
|
|
|
)
|
|
|
|
else
|
2022-08-03 01:57:59 -04:00
|
|
|
notify_legacy(
|
2022-08-31 14:15:01 -04:00
|
|
|
User.real.admins.pluck(:id),
|
|
|
|
count: counts[:admins],
|
|
|
|
updates: all_updates[:admins],
|
2021-05-25 19:47:35 -04:00
|
|
|
)
|
2020-08-07 12:13:02 -04:00
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2022-08-31 14:15:01 -04:00
|
|
|
if reviewable.reviewable_by_moderator?
|
|
|
|
if SiteSetting.enable_experimental_sidebar_hamburger
|
|
|
|
notify_users(
|
|
|
|
User.real.moderators.where("id NOT IN (?)", @contacted),
|
|
|
|
all_updates[:moderators]
|
|
|
|
)
|
|
|
|
else
|
|
|
|
notify_legacy(
|
|
|
|
User.real.moderators.where("id NOT IN (?)", @contacted).pluck(:id),
|
|
|
|
count: counts[:moderators],
|
|
|
|
updates: all_updates[:moderators],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-07 12:13:02 -04:00
|
|
|
if SiteSetting.enable_category_group_moderation? && (group = reviewable.reviewable_by_group)
|
2022-08-03 01:57:59 -04:00
|
|
|
users = group.users.includes(:group_users).where("users.id NOT IN (?)", @contacted)
|
2022-08-31 14:15:01 -04:00
|
|
|
|
2022-08-03 01:57:59 -04:00
|
|
|
users.find_each do |user|
|
2021-05-25 19:47:35 -04:00
|
|
|
count = 0
|
|
|
|
updates = {}
|
|
|
|
user.group_users.each do |gu|
|
2022-08-03 01:57:59 -04:00
|
|
|
updates.merge!(all_updates[gu.group_id])
|
|
|
|
count += counts[gu.group_id]
|
|
|
|
end
|
2022-08-31 14:15:01 -04:00
|
|
|
|
|
|
|
if SiteSetting.enable_experimental_sidebar_hamburger
|
2022-08-03 01:57:59 -04:00
|
|
|
notify_user(user, updates)
|
|
|
|
else
|
|
|
|
notify_legacy([user.id], count: count, updates: updates)
|
2021-05-25 19:47:35 -04:00
|
|
|
end
|
2020-08-07 12:13:02 -04:00
|
|
|
end
|
2022-08-31 14:15:01 -04:00
|
|
|
|
2022-08-03 01:57:59 -04:00
|
|
|
@contacted += users.pluck(:id)
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-07 12:13:02 -04:00
|
|
|
protected
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2022-08-03 01:57:59 -04:00
|
|
|
def notify_legacy(user_ids, count:, updates:)
|
2020-04-27 13:51:25 -04:00
|
|
|
return if user_ids.blank?
|
2021-05-25 19:47:35 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
data = { reviewable_count: count }
|
2021-05-25 19:47:35 -04:00
|
|
|
data[:updates] = updates if updates.present?
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
MessageBus.publish("/reviewable_counts", data, user_ids: user_ids)
|
|
|
|
@contacted += user_ids
|
|
|
|
end
|
|
|
|
|
2022-08-03 01:57:59 -04:00
|
|
|
def notify_users(users, updates)
|
|
|
|
users.find_each { |user| notify_user(user, updates) }
|
|
|
|
@contacted += users.pluck(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_user(user, updates)
|
|
|
|
data = {
|
|
|
|
reviewable_count: user.reviewable_count,
|
|
|
|
unseen_reviewable_count: user.unseen_reviewable_count
|
|
|
|
}
|
|
|
|
data[:updates] = updates if updates.present?
|
|
|
|
|
|
|
|
user.publish_reviewable_counts(data)
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|