From e53f110c83cee6414845e72229848974a34e8fa2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Wed, 16 Dec 2015 22:20:38 +0100
Subject: [PATCH] Revert "FIX: prevent double notifications when replying to a
 group message"

This reverts commit e82dc21083586b881e581f0bd9b6091d836a26fb.
---
 app/services/post_alerter.rb | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb
index a66a7ea02..2a1bd9cd1 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