Skip to content

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

DEV: TopicListItemSerializer tells if topic is among the top 100 hottest topics (#31935)

Roman Rizzi9bc394191c4b
diff --git a/app/models/theme_modifier_set.rb b/app/models/theme_modifier_set.rb
index 218f254e150..bff4604deb7 100644
--- a/app/models/theme_modifier_set.rb
+++ b/app/models/theme_modifier_set.rb
@@ -150,6 +150,7 @@ end
# serialize_post_user_badges :string is an Array
# theme_setting_modifiers :jsonb
# serialize_topic_op_likes_data :boolean
+# serialize_topic_is_hot :boolean
#
# Indexes
#
diff --git a/app/models/topic_hot_score.rb b/app/models/topic_hot_score.rb
index a58fb6d8837..1783e0d0aa5 100644
--- a/app/models/topic_hot_score.rb
+++ b/app/models/topic_hot_score.rb
@@ -5,6 +5,21 @@ class TopicHotScore < ActiveRecord::Base
DEFAULT_BATCH_SIZE = 1000
+ def self.hot_topic_ids_cache
+ @hot_topic_ids_cache ||= DistributedCache.new("hot_topic_ids_cache")
+ end
+
+ def self.hottest_topic_ids
+ hot_topic_ids_cache["ids"].presence || Set.new
+ end
+
+ def self.recreate_hottest_topic_ids_cache
+ hot_topic_ids_cache.defer_set(
+ "ids",
+ DB.query_single("SELECT topic_id FROM topic_hot_scores ORDER BY score DESC LIMIT 100").to_set,
+ )
+ end
+
def self.update_scores(max = DEFAULT_BATCH_SIZE)
# score is
# (total likes - 1) / (age in hours + 2) ^ gravity
@@ -146,6 +161,8 @@ class TopicHotScore < ActiveRecord::Base
DB.exec(sql, args)
+ recreate_hottest_topic_ids_cache
+
DiscourseEvent.trigger(:topic_hot_scores_updated)
end
end
diff --git a/app/serializers/topic_list_item_serializer.rb b/app/serializers/topic_list_item_serializer.rb
index 3230b198dcb..2734c91b246 100644
--- a/app/serializers/topic_list_item_serializer.rb
+++ b/app/serializers/topic_list_item_serializer.rb
@@ -18,7 +18,8 @@ class TopicListItemSerializer < ListableTopicSerializer
:featured_link,
:featured_link_root_domain,
:allowed_user_count,
- :participant_groups
+ :participant_groups,
+ :is_hot
has_many :posters, serializer: TopicPosterSerializer, embed: :objects
has_many :participants, serializer: TopicPosterSerializer, embed: :objects
@@ -142,6 +143,17 @@ class TopicListItemSerializer < ListableTopicSerializer
object.private_message?
end
+ def is_hot
+ TopicHotScore.hottest_topic_ids.include?(object.id)
+ end
+
+ def include_is_hot?
+ theme_enabled = theme_modifier_helper.serialize_topic_is_hot
+ plugin_enabled = DiscoursePluginRegistry.apply_modifier(:serialize_topic_is_hot, false)
+
+ theme_enabled || plugin_enabled
+ end
+
private
def serialize_topic_op_likes_data_enabled?
diff --git a/db/migrate/20250321143553_add_serialize_topic_is_hot_theme_modifier.rb b/db/migrate/20250321143553_add_serialize_topic_is_hot_theme_modifier.rb
new file mode 100644
index 00000000000..0bb699a06eb
--- /dev/null
+++ b/db/migrate/20250321143553_add_serialize_topic_is_hot_theme_modifier.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class AddSerializeTopicIsHotThemeModifier < ActiveRecord::Migration[7.2]
+ def change
+ add_column :theme_modifier_sets, :serialize_topic_is_hot, :boolean, null: true
+ end
+end
diff --git a/spec/models/topic_hot_scores_spec.rb b/spec/models/topic_hot_scores_spec.rb
index 77759b53188..3acf9db5d6e 100644
--- a/spec/models/topic_hot_scores_spec.rb
+++ b/spec/models/topic_hot_scores_spec.rb
@@ -6,6 +6,8 @@ RSpec.describe TopicHotScore do
fab!(:user2) { Fabricate(:user) }
fab!(:user3) { Fabricate(:user) }
+ after { TopicHotScore.hot_topic_ids_cache.clear }
+
it "also always updates based on recent activity" do
freeze_time
@@ -112,6 +114,21 @@ RSpec.describe TopicHotScore do
expect(TopicHotScore.find_by(topic_id: topic2.id).score).to be_within(0.001).of(0.001)
end
+ it "caches top 100 topic IDs" do
+ topic1 = Fabricate(:topic, like_count: 3)
+ topic2 = Fabricate(:topic, like_count: 10)
+
+ TopicHotScore.update_scores
+
+ expect(TopicHotScore.hottest_topic_ids).to contain_exactly(topic2.id, topic1.id)
+
+ topic3 = Fabricate(:topic, like_count: 100)
+
+ TopicHotScore.update_scores
+
+ expect(TopicHotScore.hottest_topic_ids).to contain_exactly(topic3.id, topic2.id, topic1.id)
+ end
+
it "ignores topics in the future" do
freeze_time
diff --git a/spec/serializers/topic_list_item_serializer_spec.rb b/spec/serializers/topic_list_item_serializer_spec.rb
index fd7f3054d4c..ad3697c8fdf 100644
--- a/spec/serializers/topic_list_item_serializer_spec.rb
+++ b/spec/serializers/topic_list_item_serializer_spec.rb
@@ -207,4 +207,71 @@ RSpec.describe TopicListItemSerializer do
DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &proc)
end
end
+
+ describe "#is_hot" do
+ describe "including the attr based on theme modifier or plugin registry" do
+ fab!(:hot_topic) { Fabricate(:topic, like_count: 10) }
+
+ before { TopicHotScore.update_scores }
+ after { TopicHotScore.hot_topic_ids_cache.clear }
+
+ context "without opt-in" do
+ before do
+ allow_any_instance_of(ThemeModifierHelper).to receive(:serialize_topic_is_hot).and_return(
+ false,
+ )
+ end
+
+ it "doesn't includes the attr" do
+ serialized =
+ TopicListItemSerializer.new(hot_topic, scope: Guardian.new, root: false).as_json
+
+ expect(serialized.key?(:is_hot)).to eq(false)
+ end
+ end
+
+ context "when theme modifier opts-in" do
+ before do
+ allow_any_instance_of(ThemeModifierHelper).to receive(:serialize_topic_is_hot).and_return(
+ true,
+ )
+ end
+
+ it "returns true if topic is hot" do
+ serialized =
+ TopicListItemSerializer.new(hot_topic, scope: Guardian.new, root: false).as_json
+
+ expect(serialized[:is_hot]).to eq(true)
+ end
+
+ it "returns false if topic is not hot" do
+ serialized = TopicListItemSerializer.new(topic, scope: Guardian.new, root: false).as_json
+
+ expect(serialized[:is_hot]).to eq(false)
+ end
+ end
+
+ context "when plugin registry opts-in" do
+ let(:modifier) { :serialize_topic_is_hot }
+ let(:proc) { Proc.new { true } }
+ let(:plugin) { Plugin::Instance.new }
+
+ before { DiscoursePluginRegistry.register_modifier(plugin, modifier, &proc) }
+ after { DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &proc) }
+
+ it "returns true if topic is hot" do
+ serialized =
+ TopicListItemSerializer.new(hot_topic, scope: Guardian.new, root: false).as_json
+
+ expect(serialized[:is_hot]).to eq(true)
+ end
+
+ it "returns false if topic is not hot" do
+ serialized = TopicListItemSerializer.new(topic, scope: Guardian.new, root: false).as_json
+
+ expect(serialized[:is_hot]).to eq(false)
+ end
+ end
+ end
+ end
end