FIX: the muted message should be sent after edit (#9593)

FIX: the muted message should be sent after edit (#9593)

Recently, we added feature that we are sending /muted to users who muted specific topic just before /latest so the client knows to ignore those messages - https://github.com/discourse/discourse/pull/9482

Same /muted message should be included when the post is edited

diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb
index df6516c..015e1b0 100644
--- a/app/models/topic_tracking_state.rb
+++ b/app/models/topic_tracking_state.rb
@@ -72,8 +72,8 @@ class TopicTrackingState
     "/unread/#{user_id}"
   end
 
-  def self.publish_muted(post)
-    user_ids = post.topic.topic_users
+  def self.publish_muted(topic)
+    user_ids = topic.topic_users
       .where(notification_level: NotificationLevels.all[:muted])
       .joins(:user)
       .where("users.last_seen_at > ?", 7.days.ago)
@@ -82,7 +82,7 @@ class TopicTrackingState
       .pluck(:user_id)
     return if user_ids.blank?
     message = {
-      topic_id: post.topic_id,
+      topic_id: topic.id,
       message_type: MUTED_MESSAGE_TYPE,
     }
     MessageBus.publish("/latest", message.as_json, user_ids: user_ids)
diff --git a/lib/post_jobs_enqueuer.rb b/lib/post_jobs_enqueuer.rb
index d539a4d..afdd3cd 100644
--- a/lib/post_jobs_enqueuer.rb
+++ b/lib/post_jobs_enqueuer.rb
@@ -58,7 +58,7 @@ class PostJobsEnqueuer
 
   def after_post_create
     if @post.post_number > 1
-      TopicTrackingState.publish_muted(@post)
+      TopicTrackingState.publish_muted(@post.topic)
       TopicTrackingState.publish_unread(@post)
     end
     TopicTrackingState.publish_latest(@topic, @post.whisper?)
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index 0d60af6..9da306d 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -523,6 +523,7 @@ class PostRevisor
   def bump_topic
     return if bypass_bump? || !is_last_post?
     @topic.update_column(:bumped_at, Time.now)
+    TopicTrackingState.publish_muted(@topic)
     TopicTrackingState.publish_latest(@topic)
   end
 
diff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rb
index e5467f4..b79490b 100644
--- a/spec/components/post_revisor_spec.rb
+++ b/spec/components/post_revisor_spec.rb
@@ -6,7 +6,7 @@ require 'post_revisor'
 describe PostRevisor do
 
   fab!(:topic) { Fabricate(:topic) }
-  fab!(:newuser) { Fabricate(:newuser) }
+  fab!(:newuser) { Fabricate(:newuser, last_seen_at: Date.today) }
   fab!(:user) { Fabricate(:user) }
   fab!(:admin) { Fabricate(:admin) }
   fab!(:moderator) { Fabricate(:moderator) }
@@ -196,6 +196,19 @@ describe PostRevisor do
           subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds)
         }.to change { post.topic.bumped_at }
       end
+
+      it "should send muted and latest message" do
+        TopicUser.create!(topic: post.topic, user: post.user, notification_level: 0)
+        messages = MessageBus.track_publish("/latest") do
+          subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds)
+        end
+
+        muted_message = messages.find { |message| message.data["message_type"] == "muted" }
+        latest_message = messages.find { |message| message.data["message_type"] == "latest" }
+
+        expect(muted_message.data["topic_id"]).to eq(topic.id)
+        expect(latest_message.data["topic_id"]).to eq(topic.id)
+      end
     end
 
     describe 'edit reasons' do
diff --git a/spec/models/topic_tracking_state_spec.rb b/spec/models/topic_tracking_state_spec.rb
index 25bc127..db0a13f 100644
--- a/spec/models/topic_tracking_state_spec.rb
+++ b/spec/models/topic_tracking_state_spec.rb
@@ -81,9 +81,9 @@ describe TopicTrackingState do
     end
 
     it 'can correctly publish muted' do
-      TopicUser.find_by(topic: post.topic, user: post.user).update(notification_level: 0)
+      TopicUser.find_by(topic: topic, user: post.user).update(notification_level: 0)
       messages = MessageBus.track_publish("/latest") do
-        TopicTrackingState.publish_muted(post)
+        TopicTrackingState.publish_muted(topic)
       end
 
       muted_message = messages.find { |message| message.data["message_type"] == "muted" }
@@ -94,7 +94,7 @@ describe TopicTrackingState do
 
     it 'should not publish any message when notification level is not muted' do
       messages = MessageBus.track_publish("/latest") do
-        TopicTrackingState.publish_muted(post)
+        TopicTrackingState.publish_muted(topic)
       end
       muted_messages = messages.select { |message| message.data["message_type"] == "muted" }
 
@@ -102,10 +102,10 @@ describe TopicTrackingState do
     end
 
     it 'should not publish any message when the user was not seen in the last 7 days' do
-      TopicUser.find_by(topic: post.topic, user: post.user).update(notification_level: 0)
+      TopicUser.find_by(topic: topic, user: post.user).update(notification_level: 0)
       post.user.update(last_seen_at: 8.days.ago)
       messages = MessageBus.track_publish("/latest") do
-        TopicTrackingState.publish_muted(post)
+        TopicTrackingState.publish_muted(topic)
       end
       muted_messages = messages.select { |message| message.data["message_type"] == "muted" }
       expect(muted_messages).to eq([])

GitHub sha: 37e93914

This commit appears in #9593 which was approved by eviltrout. It was merged by lis2.