FEATURE: Search filter for searching all PMs on a site for admin. (#11280)

FEATURE: Search filter for searching all PMs on a site for admin. (#11280)

Admins can search all PMS on a site by using the in:all-pms advanced filter.

diff --git a/lib/search.rb b/lib/search.rb
index e9e226d..7e7e5cf 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -173,6 +173,7 @@ class Search
     @blurb_length = @opts[:blurb_length]
     @valid = true
     @page = @opts[:page]
+    @search_all_pms = false
 
     term = term.to_s.dup
 
@@ -191,7 +192,7 @@ class Search
       @original_term = Search.escape_string(@term)
     end
 
-    if @search_pms || @opts[:type_filter] == 'private_messages'
+    if @search_pms || @search_all_pms || @opts[:type_filter] == 'private_messages'
       @opts[:type_filter] = "private_messages"
       @search_context ||= @guardian.user
 
@@ -331,6 +332,10 @@ class Search
     end
   end
 
+  advanced_filter(/^in:all-pms$/i) do |posts|
+    posts.private_posts if @guardian.is_admin?
+  end
+
   advanced_filter(/^in:tagged$/i) do |posts|
     posts
       .where('EXISTS (SELECT 1 FROM topic_tags WHERE topic_tags.topic_id = posts.topic_id)')
@@ -728,6 +733,9 @@ class Search
       elsif word =~ /^in:personal-direct$/i
         @search_pms = true
         nil
+      elsif word =~ /^in:all-pms$/i
+        @search_all_pms = true
+        nil
       elsif word =~ /^personal_messages:(.+)$/i
         if user = User.find_by_username($1)
           @search_pms = true
@@ -933,7 +941,11 @@ class Search
       if @search_context.present?
         if @search_context.is_a?(User)
           if type_filter === "private_messages"
-            @guardian.is_admin? ? posts.private_posts_for_user(@search_context) : posts
+            if @guardian.is_admin? && !@search_all_pms
+              posts.private_posts_for_user(@search_context)
+            else
+              posts
+            end
           else
             posts.where("posts.user_id = #{@search_context.id}")
           end
diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb
index 236f562..cc646d0 100644
--- a/spec/components/search_spec.rb
+++ b/spec/components/search_spec.rb
@@ -334,6 +334,26 @@ describe Search do
       end
     end
 
+    context 'all-pms flag' do
+      it 'returns matching PMs if the user is an admin' do
+        results = Search.execute('mars in:all-pms', guardian: Guardian.new(admin))
+
+        expect(results.posts).to include(reply, post2)
+      end
+
+      it 'returns nothing if the user is not an admin' do
+        results = Search.execute('mars in:all-pms', guardian: Guardian.new(Fabricate(:user)))
+
+        expect(results.posts).to be_empty
+      end
+
+      it 'returns nothing if the user is a moderator' do
+        results = Search.execute('mars in:all-pms', guardian: Guardian.new(Fabricate(:moderator)))
+
+        expect(results.posts).to be_empty
+      end
+    end
+
     context 'personal-direct flag' do
       let(:current) { Fabricate(:user, admin: true, username: "current_user") }
       let(:participant) { Fabricate(:user, username: "participant_1") }

GitHub sha: d815b959

This commit appears in #11280 which was approved by eviltrout. It was merged by romanrizzi.