diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index 22ff864f84d..2bc5524cf48 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -3,8 +3,7 @@ require_dependency 'user_action_creator' class PostAlerter def self.post_created(post, opts = {}) - alerter = PostAlerter.new(opts) - alerter.after_save_post(post, true) + PostAlerter.new(opts).after_save_post(post, true) post end @@ -90,30 +89,8 @@ class PostAlerter # private messages if new_record if post.topic.private_message? - # users that aren't part of any mentioned groups - users = directly_targeted_users(post).reject { |u| notified.include?(u) } - DiscourseEvent.trigger(:before_create_notifications_for_users, users, post) - users.each do |user| - notification_level = TopicUser.get(post.topic, user).try(:notification_level) - if reply_to_user == user || notification_level == TopicUser.notification_levels[:watching] || user.staged? - create_notification(user, Notification.types[:private_message], post) - end - end - # users that are part of all mentionned groups - users = indirectly_targeted_users(post).reject { |u| notified.include?(u) } - DiscourseEvent.trigger(:before_create_notifications_for_users, users, post) - users.each do |user| - # only create a notification when watching the group - notification_level = TopicUser.get(post.topic, user).try(:notification_level) - - if notification_level == TopicUser.notification_levels[:watching] - create_notification(user, Notification.types[:private_message], post) - elsif notification_level == TopicUser.notification_levels[:tracking] - notify_group_summary(user, post) - end - end + notify_pm_users(post, reply_to_user, notified) elsif post.post_type == Post.types[:regular] - # If it's not a private message and it's not an automatic post caused by a moderator action, notify the users notify_post_users(post, notified) end end @@ -153,10 +130,12 @@ class PostAlerter # Don't notify the OP user_ids -= [post.user_id] users = User.where(id: user_ids) - DiscourseEvent.trigger(:before_create_notifications_for_users, users, post) - users.each do |user| - create_notification(user, Notification.types[:watching_first_post], post) + Scheduler::Defer.later("Notify First Post Watchers") do + DiscourseEvent.trigger(:before_create_notifications_for_users, users, post) + users.each do |user| + create_notification(user, Notification.types[:watching_first_post], post) + end end end @@ -222,7 +201,6 @@ class PostAlerter end def notify_group_summary(user, post) - @group_stats ||= {} stats = (@group_stats[post.topic_id] ||= group_stats(post.topic)) return unless stats @@ -262,12 +240,9 @@ class PostAlerter end def should_notify_like?(user, notification) - return true if user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:always] - return true if user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:first_time_and_daily] && notification.created_at < 1.day.ago - - return false + false end def should_notify_previous?(user, notification, opts) @@ -504,59 +479,101 @@ class PostAlerter users = [users] unless users.is_a?(Array) users = users.reject { |u| u.staged? } if post.topic&.private_message? - DiscourseEvent.trigger(:before_create_notifications_for_users, users, post) - users.each do |u| - create_notification(u, Notification.types[type], post, opts) + Scheduler::Defer.later("Notify Users") do + DiscourseEvent.trigger(:before_create_notifications_for_users, users, post) + users.each do |u| + create_notification(u, Notification.types[type], post, opts) + end end users end + def notify_pm_users(post, reply_to_user, notified) + return unless post.topic + + Scheduler::Defer.later("Notify PM Users") do + # users that aren't part of any mentioned groups + users = directly_targeted_users(post).reject { |u| notified.include?(u) } + DiscourseEvent.trigger(:before_create_notifications_for_users, users, post) + users.each do |user| + notification_level = TopicUser.get(post.topic, user)&.notification_level + if reply_to_user == user || notification_level == TopicUser.notification_levels[:watching] || user.staged? + create_notification(user, Notification.types[:private_message], post) + end + end + + # users that are part of all mentionned groups + users = indirectly_targeted_users(post).reject { |u| notified.include?(u) } + DiscourseEvent.trigger(:before_create_notifications_for_users, users, post) + users.each do |user| + case TopicUser.get(post.topic, user)&.notification_level + when TopicUser.notification_levels[:watching] + # only create a notification when watching the group + create_notification(user, Notification.types[:private_message], post) + when TopicUser.notification_levels[:tracking] + notify_group_summary(user, post) + end + end + end + end + def notify_post_users(post, notified) return unless post.topic - condition = <