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.
This commit is contained in:
Roman Rizzi 2020-11-19 13:56:19 -03:00 committed by GitHub
parent 5202380a04
commit d815b95935
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 2 deletions

View File

@ -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

View File

@ -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") }