FEATURE: Add group_messages: keyword to advanced search (#16584)

This commit is contained in:
Penar Musaraj
2022-04-28 10:47:40 -04:00
committed by GitHub
parent de19003bad
commit b266a36967
2 changed files with 97 additions and 35 deletions

View File

@ -609,6 +609,20 @@ class Search
end
end
advanced_filter(/^group_messages:(.+)$/i) do |posts, match|
group_id = Group
.visible_groups(@guardian.user)
.members_visible_groups(@guardian.user)
.where(has_messages: true)
.where('name ilike ? OR (id = ? AND id > 0)', match, match.to_i).pluck_first(:id)
if group_id
posts.where('posts.topic_id IN (SELECT topic_id FROM topic_allowed_groups WHERE group_id = ?)', group_id)
else
posts.where("1 = 0")
end
end
advanced_filter(/^user:(.+)$/i) do |posts, match|
user_id = User.where(staged: false).where('username_lower = ? OR id = ?', match.downcase, match.to_i).pluck_first(:id)
if user_id
@ -757,6 +771,9 @@ class Search
elsif word =~ /^in:all-pms$/i
@search_all_pms = true
nil
elsif word =~ /^group_messages:(.+)$/i
@search_pms = true
nil
elsif word =~ /^personal_messages:(.+)$/i
if user = User.find_by_username($1)
@search_pms = true

View File

@ -582,10 +582,11 @@ describe Search do
end
end
context 'personal-direct flag' do
context 'personal-direct and group_messages flags' do
let(:current) { Fabricate(:user, admin: true, username: "current_user") }
let(:participant) { Fabricate(:user, username: "participant_1") }
let(:participant_2) { Fabricate(:user, username: "participant_2") }
let(:non_participant) { Fabricate(:user, username: "non_participant") }
let(:group) do
group = Fabricate(:group, has_messages: true)
@ -609,6 +610,7 @@ describe Search do
pm.reload
end
context "personal-direct flag" do
it 'can find all direct PMs of the current user' do
pm = create_pm(users: [current, participant])
_pm_2 = create_pm(users: [participant_2, participant])
@ -655,6 +657,49 @@ describe Search do
end
end
context "group_messages flag" do
it 'returns results correctly for a PM in a given group' do
pm = create_pm(users: [participant, participant_2], group: group)
results = Search.execute("group_messages:#{group.name}", guardian: Guardian.new(current))
expect(results.posts).to contain_exactly(pm.first_post)
results = Search.execute("secret group_messages:#{group.name}", guardian: Guardian.new(current))
expect(results.posts).to contain_exactly(pm.first_post)
end
it 'returns nothing if user is not a group member' do
pm = create_pm(users: [current, participant], group: group)
results = Search.execute("group_messages:#{group.name}", guardian: Guardian.new(non_participant))
expect(results.posts.size).to eq(0)
# even for admins
results = Search.execute("group_messages:#{group.name}", guardian: Guardian.new(admin))
expect(results.posts.size).to eq(0)
end
it 'returns nothing if group has messages disabled' do
pm = create_pm(users: [current, participant], group: group)
group.update!(has_messages: false)
results = Search.execute("group_messages:#{group.name}", guardian: Guardian.new(current))
expect(results.posts.size).to eq(0)
end
it 'is correctly scoped to a given group' do
wrong_group = Fabricate(:group, has_messages: true)
pm = create_pm(users: [current, participant], group: group)
results = Search.execute("group_messages:#{group.name}", guardian: Guardian.new(current))
expect(results.posts).to contain_exactly(pm.first_post)
results = Search.execute("group_messages:#{wrong_group.name}", guardian: Guardian.new(current))
expect(results.posts.size).to eq(0)
end
end
end
context 'all topics' do
let!(:u1) { Fabricate(:user, username: 'fred', name: 'bob jones', email: 'foo+1@bar.baz') }
let!(:u2) { Fabricate(:user, username: 'bob', name: 'fred jones', email: 'foo+2@bar.baz') }