Skip to content

A platform for community discussion. Free, open, simple.

FIX: PM unread count shows (1) but message list is empty (#38733)

Régis Hanola8285f11be2e
diff --git a/app/jobs/regular/post_update_topic_tracking_state.rb b/app/jobs/regular/post_update_topic_tracking_state.rb
index da3a11268d8..c308df571a6 100644
--- a/app/jobs/regular/post_update_topic_tracking_state.rb
+++ b/app/jobs/regular/post_update_topic_tracking_state.rb
@@ -10,8 +10,9 @@ module Jobs
if topic.private_message?
PrivateMessageTopicTrackingState.publish_unread(post) if post.post_number > 1
-
- TopicGroup.new_message_update(topic.last_poster, topic.id, post.post_number)
+ if post.post_type != Post.types[:small_action]
+ TopicGroup.new_message_update(topic.last_poster, topic.id, post.post_number)
+ end
else
TopicTrackingState.publish_unmuted(post.topic)
if post.post_number > 1
diff --git a/app/models/private_message_topic_tracking_state.rb b/app/models/private_message_topic_tracking_state.rb
index 14518158fe7..d50069b3182 100644
--- a/app/models/private_message_topic_tracking_state.rb
+++ b/app/models/private_message_topic_tracking_state.rb
@@ -92,6 +92,7 @@ class PrivateMessageTopicTrackingState
topics.archetype = 'private_message' AND
((#{unread}) OR (#{new})) AND
topics.deleted_at IS NULL
+ #{user.staff? ? "" : "AND topics.visible"}
SQL
end
@@ -104,7 +105,7 @@ class PrivateMessageTopicTrackingState
allowed_group_ids = topic.allowed_groups.pluck(:id)
group_ids =
- if post.post_type == Post.types[:whisper]
+ if post.post_type == Post.types[:whisper] || post.post_type == Post.types[:small_action]
[Group::AUTO_GROUPS[:staff]]
else
allowed_group_ids
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 012a29f28a3..337824436e3 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -861,14 +861,18 @@ class Topic < ActiveRecord::Base
def self.next_post_number(topic_id, opts = {})
highest =
DB
- .query_single(
- "SELECT coalesce(max(post_number),0) AS max FROM posts WHERE topic_id = ?",
- topic_id,
- )
+ .query_single("SELECT coalesce(max(post_number),0) FROM posts WHERE topic_id = ?", topic_id)
.first
.to_i
- if opts[:whisper]
+ # PM small_action posts only bump highest_staff_post_number, not
+ # highest_post_number, matching the exclusion in reset_highest.
+ staff_only = opts[:post_type] == Post.types[:whisper]
+ staff_only ||=
+ opts[:post_type] == Post.types[:small_action] &&
+ Topic.where(id: topic_id, archetype: Archetype.private_message).exists?
+
+ if staff_only
result = DB.query_single(<<~SQL, highest, topic_id)
UPDATE topics
SET highest_staff_post_number = ? + 1
@@ -881,7 +885,7 @@ class Topic < ActiveRecord::Base
reply_sql = opts[:reply] ? ", reply_count = reply_count + 1" : ""
posts_sql = opts[:post] ? ", posts_count = posts_count + 1" : ""
- result = DB.query_single(<<~SQL, highest: highest, topic_id: topic_id)
+ result = DB.query_single(<<~SQL, highest:, topic_id:)
UPDATE topics
SET highest_staff_post_number = :highest + 1,
highest_post_number = :highest + 1
diff --git a/lib/post_creator.rb b/lib/post_creator.rb
index 71c8ba5434b..f57111a45c0 100644
--- a/lib/post_creator.rb
+++ b/lib/post_creator.rb
@@ -293,14 +293,13 @@ class PostCreator
post.word_count = post.raw.scan(/[[:word:]]+/).size
- whisper = post.post_type == Post.types[:whisper]
increase_posts_count =
!post.topic&.private_message? || post.post_type != Post.types[:small_action]
post.post_number ||=
Topic.next_post_number(
post.topic_id,
reply: post.reply_to_post_number.present?,
- whisper: whisper,
+ post_type: post.post_type,
post: increase_posts_count,
)
diff --git a/spec/jobs/post_update_topic_tracking_state_spec.rb b/spec/jobs/post_update_topic_tracking_state_spec.rb
index 54357513c5f..574a82d7e37 100644
--- a/spec/jobs/post_update_topic_tracking_state_spec.rb
+++ b/spec/jobs/post_update_topic_tracking_state_spec.rb
@@ -15,4 +15,27 @@ RSpec.describe Jobs::PostUpdateTopicTrackingState do
messages = MessageBus.track_publish { job.execute({ post_id: post.id }) }
expect(messages.size).to eq(0)
end
+
+ it "should not update topic groups for small_action posts in private messages" do
+ user = Fabricate(:user, refresh_auto_groups: true)
+ recipient = Fabricate(:user)
+ pm =
+ create_post(
+ user: user,
+ target_usernames: [recipient.username],
+ archetype: Archetype.private_message,
+ )
+
+ small_action =
+ Fabricate(
+ :post,
+ topic: pm.topic,
+ user: Discourse.system_user,
+ post_type: Post.types[:small_action],
+ action_code: "visible.disabled",
+ )
+
+ TopicGroup.expects(:new_message_update).never
+ job.execute({ post_id: small_action.id })
+ end
end
diff --git a/spec/lib/post_creator_spec.rb b/spec/lib/post_creator_spec.rb
index 36e9976e186..f87aadb6cbd 100644
--- a/spec/lib/post_creator_spec.rb
+++ b/spec/lib/post_creator_spec.rb
@@ -1236,6 +1236,28 @@ RSpec.describe PostCreator do
expect(topic.posts_count).to eq(3)
expect(topic.word_count).to eq([p1, p2, p3].sum(&:word_count))
end
+
+ it "does not bump highest_post_number for small_action posts in PMs" do
+ topic = Fabricate(:private_message_topic, user: Fabricate(:user, refresh_auto_groups: true))
+ Fabricate(:post, topic: topic)
+ topic.reload
+
+ expect(topic.highest_post_number).to eq(1)
+ expect(topic.highest_staff_post_number).to eq(1)
+
+ PostCreator.create!(
+ Discourse.system_user,
+ raw: "topic unlisted",
+ topic_id: topic.id,
+ post_type: Post.types[:small_action],
+ action_code: "visible.disabled",
+ skip_validations: true,
+ )
+ topic.reload
+
+ expect(topic.highest_post_number).to eq(1)
+ expect(topic.highest_staff_post_number).to eq(2)
+ end
end
describe "warnings" do
diff --git a/spec/models/private_message_topic_tracking_state_spec.rb b/spec/models/private_message_topic_tracking_state_spec.rb
index 0d754ae29ff..39c72a0f7cc 100644
--- a/spec/models/private_message_topic_tracking_state_spec.rb
+++ b/spec/models/private_message_topic_tracking_state_spec.rb
@@ -83,6 +83,25 @@ RSpec.describe PrivateMessageTopicTrackingState do
expect(described_class.report(user_2).map(&:topic_id)).to contain_exactly(private_message.id)
end
+
+ it "excludes invisible topics for non-staff users" do
+ TopicUser.find_by(user: user_2, topic: private_message).update!(last_read_post_number: 1)
+
+ private_message.update!(visible: false, highest_post_number: 2)
+
+ report = described_class.report(user_2)
+ expect(report.map(&:topic_id)).to contain_exactly(group_message.id)
+ end
+
+ it "includes invisible topics for staff users" do
+ user_2.grant_admin!
+ TopicUser.find_by(user: user_2, topic: private_message).update!(last_read_post_number: 1)
+
+ private_message.update!(visible: false, highest_post_number: 2)
+
+ report = described_class.report(user_2)
+ expect(report.map(&:topic_id)).to contain_exactly(group_message.id, private_message.id)
+ end
end
describe ".publish_new" do
@@ -144,6 +163,34 @@ RSpec.describe PrivateMessageTopicTrackingState do
expect(messages).to eq([])
end
+
+ it "publishes small_action posts only to staff users" do
+ staff = Fabricate(:moderator, refresh_auto_groups: true)
+ private_message.topic_allowed_users.create!(user_id: staff.id)
+ TopicUser.change(
+ staff.id,
+ private_message.id,
+ notification_level: NotificationLevels.all[:watching],
+ last_read_post_number: 1,
+ )
+
+ TopicUser.find_by(user: user_2, topic: private_message).update!(last_read_post_number: 1)
+
+ small_action =
+ Fabricate(
+ :post,
+ topic: private_message,
+ user: Discourse.system_user,
+ post_type: Post.types[:small_action],
+ action_code: "visible.disabled",
+ )
+
+ messages = MessageBus.track_publish { described_class.publish_unread(small_action) }
+
+ published_user_ids = messages.flat_map(&:user_ids)
+ expect(published_user_ids).to include(staff.id)
+ expect(published_user_ids).not_to include(user_2.id)
+ end
end
describe ".publish_group_archived" do
diff --git a/spec/services/topic_status_updater_spec.rb b/spec/services/topic_status_updater_spec.rb
index 4c47df6acc1..ed7cba7e317 100644
--- a/spec/services/topic_status_updater_spec.rb
+++ b/spec/services/topic_status_updater_spec.rb
@@ -46,31 +46,21 @@ RSpec.describe TopicStatusUpdater do
TopicUser.update_last_read(user_wants_unread, post.topic.id, 1, 1, 0)
TopicUser.update_last_read(user_wants_read, post.topic.id, 1, 1, 0)
- PostTiming.create!(
- topic_id: post.topic.id,
- post_number: 1,
- user_id: user_wants_unread.id,
- msecs: 1000,
- )
- PostTiming.create!(
- topic_id: post.topic.id,
- post_number: 1,
- user_id: user_wants_read.id,
- msecs: 1000,
- )
+ PostTiming.create!(topic: post.topic, post_number: 1, user: user_wants_unread, msecs: 1000)
+ PostTiming.create!(topic: post.topic, post_number: 1, user: user_wants_read, msecs: 1000)
TopicStatusUpdater.new(post.topic, admin).update!("closed", true)
# Should have 2 posts (original + close action)
expect(post.topic.posts.count).to eq(2)
- # User with topics_unread_when_closed enabled should NOT have read the close action
- tu_wants_unread = TopicUser.find_by(user_id: user_wants_unread.id, topic_id: post.topic.id)
+ # In PMs, close small_action posts only bump highest_staff_post_number,
+ # so neither user's last_read_post_number advances past the original post.
+ tu_wants_unread = TopicUser.find_by(user: user_wants_unread, topic: post.topic)
expect(tu_wants_unread.last_read_post_number).to eq(1)
- # User with topics_unread_when_closed disabled SHOULD have read the close action
- tu_wants_read = TopicUser.find_by(user_id: user_wants_read.id, topic_id: post.topic.id)
- expect(tu_wants_read.last_read_post_number).to eq(2)
+ tu_wants_read = TopicUser.find_by(user: user_wants_read, topic: post.topic)
+ expect(tu_wants_read.last_read_post_number).to eq(1)
end
it "adds an autoclosed message" do