Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Allow searching for unsolved posts with tags #284

Merged
merged 3 commits into from Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions plugin.rb
Expand Up @@ -368,20 +368,29 @@ 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
17 changes: 14 additions & 3 deletions spec/integration/solved_spec.rb
Expand Up @@ -63,6 +63,7 @@
category_custom_field.save
category
end
fab!(:tag)
fab!(:topic_unsolved) do
Fabricate(
:custom_topic,
Expand All @@ -71,6 +72,7 @@
custom_topic_name: ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD,
)
end
fab!(:topic_unsolved_2) { Fabricate(:topic, user: user, tags: [tag]) }
fab!(:topic_solved) do
Fabricate(
:custom_topic,
Expand All @@ -96,6 +98,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 +108,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 +125,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 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

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