discourse/app/jobs/regular/group_pm_alert.rb
Dan Ungureanu 99dadb2129
PERF: Async notify users after inviting group (#12697)
Inviting a group generates a notification for each member. If this
happens synchronously it may take a while, leading to a poor user
experience.
2021-04-14 19:30:51 +03:00

43 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module Jobs
class GroupPmAlert < ::Jobs::Base
def execute(args)
return unless user = User.find_by(id: args[:user_id])
return unless group = Group.find_by(id: args[:group_id])
return unless post = Post.find_by(id: args[:post_id])
return unless topic = post.topic
group.set_message_default_notification_levels!(topic, ignore_existing: true)
alerter = PostAlerter.new
group.users.where(
"group_users.notification_level = :level",
level: NotificationLevels.all[:tracking]
).find_each do |u|
alerter.notify_group_summary(u, post)
end
notification_data = {
notification_type: Notification.types[:invited_to_private_message],
topic_id: topic.id,
post_number: 1,
data: {
topic_title: topic.title,
display_username: user.username,
group_id: group.id
}.to_json
}
group.users.where(
"group_users.notification_level in (:levels) AND user_id != :id",
levels: [NotificationLevels.all[:watching], NotificationLevels.all[:watching_first_post]],
id: user.id
).find_each do |u|
u.notifications.create!(notification_data)
end
end
end
end