FIX: Instruct AR that we want to use ai_summaries for filtering. (#927)

We use `includes` instead of `joins` because we want to eager-load summaries, avoiding an extra query when summarizing. However, Rails will complain unless you explicitly inform them you plan to use that inside a `WHERE` clause.
This commit is contained in:
Roman Rizzi 2024-11-19 17:32:13 -03:00 committed by GitHub
parent dcde94a393
commit 530a795d43
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 4 deletions

View File

@ -20,10 +20,13 @@ module DiscourseAi
plugin.register_modifier(:topic_query_create_list_topics) do |topics, options|
if Discourse.filters.include?(options[:filter]) && SiteSetting.ai_summarization_enabled &&
SiteSetting.ai_summarize_max_hot_topics_gists_per_batch > 0
topics.includes(:ai_summaries).where(
"ai_summaries.id IS NULL OR ai_summaries.summary_type = ?",
AiSummary.summary_types[:gist],
)
topics
.includes(:ai_summaries)
.where(
"ai_summaries.id IS NULL OR ai_summaries.summary_type = ?",
AiSummary.summary_types[:gist],
)
.references(:ai_summaries)
else
topics
end

View File

@ -86,6 +86,43 @@ RSpec.describe DiscourseAi::Summarization::EntryPoint do
expect(serialized[:ai_topic_gist]).to be_nil
end
it "works when the topic has whispers" do
SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
admin = Fabricate(:admin)
group.add(admin)
# We are testing a scenario where AR could get confused if we don't use `references`.
first = create_post(raw: "this is the first post", title: "super amazing title")
_whisper =
create_post(
topic_id: first.topic.id,
post_type: Post.types[:whisper],
raw: "this is a whispered reply",
)
Fabricate(:topic_ai_gist, target: first.topic)
topic_id = first.topic.id
TopicUser.update_last_read(admin, topic_id, first.post_number, 1, 1)
TopicUser.change(
admin.id,
topic_id,
notification_level: TopicUser.notification_levels[:tracking],
)
gist_topic = TopicQuery.new(admin).list_unread.topics.find { |t| t.id == topic_id }
serialized =
TopicListItemSerializer.new(
gist_topic,
scope: Guardian.new(admin),
root: false,
filter: :hot,
).as_json
expect(serialized[:ai_topic_gist]).to be_present
end
end
end
end