FIX: Don't update `watching_first_post` notifications when moving first post

FIX: Don’t update watching_first_post notifications when moving first post

The first post isn’t moved. It gets copied during a move. Notifications of this special type should still link to the original first post.

diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb
index bf7ca56..3fcf4f3 100644
--- a/app/models/post_mover.rb
+++ b/app/models/post_mover.rb
@@ -238,6 +238,7 @@ class PostMover
             )) :: JSON
       FROM moved_posts mp
       WHERE n.topic_id = mp.old_topic_id AND n.post_number = mp.old_post_number
+        AND n.notification_type <> #{Notification.types[:watching_first_post]}
     SQL
   end
 
diff --git a/spec/fabricators/notification_fabricator.rb b/spec/fabricators/notification_fabricator.rb
index 2d22ed3..6c386c7 100644
--- a/spec/fabricators/notification_fabricator.rb
+++ b/spec/fabricators/notification_fabricator.rb
@@ -72,3 +72,17 @@ Fabricator(:mentioned_notification, from: :notification) do
     }.to_json
   end
 end
+
+Fabricator(:watching_first_post_notification, from: :notification) do
+  notification_type Notification.types[:watching_first_post]
+  data do |attrs|
+    {
+      topic_title: attrs[:topic].title,
+      original_post_id: attrs[:post].id,
+      original_post_type: attrs[:post].post_type,
+      original_username: attrs[:post].user.username,
+      revision_number: nil,
+      display_username: attrs[:post].user.username
+    }.to_json
+  end
+end
diff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rb
index da1aedd..80a5f97 100644
--- a/spec/models/post_mover_spec.rb
+++ b/spec/models/post_mover_spec.rb
@@ -312,6 +312,17 @@ describe PostMover do
             expect(n4.post_number).to eq(4)
           end
 
+          it "doesn't update notifications of type 'watching_first_post'" do
+            n1 = Fabricate(:watching_first_post_notification, post: p1, user: another_user)
+
+            topic.move_posts(user, [p1.id], title: "new testing topic name")
+
+            n1.reload
+            expect(n1.topic_id).to eq(topic.id)
+            expect(n1.data_hash[:topic_title]).to eq(topic.title)
+            expect(n1.post_number).to eq(1)
+          end
+
           it "deletes notifications for users not allowed to see the topic" do
             another_admin = Fabricate(:admin)
             staff_category = Fabricate(:private_category, group: Group[:staff])

GitHub sha: 2a95c5c5

1 Like