A platform for community discussion. Free, open, simple.
FEATURE: Exclude small actions from topic counters and unread tracking (#40481)
Régis Hanold68be7b5f6df
diff --git a/app/jobs/regular/post_update_topic_tracking_state.rb b/app/jobs/regular/post_update_topic_tracking_state.rbindex 912a742a70c..ceca7fb9043 100644--- a/app/jobs/regular/post_update_topic_tracking_state.rb+++ b/app/jobs/regular/post_update_topic_tracking_state.rb@@ -5,14 +5,13 @@ module Jobsdef execute(args)post = Post.find_by(id: args[:post_id])return if !post&.topic+ return if post.small_action?topic = post.topicif topic.private_message?PrivateMessageTopicTrackingState.publish_unread(post) if post.post_number > 1- if post.post_type != Post.types[:small_action]- TopicGroup.new_message_update(topic.last_poster, topic.id, post.post_number)- end+ TopicGroup.new_message_update(topic.last_poster, topic.id, post.post_number)elseTopicTrackingState.publish_unmuted(post.topic)if post.post_number > 1diff --git a/app/models/category.rb b/app/models/category.rbindex dcffe902497..c7b40028d76 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -521,6 +521,7 @@ class Category < ActiveRecord::BaseAND topics.visible = trueAND posts.deleted_at IS NULLAND posts.user_deleted = false+ AND posts.post_type <> #{Post.types[:small_action]}#{c.topic_id ? "AND topics.id != :topic_id" : ""}SQLdiff --git a/app/models/post.rb b/app/models/post.rbindex ed774a1f455..cce9c321df1 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -200,6 +200,10 @@ class Post < ActiveRecord::Basepost_type == Post.types[:whisper]end+ def small_action?+ post_type == Post.types[:small_action]+ end+def add_detail(key, value, extra = nil)post_details.build(key: key, value: value, extra: extra)enddiff --git a/app/models/private_message_topic_tracking_state.rb b/app/models/private_message_topic_tracking_state.rbindex d4a6917fe2c..45be8238ff7 100644--- a/app/models/private_message_topic_tracking_state.rb+++ b/app/models/private_message_topic_tracking_state.rb@@ -99,17 +99,11 @@ class PrivateMessageTopicTrackingStatedef self.publish_unread(post)topic = post.topicreturn unless topic.private_message?+ return if post.small_action?scope = TopicUser.tracking(post.topic_id).includes(user: %i[user_stat user_option])- allowed_group_ids = topic.allowed_groups.pluck(:id)-- group_ids =- if post.post_type == Post.types[:whisper] || post.post_type == Post.types[:small_action]- [Group::AUTO_GROUPS[:staff]]- else- allowed_group_ids- end+ group_ids = post.whisper? ? [Group::AUTO_GROUPS[:staff]] : topic.allowed_groups.pluck(:id)if group_ids.present?scope =@@ -142,7 +136,7 @@ class PrivateMessageTopicTrackingStatelast_read_post_number: tu.last_read_post_number,highest_post_number: post.post_number,notification_level: tu.notification_level,- group_ids: allowed_group_ids,+ group_ids: group_ids,created_by_user_id: post.user_id,},}diff --git a/app/models/topic.rb b/app/models/topic.rbindex 24c8a141fe3..cbb5fc3bcdf 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -885,6 +885,14 @@ class Topic < ActiveRecord::Baseendend+ def self.public_post_types_sql+ "post_type NOT IN (#{Post.types[:small_action]}, #{Post.types[:whisper]})"+ end++ def self.staff_post_types_sql+ "post_type <> #{Post.types[:small_action]}"+ end+# Atomically creates the next post numberdef self.next_post_number(topic_id, opts = {})highest =@@ -893,22 +901,17 @@ class Topic < ActiveRecord::Base.first.to_i- # 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?+ post_type = opts[:post_type]- if staff_only+ return highest + 1 if post_type == Post.types[:small_action]++ if post_type == Post.types[:whisper]result = DB.query_single(<<~SQL, highest, topic_id)UPDATE topicsSET highest_staff_post_number = ? + 1WHERE id = ?RETURNING highest_staff_post_numberSQL-- result.first.to_ielsereply_sql = opts[:reply] ? ", reply_count = reply_count + 1" : ""posts_sql = opts[:post] ? ", posts_count = posts_count + 1" : ""@@ -922,9 +925,9 @@ class Topic < ActiveRecord::BaseWHERE id = :topic_idRETURNING highest_post_numberSQL-- result.first.to_iend++ result.first.to_ienddef self.reset_all_highest!@@ -932,9 +935,9 @@ class Topic < ActiveRecord::BaseWITHX as (SELECT topic_id,- COALESCE(MAX(post_number), 0) highest_post_number+ COALESCE(MAX(post_number), 0) highest_staff_post_numberFROM posts- WHERE deleted_at IS NULL+ WHERE deleted_at IS NULL AND #{staff_post_types_sql}GROUP BY topic_id),Y as (@@ -943,76 +946,29 @@ class Topic < ActiveRecord::Basecount(*) posts_count,max(created_at) last_posted_atFROM posts- WHERE deleted_at IS NULL AND post_type <> 4+ WHERE deleted_at IS NULL AND #{public_post_types_sql}GROUP BY topic_id),Z as (SELECT topic_id,SUM(COALESCE(posts.word_count, 0)) word_countFROM posts- WHERE deleted_at IS NULL AND post_type <> 4+ WHERE deleted_at IS NULL AND #{public_post_types_sql}GROUP BY topic_id)UPDATE topicsSET- highest_staff_post_number = X.highest_post_number,+ highest_staff_post_number = X.highest_staff_post_number,highest_post_number = Y.highest_post_number,last_posted_at = Y.last_posted_at,posts_count = Y.posts_count,word_count = Z.word_countFROM X, Y, ZWHERE- topics.archetype <> 'private_message' ANDX.topic_id = topics.id ANDY.topic_id = topics.id ANDZ.topic_id = topics.id AND (- topics.highest_staff_post_number <> X.highest_post_number OR- topics.highest_post_number <> Y.highest_post_number OR- topics.last_posted_at <> Y.last_posted_at OR- topics.posts_count <> Y.posts_count OR- topics.word_count <> Z.word_count- )- SQL-- DB.exec <<~SQL- WITH- X as (- SELECT topic_id,- COALESCE(MAX(post_number), 0) highest_post_number- FROM posts- WHERE deleted_at IS NULL- GROUP BY topic_id- ),- Y as (- SELECT topic_id,- coalesce(MAX(post_number), 0) highest_post_number,- count(*) posts_count,- max(created_at) last_posted_at- FROM posts- WHERE deleted_at IS NULL AND post_type <> 3 AND post_type <> 4- GROUP BY topic_id- ),- Z as (- SELECT topic_id,- SUM(COALESCE(posts.word_count, 0)) word_count- FROM posts- WHERE deleted_at IS NULL AND post_type <> 3 AND post_type <> 4- GROUP BY topic_id- )- UPDATE topics- SET- highest_staff_post_number = X.highest_post_number,- highest_post_number = Y.highest_post_number,- last_posted_at = Y.last_posted_at,- posts_count = Y.posts_count,- word_count = Z.word_count- FROM X, Y, Z- WHERE- topics.archetype = 'private_message' AND- X.topic_id = topics.id AND- Y.topic_id = topics.id AND- Z.topic_id = topics.id AND (- topics.highest_staff_post_number <> X.highest_post_number OR+ topics.highest_staff_post_number <> X.highest_staff_post_number ORtopics.highest_post_number <> Y.highest_post_number ORtopics.last_posted_at <> Y.last_posted_at ORtopics.posts_count <> Y.posts_count OR@@ -1023,54 +979,44 @@ class Topic < ActiveRecord::Base# If a post is deleted we have to update our highest post counters and last post informationdef self.reset_highest(topic_id)- archetype = Topic.where(id: topic_id).pick(:archetype)-- # ignore small_action replies for private messages- post_type =- archetype == Archetype.private_message ? " AND post_type <> #{Post.types[:small_action]}" : ""-result = DB.query_single(<<~SQL, topic_id:)UPDATE topicsSEThighest_staff_post_number = (SELECT COALESCE(MAX(post_number), 0) FROM postsWHERE topic_id = :topic_id AND- deleted_at IS NULL+ deleted_at IS NULL AND+ #{staff_post_types_sql}),highest_post_number = (SELECT COALESCE(MAX(post_number), 0) FROM postsWHERE topic_id = :topic_id ANDdeleted_at IS NULL AND- post_type <> 4- #{post_type}+ #{public_post_types_sql}),posts_count = (SELECT count(*) FROM postsWHERE deleted_at IS NULL ANDtopic_id = :topic_id AND- post_type <> 4- #{post_type}+ #{public_post_types_sql}),word_count = (SELECT SUM(COALESCE(posts.word_count, 0)) FROM postsWHERE topic_id = :topic_id ANDdeleted_at IS NULL AND- post_type <> 4- #{post_type}+ #{public_post_types_sql}),last_posted_at = (SELECT MAX(created_at) FROM postsWHERE topic_id = :topic_id ANDdeleted_at IS NULL AND- post_type <> 4- #{post_type}+ #{public_post_types_sql}),last_post_user_id = COALESCE((SELECT user_id FROM postsWHERE topic_id = :topic_id ANDdeleted_at IS NULL AND- post_type <> 4- #{post_type}+ #{public_post_types_sql}ORDER BY created_at descLIMIT 1), last_post_user_id)@@ -1411,7 +1357,7 @@ class Topic < ActiveRecord::Basedef update_statistics!feature_topic_usersupdate_action_counts- self.highest_post_number = Topic.reset_highest(id)+ update_column(:highest_post_number, Topic.reset_highest(id))enddef update_action_countsdiff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rbindex 1eb8285ac3e..507f35e5473 100644--- a/app/models/topic_tracking_state.rb+++ b/app/models/topic_tracking_state.rb@@ -120,6 +120,7 @@ class TopicTrackingStatedef self.publish_unread(post)return unless post.topic.regular?+ return if post.small_action?# TODO at high scale we are going to have to defer this,# perhaps cut down to users that are around in the last 7 days as welldiff --git a/app/models/user_stat.rb b/app/models/user_stat.rbindex 091ac61dbb6..d2a8af57f0e 100644--- a/app/models/user_stat.rb+++ b/app/models/user_stat.rb@@ -252,6 +252,7 @@ class UserStat < ActiveRecord::BaseAND topics.user_id <> posts.user_idAND posts.deleted_at IS NULL AND topics.deleted_at IS NULLAND topics.archetype <> 'private_message'+ AND posts.post_type <> #{Post.types[:small_action]}#{start_time.nil? ? "" : "AND posts.created_at > ?"}SQLif start_time.nil?diff --git a/db/post_migrate/20260602104726_recalculate_topic_counters_without_small_actions.rb b/db/post_migrate/20260602104726_recalculate_topic_counters_without_small_actions.rbnew file mode 100644index 00000000000..4cc1c5c990f--- /dev/null+++ b/db/post_migrate/20260602104726_recalculate_topic_counters_without_small_actions.rb@@ -0,0 +1,126 @@+# frozen_string_literal: true++class RecalculateTopicCountersWithoutSmallActions < ActiveRecord::Migration[8.0]+ disable_ddl_transaction!++ BATCH_SIZE = 10_000++ def up+ whisper_group_ids =+ DB+ .query_single("SELECT value FROM site_settings WHERE name = 'whispers_allowed_groups'")+ .first+ .to_s+ .split("|")+ .map(&:to_i)+ .select(&:positive?)++ last_post_id = 0++ loop do+ rows = DB.query(<<~SQL, last_post_id:, batch_size: BATCH_SIZE)+ SELECT id, topic_id+ FROM posts+ WHERE post_type = 3 AND id > :last_post_id+ ORDER BY id+ LIMIT :batch_size+ SQL++ break if rows.empty?++ last_post_id = rows.last.id+ topic_ids = rows.map(&:topic_id).uniq++ recalculate_counters(topic_ids)+ clamp_last_read(topic_ids, whisper_group_ids)+ end+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end++ private++ def recalculate_counters(topic_ids)+ DB.exec(<<~SQL, topic_ids:)+ WITH stats AS (+ SELECT+ topic_id,+ COALESCE(MAX(post_number) FILTER (WHERE deleted_at IS NULL AND post_type <> 3), 0)+ AS highest_staff_post_number,+ COALESCE(MAX(post_number) FILTER (WHERE deleted_at IS NULL AND post_type NOT IN (3, 4)), 0)+ AS highest_post_number,+ COUNT(*) FILTER (WHERE deleted_at IS NULL AND post_type NOT IN (3, 4))+ AS posts_count,+ COALESCE(SUM(COALESCE(word_count, 0)) FILTER (WHERE deleted_at IS NULL AND post_type NOT IN (3, 4)), 0)+ AS word_count,+ MAX(created_at) FILTER (WHERE deleted_at IS NULL AND post_type NOT IN (3, 4))+ AS last_posted_at+ FROM posts+ WHERE topic_id IN (:topic_ids)+ GROUP BY topic_id+ ), last_posts AS (+ SELECT DISTINCT ON (topic_id) topic_id, user_id+ FROM posts+ WHERE topic_id IN (:topic_ids) AND deleted_at IS NULL AND post_type NOT IN (3, 4)+ ORDER BY topic_id, created_at DESC, id DESC+ )+ UPDATE topics+ SET highest_staff_post_number = stats.highest_staff_post_number,+ highest_post_number = stats.highest_post_number,+ posts_count = stats.posts_count,+ word_count = stats.word_count,+ last_posted_at = stats.last_posted_at,+ last_post_user_id = COALESCE(last_posts.user_id, topics.last_post_user_id)+ FROM stats+ LEFT JOIN last_posts ON last_posts.topic_id = stats.topic_id+ WHERE topics.id = stats.topic_id AND (+ topics.highest_staff_post_number IS DISTINCT FROM stats.highest_staff_post_number OR+ topics.highest_post_number IS DISTINCT FROM stats.highest_post_number OR+ topics.posts_count IS DISTINCT FROM stats.posts_count OR+ topics.word_count IS DISTINCT FROM stats.word_count OR+ topics.last_posted_at IS DISTINCT FROM stats.last_posted_at OR+ topics.last_post_user_id IS DISTINCT FROM COALESCE(last_posts.user_id, topics.last_post_user_id)+ )+ SQL+ end++ def clamp_last_read(topic_ids, whisper_group_ids)+ cap =+ "CASE WHEN #{whisperer_sql(whisper_group_ids)} THEN t.highest_staff_post_number ELSE t.highest_post_number END"++ binds = { topic_ids: }+ binds[:whisper_group_ids] = whisper_group_ids if whisper_group_ids.present?++ DB.exec(<<~SQL, **binds)+ UPDATE topic_users tu+ SET last_read_post_number = #{cap}+ FROM topics t+ WHERE tu.topic_id = t.id+ AND t.id IN (:topic_ids)+ AND tu.last_read_post_number > #{cap}+ SQL+ end++ def whisperer_sql(whisper_group_ids)+ group_check =+ if whisper_group_ids.present?+ <<~SQL+ OR EXISTS (+ SELECT 1 FROM group_users gu+ WHERE gu.user_id = u.id AND gu.group_id IN (:whisper_group_ids)+ )+ SQL+ else+ ""+ end++ <<~SQL+ EXISTS (+ SELECT 1 FROM users u+ WHERE u.id = tu.user_id AND (u.admin OR u.moderator #{group_check})+ )+ SQL+ end+enddiff --git a/db/structure.sql b/db/structure.sqlindex a07f49fe40b..6f3af8d1ad7 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -21853,6 +21853,7 @@ INSERT INTO "schema_migrations" (version) VALUES('20260604052235'),('20260603115312'),('20260603115200'),+('20260602104726'),('20260601043020'),('20260525105009'),('20260525105006'),diff --git a/lib/post_creator.rb b/lib/post_creator.rbindex 302e2742040..0c70517f403 100644--- a/lib/post_creator.rb+++ b/lib/post_creator.rb@@ -291,14 +291,12 @@ class PostCreatorpost.word_count = post.raw.scan(/[[:word:]]+/).size- 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?,post_type: post.post_type,- post: increase_posts_count,+ post: true,)cooking_options = post.cooking_options || {}@@ -521,11 +519,14 @@ class PostCreatordef update_topic_statsattrs = { updated_at: Time.now }- if @post.post_type != Post.types[:whisper] && !@opts[:silent]- attrs[:last_posted_at] = @post.created_at- attrs[:last_post_user_id] = @post.user_id- attrs[:word_count] = (@topic.word_count || 0) + @post.word_count- attrs[:excerpt] = @post.excerpt_for_topic if new_topic?+ if [email protected]? && !@opts[:silent]+ unless @post.small_action?+ attrs[:last_posted_at] = @post.created_at+ attrs[:last_post_user_id] = @post.user_id+ attrs[:word_count] = (@topic.word_count || 0) + @post.word_count+ attrs[:excerpt] = @post.excerpt_for_topic if new_topic?+ end+attrs[:bumped_at] = @post.created_at unless @post.no_bumpend@@ -621,7 +622,7 @@ class PostCreatorUserStatCountUpdater.increment!(@post) if [email protected] || @post.topic.visible- if [email protected]_message? && @post.post_type != Post.types[:whisper]@user.update(last_posted_at: @post.created_at)endenddiff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rbindex 23f1c235826..92c574ab89d 100644--- a/lib/post_destroyer.rb+++ b/lib/post_destroyer.rb@@ -334,7 +334,7 @@ class PostDestroyer.select(:created_at, :user_id, :post_number).where("topic_id = ? and id <> ?", @post.topic_id, @post.id).where.not(user_id: nil)- .where.not(post_type: Post.types[:whisper])+ .where.not(post_type: [Post.types[:whisper], Post.types[:small_action]]).order("created_at desc").firstdiff --git a/lib/tasks/import.rake b/lib/tasks/import.rakeindex 4758ce59c3b..80ec61d81c3 100644--- a/lib/tasks/import.rake+++ b/lib/tasks/import.rake@@ -424,12 +424,14 @@ def update_topicsFROM postsWHERE NOT hiddenAND deleted_at IS NULL+ AND #{Topic.public_post_types_sql}AND topic_id = p.topic_idORDER BY post_number DESCLIMIT 1) last_posterFROM posts pWHERE NOT hiddenAND deleted_at IS NULL+ AND #{Topic.public_post_types_sql}GROUP BY topic_id)UPDATE topicsdiff --git a/plugins/discourse-solved/spec/integration/solved_spec.rb b/plugins/discourse-solved/spec/integration/solved_spec.rbindex f2bd9d84309..6b8c7905ba4 100644--- a/plugins/discourse-solved/spec/integration/solved_spec.rb+++ b/plugins/discourse-solved/spec/integration/solved_spec.rb@@ -250,7 +250,7 @@ RSpec.describe "Managing Posts solved status" doexpect(category.auto_bump_topic!).to eq(false)expect(post.topic.reload.posts_count).to eq(2)- expect(post2.topic.reload.posts_count).to eq(2)+ expect(post2.topic.reload.posts_count).to eq(1)endenddiff --git a/spec/lib/post_creator_spec.rb b/spec/lib/post_creator_spec.rbindex 92d2dac6b89..c9425b590d2 100644--- a/spec/lib/post_creator_spec.rb+++ b/spec/lib/post_creator_spec.rb@@ -1224,7 +1224,7 @@ RSpec.describe PostCreator doexpect(topic.posts.where(post_type: Post.types[:small_action]).count).to eq(i)end- expect(topic.word_count).to eq(0)+ expect(topic.word_count).to be_nilp2 = Fabricate(:post, topic: topic)Topic.reset_highest(topic.id)@@ -1239,7 +1239,7 @@ RSpec.describe PostCreator doexpect(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+ it "does not bump any post number counter for small_action posts" dotopic = Fabricate(:private_message_topic, user: Fabricate(:user, refresh_auto_groups: true))Fabricate(:post, topic: topic)topic.reload@@ -1258,7 +1258,7 @@ RSpec.describe PostCreator dotopic.reloadexpect(topic.highest_post_number).to eq(1)- expect(topic.highest_staff_post_number).to eq(2)+ expect(topic.highest_staff_post_number).to eq(1)endenddiff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rbindex f6faf63e66d..ace097e64e9 100644--- a/spec/lib/topic_query_spec.rb+++ b/spec/lib/topic_query_spec.rb@@ -1276,7 +1276,7 @@ RSpec.describe TopicQuery doendcontext "with a small action at the tail of an unread topic" do- it "keeps the topic in the unread list" do+ it "excludes the topic from the unread list" dotopic = create_post(raw: "the original post", title: "super amazing title").topictopic.add_small_action(Discourse.system_user, "closed.enabled")topic.update_columns(updated_at: Time.zone.now, bumped_at: 1.year.ago)@@ -1289,7 +1289,9 @@ RSpec.describe TopicQuery doTopicUser.update_last_read(user, topic.id, 1, 1, 1)user.user_stat.update!(first_unread_at: 1.minute.ago)- expect(TopicQuery.new(user).list_unread.topics).to include(topic)+ # The small action does not advance highest_post_number, so the user has+ # read everything that counts — the topic is no longer unread.+ expect(TopicQuery.new(user).list_unread.topics).not_to include(topic)endenddiff --git a/spec/migrations/recalculate_topic_counters_without_small_actions_spec.rb b/spec/migrations/recalculate_topic_counters_without_small_actions_spec.rbnew file mode 100644index 00000000000..7e0dbfcf252--- /dev/null+++ b/spec/migrations/recalculate_topic_counters_without_small_actions_spec.rb@@ -0,0 +1,100 @@+# frozen_string_literal: true++require Rails.root.join(+ "db/post_migrate/20260602104726_recalculate_topic_counters_without_small_actions.rb",+ )++RSpec.describe RecalculateTopicCountersWithoutSmallActions do+ subject(:migrate) { described_class.new.up }++ before do+ @verbose = ActiveRecord::Migration.verbose+ ActiveRecord::Migration.verbose = false+ end++ after { ActiveRecord::Migration.verbose = @verbose }++ fab!(:author, :user)+ fab!(:reader, :user)+ fab!(:topic)++ # Build a posts table that reflects reality, then force the topic + topic_users+ # into the *pre-fix* inflated state (small action counted) so we can assert the+ # migration corrects it.+ def small_action!(post_number)+ Fabricate(:post, topic: topic, user: Discourse.system_user).tap do |p|+ p.update_columns(post_number:, post_type: Post.types[:small_action], raw: "")+ end+ end++ it "recomputes counters and clamps read state for a trailing small action" do+ op = Fabricate(:post, topic: topic, user: author)+ op.update_columns(post_number: 1, post_type: Post.types[:regular])+ small_action!(2)++ topic.update_columns(+ highest_post_number: 2,+ highest_staff_post_number: 2,+ posts_count: 2,+ last_post_user_id: Discourse.system_user.id,+ )+ tracker =+ Fabricate(+ :topic_user,+ topic: topic,+ user: reader,+ last_read_post_number: 2,+ notification_level: TopicUser.notification_levels[:tracking],+ )++ migrate++ topic.reload+ expect(topic.highest_post_number).to eq(1)+ expect(topic.highest_staff_post_number).to eq(1)+ expect(topic.posts_count).to eq(1)+ expect(topic.last_post_user_id).to eq(author.id)+ # last_read pointed at the small action -> clamped back to the real highest.+ expect(tracker.reload.last_read_post_number).to eq(1)+ end++ it "clamps whisperers to highest_staff_post_number and others to highest_post_number" do+ SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"+ admin = Fabricate(:admin)++ op = Fabricate(:post, topic: topic, user: author)+ op.update_columns(post_number: 1, post_type: Post.types[:regular])+ whisper = Fabricate(:post, topic: topic, user: admin)+ whisper.update_columns(post_number: 2, post_type: Post.types[:whisper])+ small_action!(3)++ # pre-fix inflated state: small action bumped both counters.+ topic.update_columns(highest_post_number: 3, highest_staff_post_number: 3, posts_count: 2)+ staff_tracker = Fabricate(:topic_user, topic: topic, user: admin, last_read_post_number: 3)+ reader_tracker = Fabricate(:topic_user, topic: topic, user: reader, last_read_post_number: 3)++ migrate++ topic.reload+ expect(topic.highest_post_number).to eq(1) # excludes whisper (2) and small action (3)+ expect(topic.highest_staff_post_number).to eq(2) # excludes small action, keeps whisper++ # whisperer keeps progress against the whisper; regular reader is clamped to public highest.+ expect(staff_tracker.reload.last_read_post_number).to eq(2)+ expect(reader_tracker.reload.last_read_post_number).to eq(1)+ end++ it "is idempotent and leaves already-correct topics untouched" do+ op = Fabricate(:post, topic: topic, user: author)+ op.update_columns(post_number: 1, post_type: Post.types[:regular])+ small_action!(2)+ topic.update_columns(highest_post_number: 1, highest_staff_post_number: 1, posts_count: 1)++ expect { migrate }.not_to raise_error+ migrate # second run is a no-op++ topic.reload+ expect(topic.highest_post_number).to eq(1)+ expect(topic.posts_count).to eq(1)+ end+enddiff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rbindex 13d47521b17..5b76b42f707 100644--- a/spec/models/category_spec.rb+++ b/spec/models/category_spec.rb@@ -1072,8 +1072,7 @@ RSpec.describe Category doexpect(category.auto_bump_topic!).to eq(true)expect(Topic.where(bumped_at: time).count).to eq(1)- # our extra bump message- expect(post1.topic.reload.posts_count).to eq(2)+ expect(post1.topic.reload.posts_count).to eq(1)time = freeze_time 13.hours.from_nowdiff --git a/spec/models/private_message_topic_tracking_state_spec.rb b/spec/models/private_message_topic_tracking_state_spec.rbindex 39c72a0f7cc..5a7f5c7e577 100644--- a/spec/models/private_message_topic_tracking_state_spec.rb+++ b/spec/models/private_message_topic_tracking_state_spec.rb@@ -164,18 +164,7 @@ RSpec.describe PrivateMessageTopicTrackingState doexpect(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)-+ it "does not publish small_action posts" dosmall_action =Fabricate(:post,@@ -187,9 +176,7 @@ RSpec.describe PrivateMessageTopicTrackingState domessages = 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)+ expect(messages).to eq([])endenddiff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex 509e024f9b6..52a7d635acf 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -3566,6 +3566,13 @@ RSpec.describe Topic doexpect(Topic.reset_highest(topic.id)).to eq(2)end++ it "excludes small action posts from both the public and staff highest post number" do+ third_post.update!(post_type: Post.types[:small_action])++ expect(Topic.reset_highest(topic.id)).to eq(2)+ expect(topic.reload.highest_staff_post_number).to eq(2)+ endenddescribe "#update_statistics!" dodiff --git a/spec/models/topic_tracking_state_spec.rb b/spec/models/topic_tracking_state_spec.rbindex c0aa90daa65..7f7eddb28ab 100644--- a/spec/models/topic_tracking_state_spec.rb+++ b/spec/models/topic_tracking_state_spec.rb@@ -815,6 +815,20 @@ RSpec.describe TopicTrackingState doexpect(TopicTrackingState.report(user)).to be_emptyend+ it "does not report a topic as unread when its only new post is a small action" do+ TopicUser.change(+ user.id,+ topic.id,+ notification_level: TopicUser.notification_levels[:tracking],+ last_read_post_number: 1,+ )++ topic.add_small_action(Discourse.system_user, "closed.enabled")++ expect(topic.reload.highest_post_number).to eq(1)+ expect(TopicTrackingState.report(user).map(&:topic_id)).not_to include(topic.id)+ end+describe ".report" doit "correctly reports topics with staff posts" doSiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"diff --git a/spec/services/topic_status_updater_spec.rb b/spec/services/topic_status_updater_spec.rbindex 4615ff4b494..3f2b2baaf30 100644--- a/spec/services/topic_status_updater_spec.rb+++ b/spec/services/topic_status_updater_spec.rb@@ -24,7 +24,7 @@ RSpec.describe TopicStatusUpdater doexpect(post.topic.posts.count).to eq(2)tu = TopicUser.find_by(user_id: user.id)- expect(tu.last_read_post_number).to eq(2)+ expect(tu.last_read_post_number).to eq(1)endit "respects topics_unread_when_closed preference for private messages" dodiff --git a/spec/system/topic_bulk_select_spec.rb b/spec/system/topic_bulk_select_spec.rbindex 9f1d65e58bc..a42a0dafb8c 100644--- a/spec/system/topic_bulk_select_spec.rb+++ b/spec/system/topic_bulk_select_spec.rb@@ -292,9 +292,10 @@ describe "Topic bulk select" doexpect(topic_list).to have_closed_status(topics.third)end- # Check that the user did receive a new post notification badge+ # The bulk close posts a small action, which no longer marks the topic+ # unread, so the watching user does not get a new post notification badge.visit("/latest")- expect(topic_list).to have_unread_badge(topics.third)+ expect(topic_list).to have_no_unread_badge(topics.third)endit "closes topics silently" dodiff --git a/spec/system/topics_unread_when_closed_spec.rb b/spec/system/topics_unread_when_closed_spec.rbindex 4280cc79cd7..8be322e4bb6 100644--- a/spec/system/topics_unread_when_closed_spec.rb+++ b/spec/system/topics_unread_when_closed_spec.rb@@ -25,7 +25,7 @@ describe "Topics unread when closed" doexpect(topic_list).to have_no_unread_badge(topics.third)end- it "close notifications appear when enabled (the default)" do+ it "close notifications do not appear even when enabled (the default)" douser.user_option.update!(topics_unread_when_closed: true)sign_in(user)topic = topics.third@@ -36,9 +36,10 @@ describe "Topics unread when closed" do# Close the topic as an adminTopicStatusUpdater.new(topic, admin).update!("closed", true)- # Check that the user did receive a new post notification badge+ # The close action is a small action, which no longer marks a topic unread,+ # so the badge does not appear regardless of the topics_unread_when_closed preference.visit("/latest")- expect(topic_list).to have_unread_badge(topics.third)+ expect(topic_list).to have_no_unread_badge(topics.third)endendend