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:
Bianca Nenciu 2024-04-15 02:31:07 +03:00 committed by GitHub
parent 6090580e36
commit 3e54697c5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 6 deletions

View File

@ -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

View File

@ -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