FIX: Correct highest post number for read topic tracking state. (#14273)

FIX: Correct highest post number for read topic tracking state. (#14273)

diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb
index a543439..d24dd53 100644
--- a/app/models/topic_tracking_state.rb
+++ b/app/models/topic_tracking_state.rb
@@ -61,7 +61,7 @@ class TopicTrackingState
     group_ids = topic.category && topic.category.secure_group_ids
 
     MessageBus.publish("/new", message.as_json, group_ids: group_ids)
-    publish_read(topic.id, 1, topic.user_id)
+    publish_read(topic.id, 1, topic.user)
   end
 
   def self.publish_latest(topic, staff_only = false)
@@ -226,8 +226,13 @@ class TopicTrackingState
     MessageBus.publish("/destroy", message.as_json, group_ids: group_ids)
   end
 
-  def self.publish_read(topic_id, last_read_post_number, user_id, notification_level = nil)
-    highest_post_number = DB.query_single("SELECT highest_post_number FROM topics WHERE id = ?", topic_id).first
+  def self.publish_read(topic_id, last_read_post_number, user, notification_level = nil)
+    user_id = user.id
+
+    highest_post_number = DB.query_single(
+      "SELECT #{user.staff? ? "highest_staff_post_number" : "highest_post_number"} FROM topics WHERE id = ?",
+      topic_id
+    ).first
 
     message = {
       topic_id: topic_id,
diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb
index 5eaecd2..ad2ab72 100644
--- a/app/models/topic_user.rb
+++ b/app/models/topic_user.rb
@@ -327,7 +327,12 @@ class TopicUser < ActiveRecord::Base
 
         if before_last_read < post_number
           # The user read at least one new post
-          TopicTrackingState.publish_read(topic_id, post_number, user.id, after)
+          TopicTrackingState.publish_read(
+            topic_id,
+            post_number,
+            user,
+            after
+          )
         end
 
         if new_posts_read > 0
@@ -345,7 +350,13 @@ class TopicUser < ActiveRecord::Base
         if (user.user_option.auto_track_topics_after_msecs || SiteSetting.default_other_auto_track_topics_after_msecs) == 0
           args[:new_status] = notification_levels[:tracking]
         end
-        TopicTrackingState.publish_read(topic_id, post_number, user.id, args[:new_status])
+
+        TopicTrackingState.publish_read(
+          topic_id,
+          post_number,
+          user,
+          args[:new_status]
+        )
 
         user.update_posts_read!(new_posts_read, mobile: opts[:mobile])
 
diff --git a/spec/models/topic_tracking_state_spec.rb b/spec/models/topic_tracking_state_spec.rb
index 00472bb..d795bb9 100644
--- a/spec/models/topic_tracking_state_spec.rb
+++ b/spec/models/topic_tracking_state_spec.rb
@@ -40,6 +40,43 @@ describe TopicTrackingState do
     end
   end
 
+  describe '.publish_read' do
+    it 'correctly publish read' do
+      message = MessageBus.track_publish(described_class.unread_channel_key(post.user.id)) do
+        TopicTrackingState.publish_read(post.topic_id, 1, post.user)
+      end.first
+
+      data = message.data
+
+      expect(message.user_ids).to contain_exactly(post.user_id)
+      expect(message.group_ids).to eq(nil)
+      expect(data["topic_id"]).to eq(post.topic_id)
+      expect(data["message_type"]).to eq(described_class::READ_MESSAGE_TYPE)
+      expect(data["payload"]["last_read_post_number"]).to eq(1)
+      expect(data["payload"]["highest_post_number"]).to eq(1)
+      expect(data["payload"]["notification_level"]).to eq(nil)
+    end
+
+    it 'correctly publish read for staff' do
+      create_post(
+        raw: "this is a test post",
+        topic: post.topic,
+        post_type: Post.types[:whisper],
+        user: Fabricate(:admin)
+      )
+
+      post.user.grant_admin!
+
+      message = MessageBus.track_publish(described_class.unread_channel_key(post.user.id)) do
+        TopicTrackingState.publish_read(post.topic_id, 1, post.user)
+      end.first
+
+      data = message.data
+
+      expect(data["payload"]["highest_post_number"]).to eq(2)
+    end
+  end
+
   describe '#publish_unread' do
     it "can correctly publish unread" do
       message = MessageBus.track_publish(described_class.unread_channel_key(post.user.id)) do

GitHub sha: 5de64b363060f3fe08b17e16702425da18e7efec

This commit appears in #14273 which was approved by martin. It was merged by tgxworld.