From 6a3e12a39cde72e1d0e078d5c3a0968828978e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= <1108771+megothss@users.noreply.github.com> Date: Thu, 18 Jul 2024 13:43:53 -0300 Subject: [PATCH] FEATURE: Include advanced search option to include unlisted topics in the results (#27958) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --------- Co-authored-by: Régis Hanol --- lib/search.rb | 5 ++++- spec/lib/search_spec.rb | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/search.rb b/lib/search.rb index ca48fb33f55..eac40417b84 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -935,6 +935,9 @@ class Search @search_context = user end + nil + elsif word =~ /\Ainclude:(invisible|unlisted)\z/i + @include_invisible = true nil else found ? nil : word @@ -1113,7 +1116,7 @@ class Search end is_topic_search = @search_context.present? && @search_context.is_a?(Topic) - posts = posts.where("topics.visible") unless is_topic_search + posts = posts.where("topics.visible") unless is_topic_search || @include_invisible if type_filter == "private_messages" || (is_topic_search && @search_context.private_message?) posts = diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rb index 566b95a06a6..620f3eaf77e 100644 --- a/spec/lib/search_spec.rb +++ b/spec/lib/search_spec.rb @@ -2541,6 +2541,23 @@ RSpec.describe Search do end end + describe "include:invisible / include:unlisted" do + it "allows including invisible topics in the results" do + topic = Fabricate(:topic, title: "I am testing a search", visible: false) + post = Fabricate(:post, topic: topic, raw: "this is the first post", post_number: 1) + _post2 = Fabricate(:post, topic: topic, raw: "this is the second post", post_number: 2) + + results = Search.execute("testing include:invisible") + expect(results.posts.map(&:id)).to eq([post.id]) + + results = Search.execute("testing include:unlisted") + expect(results.posts.map(&:id)).to eq([post.id]) + + results = Search.execute("testing") + expect(results.posts).to eq([]) + end + end + describe "ignore_diacritics" do before { SiteSetting.search_ignore_accents = true } let!(:post1) { Fabricate(:post, raw: "สวัสดี Rágis hello") }