diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index a66a7ea024f..2a1bd9cd197 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -17,9 +17,18 @@ class PostAlerter def after_save_post(post, new_record = false) notified = [post.user].compact - if new_record && post.topic.private_message? && (post.is_first_post? || post.post_type == Post.types[:moderator_action]) + if new_record && post.topic.private_message? # If it's a private message, notify the topic_allowed_users allowed_users(post).each do |user| + case TopicUser.get(post.topic, user).try(:notification_level) + when TopicUser.notification_levels[:tracking] + next unless post.reply_to_post_number || post.reply_to_post.try(:user_id) == user.id + when TopicUser.notification_levels[:regular] + next unless post.reply_to_post.try(:user_id) == user.id + when TopicUser.notification_levels[:muted] + notified += [user] + next + end create_notification(user, Notification.types[:private_message], post) notified += [user] end @@ -31,7 +40,9 @@ class PostAlerter notify_users(reply_to_user, :replied, post) end - notified += [reply_to_user] if reply_to_user + if reply_to_user + notified += [reply_to_user] + end mentioned_groups, mentioned_users = extract_mentions(post) @@ -134,20 +145,21 @@ class PostAlerter # Don't notify the same user about the same notification on the same post existing_notification = user.notifications - .order("notifications.id DESC") + .order("notifications.id desc") .find_by(topic_id: post.topic_id, post_number: post.post_number, notification_type: type) - if existing_notification - return unless type == Notification.types[:edited] && existing_notification.data_hash["display_username"] == opts[:display_username] + if existing_notification && existing_notification.notification_type == type + return unless existing_notification.notification_type == Notification.types[:edited] && + existing_notification.data_hash["display_username"] = opts[:display_username] end collapsed = false if type == Notification.types[:replied] || type == Notification.types[:posted] - destroy_notifications(user, Notification.types[:replied], post.topic) - destroy_notifications(user, Notification.types[:posted], post.topic) + destroy_notifications(user, Notification.types[:replied] , post.topic) + destroy_notifications(user, Notification.types[:posted] , post.topic) collapsed = true end