Skip to content

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

FIX: Unread count could disagree with the /unread topic list (#40473)

Régis Hanol5c131b793474
diff --git a/lib/topic_query.rb b/lib/topic_query.rb
index c1c19ae2a98..00034ade4a3 100644
--- a/lib/topic_query.rb
+++ b/lib/topic_query.rb
@@ -265,6 +265,7 @@ class TopicQuery
topic:,
per_page: builder.results_left,
max_age: SiteSetting.suggested_topics_unread_max_days_old,
+ age_column: :bumped_at,
),
)
else
@@ -273,6 +274,7 @@ class TopicQuery
topic: topic,
per_page: builder.results_left,
max_age: SiteSetting.suggested_topics_unread_max_days_old,
+ age_column: :bumped_at,
),
:high,
)
@@ -1338,7 +1340,11 @@ class TopicQuery
# perf note, in the past we tried doing this in a subquery but performance was
# terrible, also tried with a join and it was bad
- results = results.where("topics.bumped_at >= ?", unread_at)
+ #
+ # Default to updated_at so the unread/new lists stay consistent with the unread
+ # count (TopicTrackingState); suggested topics opt into bumped_at via age_column.
+ age_column = options[:age_column] == :bumped_at ? "bumped_at" : "updated_at"
+ results = results.where("topics.#{age_column} >= ?", unread_at)
end
results
end
diff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rb
index cf984ededa2..f6faf63e66d 100644
--- a/spec/lib/topic_query_spec.rb
+++ b/spec/lib/topic_query_spec.rb
@@ -1275,6 +1275,24 @@ RSpec.describe TopicQuery do
end
end
+ context "with a small action at the tail of an unread topic" do
+ it "keeps the topic in the unread list" do
+ topic = create_post(raw: "the original post", title: "super amazing title").topic
+ topic.add_small_action(Discourse.system_user, "closed.enabled")
+ topic.update_columns(updated_at: Time.zone.now, bumped_at: 1.year.ago)
+
+ TopicUser.change(
+ user.id,
+ topic.id,
+ notification_level: TopicUser.notification_levels[:tracking],
+ )
+ TopicUser.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)
+ end
+ end
+
context "with read data" do
fab!(:partially_read) { Fabricate(:post, user: creator).topic }
fab!(:fully_read) { Fabricate(:post, user: creator).topic }