FIX: Allow searching for unsolved posts with tags (#284)

When using the `status:unsolved` search filter, the plugin was only
returning results from topics in categories where solved was enabled.

This commit changes the search query to also include topics with tags
that have solved enabled via the `enable_solved_tags` site setting.

This fixes the `status:unsolved tags:tag1` search query.
This commit is contained in:
Penar Musaraj 2024-03-28 16:46:22 -04:00 committed by GitHub
parent 4c6ddcf08d
commit a18ce6d712
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 5 deletions

View File

@ -368,20 +368,29 @@ after_initialize do
)", )",
) )
else else
tag_ids = Tag.where(name: SiteSetting.enable_solved_tags.split("|")).pluck(:id)
posts.where( posts.where(
"topics.id NOT IN ( "topics.id NOT IN (
SELECT tc.topic_id SELECT tc.topic_id
FROM topic_custom_fields tc FROM topic_custom_fields tc
WHERE tc.name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}' AND WHERE tc.name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}' AND
tc.value IS NOT NULL tc.value IS NOT NULL
) AND topics.id IN ( ) AND (topics.id IN (
SELECT top.id SELECT top.id
FROM topics top FROM topics top
INNER JOIN category_custom_fields cc INNER JOIN category_custom_fields cc
ON top.category_id = cc.category_id ON top.category_id = cc.category_id
WHERE cc.name = '#{::DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD}' AND WHERE cc.name = '#{::DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD}' AND
cc.value = 'true' cc.value = 'true'
)", ) OR topics.id IN (
SELECT top.id
FROM topics top
INNER JOIN topic_tags tt
ON top.id = tt.topic_id
WHERE tt.tag_id IN (?)
))",
tag_ids,
) )
end end
end end

View File

@ -63,6 +63,7 @@ RSpec.describe "Managing Posts solved status" do
category_custom_field.save category_custom_field.save
category category
end end
fab!(:tag)
fab!(:topic_unsolved) do fab!(:topic_unsolved) do
Fabricate( Fabricate(
:custom_topic, :custom_topic,
@ -71,6 +72,7 @@ RSpec.describe "Managing Posts solved status" do
custom_topic_name: ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD, custom_topic_name: ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD,
) )
end end
fab!(:topic_unsolved_2) { Fabricate(:topic, user: user, tags: [tag]) }
fab!(:topic_solved) do fab!(:topic_solved) do
Fabricate( Fabricate(
:custom_topic, :custom_topic,
@ -96,6 +98,7 @@ RSpec.describe "Managing Posts solved status" do
) )
end end
fab!(:post_unsolved) { Fabricate(:post, topic: topic_unsolved) } fab!(:post_unsolved) { Fabricate(:post, topic: topic_unsolved) }
fab!(:post_unsolved_2) { Fabricate(:post, topic: topic_unsolved_2) }
fab!(:post_solved) do fab!(:post_solved) do
post = Fabricate(:post, topic: topic_solved) post = Fabricate(:post, topic: topic_solved)
DiscourseSolved.accept_answer!(post, Discourse.system_user) DiscourseSolved.accept_answer!(post, Discourse.system_user)
@ -105,10 +108,12 @@ RSpec.describe "Managing Posts solved status" do
fab!(:post_disabled_2) { Fabricate(:post, topic: topic_disabled_2) } fab!(:post_disabled_2) { Fabricate(:post, topic: topic_disabled_2) }
before do before do
SiteSetting.enable_solved_tags = tag.name
SearchIndexer.enable SearchIndexer.enable
Jobs.run_immediately! Jobs.run_immediately!
SearchIndexer.index(topic_unsolved, force: true) SearchIndexer.index(topic_unsolved, force: true)
SearchIndexer.index(topic_unsolved_2, force: true)
SearchIndexer.index(topic_solved, force: true) SearchIndexer.index(topic_solved, force: true)
SearchIndexer.index(topic_disabled_1, force: true) SearchIndexer.index(topic_disabled_1, force: true)
SearchIndexer.index(topic_disabled_2, force: true) SearchIndexer.index(topic_disabled_2, force: true)
@ -120,17 +125,23 @@ RSpec.describe "Managing Posts solved status" do
describe "when allow solved on all topics is disabled" do describe "when allow solved on all topics is disabled" do
before { SiteSetting.allow_solved_on_all_topics = false } before { SiteSetting.allow_solved_on_all_topics = false }
it "only returns posts where 'Allow topic owner and staff to mark a reply as the solution' is enabled and post is not solved" do it "only returns unsolved posts from categories and tags where solving is enabled" do
result = Search.execute("status:unsolved") result = Search.execute("status:unsolved")
expect(result.posts.pluck(:id)).to match_array([post_unsolved.id]) expect(result.posts.pluck(:id)).to match_array([post_unsolved.id, post_unsolved_2.id])
end
it "returns the filtered results when combining search with a tag" do
result = Search.execute("status:unsolved tag:#{tag.name}")
expect(result.posts.pluck(:id)).to match_array([post_unsolved_2.id])
end end
end end
describe "when allow solved on all topics is enabled" do describe "when allow solved on all topics is enabled" do
before { SiteSetting.allow_solved_on_all_topics = true } before { SiteSetting.allow_solved_on_all_topics = true }
it "only returns posts where the post is not solved" do it "only returns posts where the post is not solved" do
result = Search.execute("status:unsolved") result = Search.execute("status:unsolved")
expect(result.posts.pluck(:id)).to match_array( expect(result.posts.pluck(:id)).to match_array(
[post_unsolved.id, post_disabled_1.id, post_disabled_2.id], [post_unsolved.id, post_unsolved_2.id, post_disabled_1.id, post_disabled_2.id],
) )
end end
end end