Skip to content

Commit a18ce6d

Browse files
authored
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.
1 parent 4c6ddcf commit a18ce6d

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

plugin.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,20 +368,29 @@ def self.skip_db?
368368
)",
369369
)
370370
else
371+
tag_ids = Tag.where(name: SiteSetting.enable_solved_tags.split("|")).pluck(:id)
372+
371373
posts.where(
372374
"topics.id NOT IN (
373375
SELECT tc.topic_id
374376
FROM topic_custom_fields tc
375377
WHERE tc.name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}' AND
376378
tc.value IS NOT NULL
377-
) AND topics.id IN (
379+
) AND (topics.id IN (
378380
SELECT top.id
379381
FROM topics top
380382
INNER JOIN category_custom_fields cc
381383
ON top.category_id = cc.category_id
382384
WHERE cc.name = '#{::DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD}' AND
383385
cc.value = 'true'
384-
)",
386+
) OR topics.id IN (
387+
SELECT top.id
388+
FROM topics top
389+
INNER JOIN topic_tags tt
390+
ON top.id = tt.topic_id
391+
WHERE tt.tag_id IN (?)
392+
))",
393+
tag_ids,
385394
)
386395
end
387396
end

spec/integration/solved_spec.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
category_custom_field.save
6464
category
6565
end
66+
fab!(:tag)
6667
fab!(:topic_unsolved) do
6768
Fabricate(
6869
:custom_topic,
@@ -71,6 +72,7 @@
7172
custom_topic_name: ::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD,
7273
)
7374
end
75+
fab!(:topic_unsolved_2) { Fabricate(:topic, user: user, tags: [tag]) }
7476
fab!(:topic_solved) do
7577
Fabricate(
7678
:custom_topic,
@@ -96,6 +98,7 @@
9698
)
9799
end
98100
fab!(:post_unsolved) { Fabricate(:post, topic: topic_unsolved) }
101+
fab!(:post_unsolved_2) { Fabricate(:post, topic: topic_unsolved_2) }
99102
fab!(:post_solved) do
100103
post = Fabricate(:post, topic: topic_solved)
101104
DiscourseSolved.accept_answer!(post, Discourse.system_user)
@@ -105,10 +108,12 @@
105108
fab!(:post_disabled_2) { Fabricate(:post, topic: topic_disabled_2) }
106109

107110
before do
111+
SiteSetting.enable_solved_tags = tag.name
108112
SearchIndexer.enable
109113
Jobs.run_immediately!
110114

111115
SearchIndexer.index(topic_unsolved, force: true)
116+
SearchIndexer.index(topic_unsolved_2, force: true)
112117
SearchIndexer.index(topic_solved, force: true)
113118
SearchIndexer.index(topic_disabled_1, force: true)
114119
SearchIndexer.index(topic_disabled_2, force: true)
@@ -120,17 +125,23 @@
120125
describe "when allow solved on all topics is disabled" do
121126
before { SiteSetting.allow_solved_on_all_topics = false }
122127

123-
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
128+
it "only returns unsolved posts from categories and tags where solving is enabled" do
124129
result = Search.execute("status:unsolved")
125-
expect(result.posts.pluck(:id)).to match_array([post_unsolved.id])
130+
expect(result.posts.pluck(:id)).to match_array([post_unsolved.id, post_unsolved_2.id])
131+
end
132+
133+
it "returns the filtered results when combining search with a tag" do
134+
result = Search.execute("status:unsolved tag:#{tag.name}")
135+
expect(result.posts.pluck(:id)).to match_array([post_unsolved_2.id])
126136
end
127137
end
138+
128139
describe "when allow solved on all topics is enabled" do
129140
before { SiteSetting.allow_solved_on_all_topics = true }
130141
it "only returns posts where the post is not solved" do
131142
result = Search.execute("status:unsolved")
132143
expect(result.posts.pluck(:id)).to match_array(
133-
[post_unsolved.id, post_disabled_1.id, post_disabled_2.id],
144+
[post_unsolved.id, post_unsolved_2.id, post_disabled_1.id, post_disabled_2.id],
134145
)
135146
end
136147
end

0 commit comments

Comments
 (0)