A platform for community discussion. Free, open, simple.
FIX: Exclude unlisted topics from hot scores (#37312)
Sam6c0afb4a5440
diff --git a/app/models/topic_hot_score.rb b/app/models/topic_hot_score.rbindex 90adecda238..96299df64ac 100644--- a/app/models/topic_hot_score.rb+++ b/app/models/topic_hot_score.rb@@ -11,7 +11,11 @@ class TopicHotScore < ActiveRecord::Baseenddef self.recreate_hottest_topic_ids_cache- hot_topics = Topic.where(archetype: Archetype.default).where.not(id: Category.topic_ids)+ hot_topics =+ Topic+ .where(archetype: Archetype.default)+ .where(visible: true)+ .where.not(id: Category.topic_ids)# 10% of the topics with activity since the hot topics cutoff.limit =@@ -77,6 +81,7 @@ class TopicHotScore < ActiveRecord::BaseAND topics.deleted_at IS NULLAND topics.archetype <> :private_messageAND topics.created_at <= :now+ AND topics.visible = trueORDER BYCASE WHEN topics.pinned_at IS NOT NULL THEN 0 ELSE 1 END ASC,topics.bumped_at desc@@ -118,6 +123,7 @@ class TopicHotScore < ActiveRecord::Basep.created_at >= :recent_cutoffAND t.archetype <> 'private_message'AND t.deleted_at IS NULL+ AND t.visible = trueAND p.deleted_at IS NULLAND p.user_deleted = falseAND t.created_at <= :now@@ -154,17 +160,19 @@ class TopicHotScore < ActiveRecord::Base) X)UPDATE topic_hot_scores ths- SET score = (- CASE WHEN topics.created_at > :recent_cutoff- THEN ths.recent_likes ELSE topics.like_count END- ) /- (EXTRACT(EPOCH FROM (:now - topics.created_at)) / 3600 + 2) ^ :gravity- +- CASE WHEN ths.recent_first_bumped_at IS NULL THEN 0 ELSE- (ths.recent_likes + ths.recent_posters - 1) /- (EXTRACT(EPOCH FROM (:now - recent_first_bumped_at)) / 3600 + 2) ^ :gravity- END- ,+ SET score = CASE WHEN topics.visible = false THEN 0 ELSE+ ((+ CASE WHEN topics.created_at > :recent_cutoff+ THEN ths.recent_likes ELSE topics.like_count END+ ) /+ (EXTRACT(EPOCH FROM (:now - topics.created_at)) / 3600 + 2) ^ :gravity+ ++ CASE WHEN ths.recent_first_bumped_at IS NULL THEN 0 ELSE+ (ths.recent_likes + ths.recent_posters - 1) /+ (EXTRACT(EPOCH FROM (:now - recent_first_bumped_at)) / 3600 + 2) ^ :gravity+ END+ ) * CASE WHEN topics.closed THEN 0.5 ELSE 1 END+ END,updated_at = :nowFROM topicsdiff --git a/app/services/topic_status_updater.rb b/app/services/topic_status_updater.rbindex 528b67280cd..b75598e51f6 100644--- a/app/services/topic_status_updater.rb+++ b/app/services/topic_status_updater.rb@@ -85,6 +85,8 @@ TopicStatusUpdater =CategoryFeaturedTopic.where(topic_id: topic.id).delete_allend+ TopicHotScore.where(topic_id: topic.id).delete_all if status.visible? && status.disabled?+resultenddiff --git a/db/migrate/20260126204830_cleanup_unlisted_topic_hot_scores.rb b/db/migrate/20260126204830_cleanup_unlisted_topic_hot_scores.rbnew file mode 100644index 00000000000..4a79f509434--- /dev/null+++ b/db/migrate/20260126204830_cleanup_unlisted_topic_hot_scores.rb@@ -0,0 +1,15 @@+# frozen_string_literal: true+class CleanupUnlistedTopicHotScores < ActiveRecord::Migration[8.0]+ def up+ execute <<~SQL+ DELETE FROM topic_hot_scores+ WHERE topic_id IN (+ SELECT id FROM topics WHERE visible = false+ )+ SQL+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/spec/models/topic_hot_scores_spec.rb b/spec/models/topic_hot_scores_spec.rbindex 3fbdaed1d72..50c8c47c964 100644--- a/spec/models/topic_hot_scores_spec.rb+++ b/spec/models/topic_hot_scores_spec.rb@@ -193,5 +193,62 @@ RSpec.describe TopicHotScore doexpect(hottest_ids).not_to include(category_topic.id)end++ it "applies faster decay to closed topics" do+ freeze_time++ open_topic = Fabricate(:topic, like_count: 10, created_at: 2.weeks.ago)+ closed_topic = Fabricate(:topic, like_count: 10, created_at: 2.weeks.ago, closed: true)++ TopicHotScore.update_scores++ open_score = TopicHotScore.find_by(topic_id: open_topic.id).score+ closed_score = TopicHotScore.find_by(topic_id: closed_topic.id).score++ expect(closed_score).to be < open_score+ expect(closed_score).to be_within(0.0001).of(open_score * 0.5)+ end++ it "does not create hot scores for unlisted topics" do+ freeze_time++ unlisted_topic = Fabricate(:topic, visible: false, like_count: 10, created_at: 1.day.ago)++ TopicHotScore.update_scores++ expect(TopicHotScore.find_by(topic_id: unlisted_topic.id)).to be_nil+ end++ it "excludes unlisted topics from hottest_topic_ids cache" do+ freeze_time++ unlisted_topic =+ Fabricate(+ :topic,+ visible: false,+ like_count: 100,+ created_at: 1.day.ago,+ last_posted_at: 10.minutes.ago,+ )+ TopicHotScore.create!(topic_id: unlisted_topic.id, score: 999.0)++ TopicHotScore.recreate_hottest_topic_ids_cache++ expect(TopicHotScore.hottest_topic_ids).not_to include(unlisted_topic.id)+ end++ it "sets score to 0 for unlisted topics during update" do+ freeze_time++ topic = Fabricate(:topic, like_count: 10, created_at: 2.weeks.ago)+ TopicHotScore.update_scores++ expect(TopicHotScore.find_by(topic_id: topic.id).score).to be > 0++ topic.update!(visible: false)+ TopicHotScore.update_scores++ expect(TopicHotScore.find_by(topic_id: topic.id).score).to eq(0)+ endendenddiff --git a/spec/services/topic_status_updater_spec.rb b/spec/services/topic_status_updater_spec.rbindex 572d84b24d9..1e579bc3f99 100644--- a/spec/services/topic_status_updater_spec.rb+++ b/spec/services/topic_status_updater_spec.rb@@ -269,5 +269,14 @@ RSpec.describe TopicStatusUpdater doexpect(topic.visible).to eq(true)expect(topic.visibility_reason_id).to eq(Topic.visibility_reasons[:manually_relisted])end++ it "deletes hot score when topic is unlisted" do+ topic = Fabricate(:topic)+ TopicHotScore.create!(topic_id: topic.id, score: 1.0)++ TopicStatusUpdater.new(topic, admin).update!("visible", false)++ expect(TopicHotScore.find_by(topic_id: topic.id)).to be_nil+ endendend