Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 6932262

Browse files
committed
Avoid loading summaries for other topic lists
1 parent 650a6e8 commit 6932262

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

lib/summarization/entry_point.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,24 @@ def inject_into(plugin)
3333
:topic_list_item,
3434
:ai_topic_gist,
3535
include_condition: -> do
36+
# Hack(roman): Not ideal but at this point I don't have a better way of knowing if I'm serializing items for the hot filter.
37+
# If the association wasn't loaded, assume don't care about summaries, and including it anyway will result in multiple n+1 queries.
38+
# In the future, the serializer could have more informacion about the topic list, so we don't depend on this.
3639
SiteSetting.ai_summarization_enabled &&
37-
SiteSetting.ai_summarize_max_hot_topics_gists_per_batch > 0
40+
SiteSetting.ai_summarize_max_hot_topics_gists_per_batch > 0 &&
41+
object.ai_summaries.loaded?
3842
end,
39-
) { object.ai_summaries.to_a.first&.summarized_text }
43+
) do
44+
summaries = object.ai_summaries.to_a
45+
46+
# Summaries should always have one element here.
47+
# This is an extra safeguard to avoid including regular summaries.
48+
summaries.find { |s| s.summary_type == "gist" }&.summarized_text
49+
end
4050

4151
# To make sure hot topic gists are inmediately up to date, we rely on this event
4252
# instead of using a scheduled job.
43-
plugin.on(:topic_hot_scores_updated) { Jobs.enqueue(:summarize_hot_topics_batch) }
53+
plugin.on(:topic_hot_scores_updated) { Jobs.enqueue(:hot_topics_gist_batch) }
4454
end
4555
end
4656
end

spec/lib/modules/summarization/entry_point_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,26 @@
5959

6060
expect(serialized[:ai_topic_gist]).to be_present
6161
end
62+
63+
it "doesn't include the summary when looking at other topic lists" do
64+
gist_topic = topic_query.list_latest.topics.find { |t| t.id == topic_ai_gist.target_id }
65+
66+
serialized =
67+
TopicListItemSerializer.new(gist_topic, scope: Guardian.new, root: false).as_json
68+
69+
expect(serialized[:ai_topic_gist]).to be_nil
70+
end
6271
end
6372
end
6473
end
6574
end
75+
76+
describe "#on topic_hot_scores_updated" do
77+
it "queues a job to generate gists" do
78+
expect { DiscourseEvent.trigger(:topic_hot_scores_updated) }.to change(
79+
Jobs::HotTopicsGistBatch.jobs,
80+
:size,
81+
).by(1)
82+
end
83+
end
6684
end

0 commit comments

Comments
 (0)