FIX: Load categories for related topics (#570)
This is necessary when "lazy load categories" feature is enabled to make sure the categories are rendered for all related topics.
This commit is contained in:
parent
6090580e36
commit
3e54697c5a
|
@ -9,7 +9,7 @@ module DiscourseAi
|
||||||
:topic_view_suggested_topics_options,
|
:topic_view_suggested_topics_options,
|
||||||
) do |suggested_options, topic_view|
|
) do |suggested_options, topic_view|
|
||||||
related_topics = topic_view.related_topics
|
related_topics = topic_view.related_topics
|
||||||
include_random = related_topics.nil? || related_topics.length == 0
|
include_random = !related_topics || related_topics.topics.length == 0
|
||||||
suggested_options.merge(include_random: include_random)
|
suggested_options.merge(include_random: include_random)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -20,10 +20,16 @@ module DiscourseAi
|
||||||
end
|
end
|
||||||
|
|
||||||
@related_topics ||=
|
@related_topics ||=
|
||||||
::DiscourseAi::Embeddings::SemanticTopicQuery
|
::DiscourseAi::Embeddings::SemanticTopicQuery.new(@user).list_semantic_related_topics(
|
||||||
.new(@user)
|
topic,
|
||||||
.list_semantic_related_topics(topic)
|
)
|
||||||
.topics
|
end
|
||||||
|
|
||||||
|
# define_method must be used (instead of add_to_class) to make sure
|
||||||
|
# that method still works when plugin is disabled too
|
||||||
|
TopicView.alias_method(:categories_old, :categories)
|
||||||
|
TopicView.define_method(:categories) do
|
||||||
|
@categories ||= [*categories_old, *related_topics&.categories].flatten.uniq.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
%i[topic_view TopicViewPosts].each do |serializer|
|
%i[topic_view TopicViewPosts].each do |serializer|
|
||||||
|
@ -33,7 +39,7 @@ module DiscourseAi
|
||||||
include_condition: -> { SiteSetting.ai_embeddings_semantic_related_topics_enabled },
|
include_condition: -> { SiteSetting.ai_embeddings_semantic_related_topics_enabled },
|
||||||
) do
|
) do
|
||||||
if object.next_page.nil? && !object.topic.private_message?
|
if object.next_page.nil? && !object.topic.private_message?
|
||||||
object.related_topics.map do |t|
|
object.related_topics.topics.map do |t|
|
||||||
SuggestedTopicSerializer.new(t, scope: scope, root: false)
|
SuggestedTopicSerializer.new(t, scope: scope, root: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -34,6 +34,23 @@ describe ::TopicsController do
|
||||||
expect(json["suggested_topics"].length).to eq(0)
|
expect(json["suggested_topics"].length).to eq(0)
|
||||||
expect(json["related_topics"].length).to eq(2)
|
expect(json["related_topics"].length).to eq(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "includes related topics in payload when configured" do
|
||||||
|
SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}"
|
||||||
|
category = Fabricate(:category)
|
||||||
|
topic.update!(category: category)
|
||||||
|
|
||||||
|
get("#{topic.relative_url}.json")
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
json = response.parsed_body
|
||||||
|
|
||||||
|
expect(json["suggested_topics"].length).to eq(0)
|
||||||
|
expect(json["related_topics"].length).to eq(2)
|
||||||
|
expect(json["categories"].map { |c| c["id"] }).to contain_exactly(
|
||||||
|
SiteSetting.uncategorized_category_id,
|
||||||
|
category.id,
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "crawler" do
|
describe "crawler" do
|
||||||
|
|
Loading…
Reference in New Issue