DEV: Add apply_modifier for SemanticTopicQuery topics list (#830)

This commit is contained in:
Mark VanLandingham 2024-10-10 12:13:16 -05:00 committed by GitHub
parent c5b323fc07
commit 52d90cf1bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 5 deletions

View File

@ -16,12 +16,12 @@ class DiscourseAi::Embeddings::SemanticTopicQuery < TopicQuery
create_list(:semantic_related, query_opts) do |topics|
candidate_ids = DiscourseAi::Embeddings::SemanticRelated.new.related_topic_ids_for(topic)
list =
topics
.where.not(id: topic.id)
.where(id: candidate_ids)
.order("array_position(ARRAY#{candidate_ids}, topics.id)") # array_position forces the order of the topics to be preserved
list = topics.where.not(id: topic.id).where(id: candidate_ids)
list = DiscoursePluginRegistry.apply_modifier(:semantic_related_topics_query, list)
# array_position forces the order of the topics to be preserved
list = list.order("array_position(ARRAY#{candidate_ids}, topics.id)")
list = remove_muted(list, @user, query_opts)
end
end

View File

@ -112,6 +112,28 @@ describe DiscourseAi::Embeddings::EntryPoint do
)
end
end
context "with semantic_related_topics_query modifier registered" do
fab!(:included_topic) { Fabricate(:topic) }
fab!(:excluded_topic) { Fabricate(:topic) }
before { stub_semantic_search_with([included_topic.id, excluded_topic.id]) }
let(:modifier_block) { Proc.new { |query| query.where.not(id: excluded_topic.id) } }
it "Allows modifications to default results (excluding a topic in this case)" do
plugin_instance = Plugin::Instance.new
plugin_instance.register_modifier(:semantic_related_topics_query, &modifier_block)
expect(topic_query.list_semantic_related_topics(target).topics).to eq([included_topic])
ensure
DiscoursePluginRegistry.unregister_modifier(
plugin_instance,
:semantic_related_topics_query,
&modifier_block
)
end
end
end
end
end