FIX: Only show public visible topics as suggested for anons (#27)

* FIX: Only show public visible topics as suggested for anons

* DEV: Add tests for embeddings

* Update spec/lib/modules/embeddings/semantic_suggested_spec.rb

Co-authored-by: Bianca Nenciu <nbianca@users.noreply.github.com>

* Update spec/lib/modules/embeddings/semantic_suggested_spec.rb

Co-authored-by: Bianca Nenciu <nbianca@users.noreply.github.com>

* move to top

---------

Co-authored-by: Bianca Nenciu <nbianca@users.noreply.github.com>
This commit is contained in:
Rafael dos Santos Silva 2023-03-23 17:28:01 -03:00 committed by GitHub
parent 4c960970fa
commit 45950f1bb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 1 deletions

View File

@ -33,7 +33,12 @@ module DiscourseAi
# array_position forces the order of the topics to be preserved
candidates =
::Topic.where(id: candidate_ids).order("array_position(ARRAY#{candidate_ids}, id)")
::Topic
.visible
.listable_topics
.secured
.where(id: candidate_ids)
.order("array_position(ARRAY#{candidate_ids}, id)")
{ result: candidates, params: {} }
end

View File

@ -0,0 +1,31 @@
# frozen_string_literal: true
require "rails_helper"
describe DiscourseAi::Embeddings::EntryPoint do
fab!(:user) { Fabricate(:user) }
describe "registering event callbacks" do
context "when creating a topic" do
let(:creator) do
PostCreator.new(
user,
raw: "this is the new content for my topic",
title: "this is my new topic title",
)
end
it "queues a job on create if embeddings is enabled" do
SiteSetting.ai_embeddings_enabled = true
expect { creator.create }.to change(Jobs::GenerateEmbeddings.jobs, :size).by(1)
end
it "does nothing if sentiment analysis is disabled" do
SiteSetting.ai_embeddings_enabled = false
expect { creator.create }.not_to change(Jobs::GenerateEmbeddings.jobs, :size)
end
end
end
end

View File

@ -0,0 +1,38 @@
# frozen_string_literal: true
require "rails_helper"
describe DiscourseAi::Embeddings::SemanticSuggested do
fab!(:target) { Fabricate(:topic) }
fab!(:normal_topic_1) { Fabricate(:topic) }
fab!(:normal_topic_2) { Fabricate(:topic) }
fab!(:normal_topic_3) { Fabricate(:topic) }
fab!(:unlisted_topic) { Fabricate(:topic, visible: false) }
fab!(:private_topic) { Fabricate(:private_message_topic) }
fab!(:secured_category) { Fabricate(:category, read_restricted: true) }
fab!(:secured_category_topic) { Fabricate(:topic, category: secured_category) }
before { SiteSetting.ai_embeddings_semantic_suggested_topics_anons_enabled = true }
describe "#build_suggested_topics" do
before do
Discourse.cache.clear
described_class.stubs(:search_suggestions).returns(
Topic.unscoped.order(id: :desc).limit(10).pluck(:id),
)
end
after { Discourse.cache.clear }
it "returns the suggested topics without non public topics" do
suggested = described_class.build_suggested_topics(target, {}, TopicQuery.new(nil))
suggested_results = suggested[:result]
expect(suggested_results).to include(normal_topic_1)
expect(suggested_results).to include(normal_topic_2)
expect(suggested_results).to include(normal_topic_3)
expect(suggested_results).to_not include(unlisted_topic)
expect(suggested_results).to_not include(private_topic)
expect(suggested_results).to_not include(secured_category_topic)
end
end
end