From a18ce6d712fafed286bcc99543dd173110c6dfb8 Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Thu, 28 Mar 2024 16:46:22 -0400 Subject: [PATCH] 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. --- plugin.rb | 13 +++++++++++-- spec/integration/solved_spec.rb | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/plugin.rb b/plugin.rb index 31dad15..053564c 100644 --- a/plugin.rb +++ b/plugin.rb @@ -368,20 +368,29 @@ after_initialize do )", ) 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 diff --git a/spec/integration/solved_spec.rb b/spec/integration/solved_spec.rb index 93dc089..43c20fd 100644 --- a/spec/integration/solved_spec.rb +++ b/spec/integration/solved_spec.rb @@ -63,6 +63,7 @@ RSpec.describe "Managing Posts solved status" do category_custom_field.save category end + fab!(:tag) fab!(:topic_unsolved) do Fabricate( :custom_topic, @@ -71,6 +72,7 @@ RSpec.describe "Managing Posts solved status" do 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, @@ -96,6 +98,7 @@ RSpec.describe "Managing Posts solved status" do ) 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) @@ -105,10 +108,12 @@ RSpec.describe "Managing Posts solved status" do 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) @@ -120,17 +125,23 @@ RSpec.describe "Managing Posts solved status" do 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