diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb index a5434399ff4..d24dd535a37 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 5eaecd27163..ad2ab724e4a 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 00472bb44c7..d795bb90f42 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