From 530a795d43b338ea9da768321247f9f2303b8dab Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Tue, 19 Nov 2024 17:32:13 -0300 Subject: [PATCH] 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. --- lib/summarization/entry_point.rb | 11 ++++-- .../modules/summarization/entry_point_spec.rb | 37 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/summarization/entry_point.rb b/lib/summarization/entry_point.rb index c5ad0f8c..ed704a09 100644 --- a/lib/summarization/entry_point.rb +++ b/lib/summarization/entry_point.rb @@ -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 diff --git a/spec/lib/modules/summarization/entry_point_spec.rb b/spec/lib/modules/summarization/entry_point_spec.rb index 0ba22770..f4c01da6 100644 --- a/spec/lib/modules/summarization/entry_point_spec.rb +++ b/spec/lib/modules/summarization/entry_point_spec.rb @@ -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