FEATURE: Return only applicable suggestions in AiHelper category/tags suggestions (#184)

This commit is contained in:
Rafael dos Santos Silva 2023-09-04 14:30:33 -03:00 committed by GitHub
parent 3c4a53b2cb
commit 4864978495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -72,7 +72,11 @@ module DiscourseAi
RateLimiter.new(current_user, "ai_assistant", 6, 3.minutes).performed!
render json: DiscourseAi::AiHelper::SemanticCategorizer.new(params[:text]).categories,
render json:
DiscourseAi::AiHelper::SemanticCategorizer.new(
params[:text],
current_user,
).categories,
status: 200
end
@ -81,7 +85,9 @@ module DiscourseAi
RateLimiter.new(current_user, "ai_assistant", 6, 3.minutes).performed!
render json: DiscourseAi::AiHelper::SemanticCategorizer.new(params[:text]).tags, status: 200
render json:
DiscourseAi::AiHelper::SemanticCategorizer.new(params[:text], current_user).tags,
status: 200
end
private

View File

@ -2,7 +2,8 @@
module DiscourseAi
module AiHelper
class SemanticCategorizer
def initialize(text)
def initialize(text, user)
@user = user
@text = text
end
@ -22,6 +23,7 @@ module DiscourseAi
::Topic
.joins(:category)
.where(id: candidate_ids)
.where("categories.id IN (?)", Category.topic_create_allowed(@user.guardian).pluck(:id))
.order("array_position(ARRAY#{candidate_ids}, topics.id)")
.pluck("categories.slug")
.map
@ -52,6 +54,7 @@ module DiscourseAi
::Topic
.joins(:topic_tags, :tags)
.where(id: candidate_ids)
.where("tags.id IN (?)", DiscourseTagging.visible_tags(@user.guardian).pluck(:id))
.group("topics.id")
.order("array_position(ARRAY#{candidate_ids}, topics.id)")
.pluck("array_agg(tags.name)")