Skip to content

Commit

Permalink
FIX: Allow searching for unsolved posts with tags
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pmusaraj committed Mar 28, 2024
1 parent 4c6ddcf commit 9b500aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
12 changes: 10 additions & 2 deletions plugin.rb
Expand Up @@ -368,20 +368,28 @@ def self.skip_db?
)",
)
else
tag_ids = Tag.where(name: SiteSetting.enable_solved_tags.split("|")).pluck(:id)

posts.where(
"topics.id NOT IN (
SELECT tc.topic_id
FROM topic_custom_fields tc
WHERE tc.name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}' AND
tc.value IS NOT NULL
) AND topics.id IN (
) AND (topics.id IN (
SELECT top.id
FROM topics top
INNER JOIN category_custom_fields cc
ON top.category_id = cc.category_id
WHERE cc.name = '#{::DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD}' AND
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
Expand Down
23 changes: 20 additions & 3 deletions spec/integration/solved_spec.rb
Expand Up @@ -63,6 +63,7 @@
category_custom_field.save
category
end
fab!(:tag) { Fabricate(:tag, name: "tag1") }
fab!(:topic_unsolved) do
Fabricate(
:custom_topic,
Expand All @@ -71,6 +72,13 @@
custom_topic_name: ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD,
)
end
fab!(:topic_unsolved_2) do
Fabricate(
:topic,
user: user,
tags: [tag],
)
end
fab!(:topic_solved) do
Fabricate(
:custom_topic,
Expand All @@ -96,6 +104,7 @@
)
end
fab!(:post_unsolved) { Fabricate(:post, topic: topic_unsolved) }
fab!(:post_unsolved_2) { Fabricate(:post, topic: topic_unsolved_2) }
fab!(:post_solved) do
post = Fabricate(:post, topic: topic_solved)
DiscourseSolved.accept_answer!(post, Discourse.system_user)
Expand All @@ -105,10 +114,12 @@
fab!(:post_disabled_2) { Fabricate(:post, topic: topic_disabled_2) }

before do
SiteSetting.enable_solved_tags = tag.name
SearchIndexer.enable
Jobs.run_immediately!

SearchIndexer.index(topic_unsolved, force: true)
SearchIndexer.index(topic_unsolved_2, force: true)
SearchIndexer.index(topic_solved, force: true)
SearchIndexer.index(topic_disabled_1, force: true)
SearchIndexer.index(topic_disabled_2, force: true)
Expand All @@ -120,17 +131,23 @@
describe "when allow solved on all topics is disabled" do
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")
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 correct results when combining search with a tag" do
result = Search.execute("status:unsolved tag:tag1")
expect(result.posts.pluck(:id)).to match_array([post_unsolved_2.id])
end
end

describe "when allow solved on all topics is enabled" do
before { SiteSetting.allow_solved_on_all_topics = true }
it "only returns posts where the post is not solved" do
result = Search.execute("status:unsolved")
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
Expand Down

0 comments on commit 9b500aa

Please sign in to comment.