From df7c38dd51f3754d90d8829eb65091e4ddb415b2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Mon, 16 Mar 2015 12:02:34 +0100
Subject: [PATCH] FIX: don't add an automated message when *a* moderator
 already replied (as opposed to *the* moderator)

---
 app/models/post_action.rb       |  6 +++---
 spec/models/post_action_spec.rb | 20 +++++++++++++++-----
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/app/models/post_action.rb b/app/models/post_action.rb
index 5a7514f454f..1ba8be9a4b1 100644
--- a/app/models/post_action.rb
+++ b/app/models/post_action.rb
@@ -185,14 +185,14 @@ class PostAction < ActiveRecord::Base
   def add_moderator_post_if_needed(moderator, disposition, delete_post=false)
     return if !SiteSetting.auto_respond_to_flag_actions
     return if related_post.nil? || related_post.topic.nil?
-    return if moderator_already_replied?(related_post.topic, moderator)
+    return if staff_already_replied?(related_post.topic)
     message_key = "flags_dispositions.#{disposition}"
     message_key << "_and_deleted" if delete_post
     related_post.topic.add_moderator_post(moderator, I18n.t(message_key), skip_notifications: true)
   end
 
-  def moderator_already_replied?(topic, moderator)
-    topic.posts.where("user_id = :user_id OR post_type = :post_type", user_id: moderator.id, post_type: Post.types[:moderator_action]).exists?
+  def staff_already_replied?(topic)
+    topic.posts.where("user_id IN (SELECT id FROM users WHERE moderator OR admin) OR post_type = :post_type", post_type: Post.types[:moderator_action]).exists?
   end
 
   def self.create_message_for_post_action(user, post, post_action_type_id, opts)
diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb
index b3700ff465c..3dc1d3b1078 100644
--- a/spec/models/post_action_spec.rb
+++ b/spec/models/post_action_spec.rb
@@ -54,18 +54,28 @@ describe PostAction do
       action.reload
       expect(action.deleted_at).to eq(nil)
 
-      # Acting on the flag should post an automated status message
+      # Acting on the flag should not post an automated status message (since a moderator already replied)
       expect(topic.posts.count).to eq(2)
       PostAction.agree_flags!(post, admin)
       topic.reload
-      expect(topic.posts.count).to eq(3)
-      expect(topic.posts.last.post_type).to eq(Post.types[:moderator_action])
+      expect(topic.posts.count).to eq(2)
 
-      # Clearing the flags should not post another automated status message
+      # Clearing the flags should not post an automated status message
       PostAction.act(mod, post, PostActionType.types[:notify_moderators], message: "another special message")
       PostAction.clear_flags!(post, admin)
       topic.reload
-      expect(topic.posts.count).to eq(3)
+      expect(topic.posts.count).to eq(2)
+
+      # Acting on the flag should post an automated status message
+      another_post = create_post
+      action = PostAction.act(codinghorror, another_post, PostActionType.types[:notify_moderators], message: "foobar")
+      topic = action.related_post.topic
+
+      expect(topic.posts.count).to eq(1)
+      PostAction.agree_flags!(another_post, admin)
+      topic.reload
+      expect(topic.posts.count).to eq(2)
+      expect(topic.posts.last.post_type).to eq(Post.types[:moderator_action])
     end
 
     describe 'notify_moderators' do