FIX: Moderator actions and small actions shouldn't prevent fully merged topics from closing (#13200)

FIX: Moderator actions and small actions shouldn’t prevent fully merged topics from closing (#13200)

When a topic is fully merged into another topic we close it and schedule its deleting. But, because of a bug, if the merged topic contains some moderator actions or small actions it won’t be merged. This change fixes this problem.

An important note: in general, we don’t want to close a topic after moving posts if it still contains some regular posts or whispers. But when we are moving posts to a private message we don’t want the notice about it to be publicly visible. So we use whispers with action_code == ‘split_topic’ instead of small_actions in such cases and we should ignore this specific kind of whispers when decide if we should close the merged topic.

diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb
index bedd483..8a7d351 100644
--- a/app/models/post_mover.rb
+++ b/app/models/post_mover.rb
@@ -66,8 +66,6 @@ class PostMover
     Guardian.new(user).ensure_can_see! topic
     @destination_topic = topic
 
-    moving_all_posts = (@original_topic.posts.pluck(:id).sort == @post_ids.sort)
-
     create_temp_table
     delete_invalid_post_timings
     move_each_post
@@ -78,7 +76,11 @@ class PostMover
     update_upload_security_status
     update_bookmarks
 
-    if moving_all_posts
+    posts_left = @original_topic.posts
+      .where("post_type = ? or (post_type = ? and action_code != 'split_topic')", Post.types[:regular], Post.types[:whisper])
+      .count
+
+    if posts_left == 1
       close_topic_and_schedule_deletion
     end
 
diff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rb
index 05220dd..11c3eef 100644
--- a/spec/models/post_mover_spec.rb
+++ b/spec/models/post_mover_spec.rb
@@ -58,6 +58,14 @@ describe PostMover do
         @like = PostActionCreator.like(another_user, p4)
       end
 
+      def add_moderator_post_to(topic, post_type)
+        topic.add_moderator_post(
+          user,
+          "message",
+          post_type: post_type,
+          action_code: "split_topic")
+      end
+
       context 'success' do
 
         it "correctly handles notifications and bread crumbs" do
@@ -664,6 +672,17 @@ describe PostMover do
             expect(timer).to be_nil
           end
 
+          it "ignores moderator posts and closes the topic if all regular posts were moved" do
+            add_moderator_post_to topic, Post.types[:moderator_action]
+            add_moderator_post_to topic, Post.types[:small_action]
+
+            posts_to_move = [p1.id, p2.id, p3.id, p4.id]
+            topic.move_posts(user, posts_to_move, destination_topic_id: destination_topic.id)
+
+            topic.reload
+            expect(topic).to be_closed
+          end
+
           it "does not try to move small action posts" do
             small_action = Fabricate(:post, topic: topic, raw: "A small action", post_type: Post.types[:small_action])
             moved_to = topic.move_posts(user, [p1.id, p2.id, p3.id, p4.id, small_action.id], destination_topic_id: destination_topic.id)

GitHub sha: b7b8f5e6

This commit appears in #13200 which was approved by tgxworld. It was merged by AndrewPrigorshnev.