DEV: Add apply_modifier for SemanticTopicQuery topics list (#830)
This commit is contained in:
parent
c5b323fc07
commit
52d90cf1bc
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue