Skip to content

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

FIX: remove category definition topics in hot (#35449)

Régis Hanol28b3cff3f7b6
diff --git a/app/models/topic_hot_score.rb b/app/models/topic_hot_score.rb
index e3ed71df229..90adecda238 100644
--- a/app/models/topic_hot_score.rb
+++ b/app/models/topic_hot_score.rb
@@ -11,23 +11,27 @@ class TopicHotScore < ActiveRecord::Base
end
def self.recreate_hottest_topic_ids_cache
- hot_topics_limit =
- (
- Topic
- .where(archetype: Archetype.default)
- .where("last_posted_at > ?", SiteSetting.hot_topics_recent_days.days.ago)
- .count * 0.1
- ).to_i # 10% of the topics with activity since the hot topics cutoff.
-
- hot_topics_limit = [hot_topics_limit, 100].min # Capped at 100 IDs.
-
- Discourse.cache.write(
- CACHE_KEY,
- DB.query_single(
- "SELECT topic_id FROM topic_hot_scores ORDER BY score DESC LIMIT :limit",
- limit: hot_topics_limit,
- ).to_set,
- )
+ hot_topics = Topic.where(archetype: Archetype.default).where.not(id: Category.topic_ids)
+
+ # 10% of the topics with activity since the hot topics cutoff.
+ limit =
+ hot_topics
+ .where("last_posted_at > ?", SiteSetting.hot_topics_recent_days.days.ago)
+ .count
+ .to_i * 0.1
+
+ # capped at 100 topics
+ limit = [limit, 100].min
+
+ sql = <<~SQL
+ SELECT topic_id
+ FROM topic_hot_scores
+ WHERE topic_id IN (#{hot_topics.select(:id).to_sql})
+ ORDER BY score DESC
+ LIMIT :limit
+ SQL
+
+ Discourse.cache.write(CACHE_KEY, DB.query_single(sql, limit:).to_set)
end
def self.update_scores(max = DEFAULT_BATCH_SIZE)
diff --git a/spec/models/topic_hot_scores_spec.rb b/spec/models/topic_hot_scores_spec.rb
index dc1207d0a91..3fbdaed1d72 100644
--- a/spec/models/topic_hot_scores_spec.rb
+++ b/spec/models/topic_hot_scores_spec.rb
@@ -115,14 +115,20 @@ RSpec.describe TopicHotScore do
end
it "caches 10% of the hottest topic IDs" do
- 9.times { Fabricate(:topic, like_count: 3, last_posted_at: 10.minutes.ago) }
- hottest_topic = Fabricate(:topic, like_count: 10, last_posted_at: 10.minutes.ago)
+ freeze_time
+
+ 9.times do
+ Fabricate(:topic, like_count: 3, created_at: 2.weeks.ago, last_posted_at: 10.minutes.ago)
+ end
+ hottest_topic =
+ Fabricate(:topic, like_count: 10, created_at: 2.weeks.ago, last_posted_at: 10.minutes.ago)
TopicHotScore.update_scores
expect(TopicHotScore.hottest_topic_ids).to contain_exactly(hottest_topic.id)
- hottest_topic_2 = Fabricate(:topic, like_count: 100, last_posted_at: 10.minutes.ago)
+ hottest_topic_2 =
+ Fabricate(:topic, like_count: 100, created_at: 2.weeks.ago, last_posted_at: 10.minutes.ago)
TopicHotScore.update_scores
@@ -156,5 +162,36 @@ RSpec.describe TopicHotScore do
DiscourseEvent.off(:topic_hot_scores_updated, &blk)
end
end
+
+ it "excludes category description topics from hot topics list" do
+ freeze_time
+
+ category = Fabricate(:category_with_definition)
+ category_topic = category.topic
+ category_topic.update!(
+ like_count: 100,
+ created_at: 2.weeks.ago,
+ last_posted_at: 10.minutes.ago,
+ )
+
+ 9.times do
+ Fabricate(:topic, like_count: 3, created_at: 2.weeks.ago, last_posted_at: 10.minutes.ago)
+ end
+
+ regular_topic =
+ Fabricate(:topic, like_count: 10, created_at: 2.weeks.ago, last_posted_at: 10.minutes.ago)
+
+ TopicHotScore.update_scores
+
+ category_score = TopicHotScore.find_by(topic_id: category_topic.id)
+ regular_score = TopicHotScore.find_by(topic_id: regular_topic.id)
+
+ expect(category_score).to be_present
+ expect(category_score.score).to be > regular_score.score
+
+ hottest_ids = TopicHotScore.hottest_topic_ids
+
+ expect(hottest_ids).not_to include(category_topic.id)
+ end
end
end