From d7b1313d5e94fc305c5a54efd2919de0823f1234 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Mon, 18 Aug 2014 17:00:14 +0200
Subject: [PATCH] FEATURE: acting on a flag should not post an automated status
 message if you already replied to it

---
 app/models/post_action.rb       | 10 ++++++++--
 spec/models/post_action_spec.rb |  4 +---
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/app/models/post_action.rb b/app/models/post_action.rb
index e8a78aa6395..8f41cd3c02e 100644
--- a/app/models/post_action.rb
+++ b/app/models/post_action.rb
@@ -155,13 +155,19 @@ class PostAction < ActiveRecord::Base
   end
 
   def add_moderator_post_if_needed(moderator, disposition, delete_post=false)
-    return unless related_post
-    return if related_post.topic.posts.where(post_type: Post.types[:moderator_action]).exists?
+    return if related_post.nil?
+    return if moderator_already_replied?(related_post.topic, moderator)
     message_key = "flags_dispositions.#{disposition}"
     message_key << "_and_deleted" if delete_post
     related_post.topic.add_moderator_post(moderator, I18n.t(message_key))
   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?
+  end
+
   def self.create_message_for_post_action(user, post, post_action_type_id, opts)
     post_action_type = PostActionType.types[post_action_type_id]
 
diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb
index 11fe6676d20..6fadf3a4360 100644
--- a/spec/models/post_action_spec.rb
+++ b/spec/models/post_action_spec.rb
@@ -43,9 +43,7 @@ describe PostAction do
       topic.topic_users(true).map(&:notification_level).uniq.should == [TopicUser.notification_levels[:watching]]
 
       # reply to PM should not clear flag
-      p = PostCreator.new(mod, topic_id: posts[0].topic_id, raw: "This is my test reply to the user, it should clear flags")
-      p.create
-
+      PostCreator.new(mod, topic_id: posts[0].topic_id, raw: "This is my test reply to the user, it should clear flags").create
       action.reload
       action.deleted_at.should be_nil