feat: skip inbox filter if the user has access to all inboxes (#12043)
This commit is contained in:
@@ -83,10 +83,18 @@ class SearchService
|
|||||||
|
|
||||||
def message_base_query
|
def message_base_query
|
||||||
query = current_account.messages.where('created_at >= ?', 3.months.ago)
|
query = current_account.messages.where('created_at >= ?', 3.months.ago)
|
||||||
query = query.where(inbox_id: accessable_inbox_ids) unless account_user.administrator?
|
query = query.where(inbox_id: accessable_inbox_ids) unless should_skip_inbox_filtering?
|
||||||
query
|
query
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def should_skip_inbox_filtering?
|
||||||
|
account_user.administrator? || user_has_access_to_all_inboxes?
|
||||||
|
end
|
||||||
|
|
||||||
|
def user_has_access_to_all_inboxes?
|
||||||
|
accessable_inbox_ids.sort == current_account.inboxes.pluck(:id).sort
|
||||||
|
end
|
||||||
|
|
||||||
def use_gin_search
|
def use_gin_search
|
||||||
current_account.feature_enabled?('search_with_gin')
|
current_account.feature_enabled?('search_with_gin')
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -213,15 +213,32 @@ describe SearchService do
|
|||||||
account_user.update!(role: 'agent')
|
account_user.update!(role: 'agent')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'filters by accessible inbox_id' do
|
it 'filters by accessible inbox_id when user has limited access' do
|
||||||
# Testing the private method itself seems like the best way to ensure
|
# Create an additional inbox that user is NOT assigned to
|
||||||
# that the inboxes are not added to the search query
|
create(:inbox, account: account)
|
||||||
|
|
||||||
base_query = search.send(:message_base_query)
|
base_query = search.send(:message_base_query)
|
||||||
|
|
||||||
# Should have both time and inbox filters
|
# Should have both time and inbox filters
|
||||||
expect(base_query.to_sql).to include('created_at >= ')
|
expect(base_query.to_sql).to include('created_at >= ')
|
||||||
expect(base_query.to_sql).to include('inbox_id')
|
expect(base_query.to_sql).to include('inbox_id')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when user has access to all inboxes' do
|
||||||
|
before do
|
||||||
|
# Create additional inbox and assign user to all inboxes
|
||||||
|
other_inbox = create(:inbox, account: account)
|
||||||
|
create(:inbox_member, user: user, inbox: other_inbox)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'skips inbox filtering as optimization' do
|
||||||
|
base_query = search.send(:message_base_query)
|
||||||
|
|
||||||
|
# Should only have the time filter, not inbox filter
|
||||||
|
expect(base_query.to_sql).to include('created_at >= ')
|
||||||
|
expect(base_query.to_sql).not_to include('inbox_id')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user