feat: skip inbox filter if the user has access to all inboxes (#12043)

This commit is contained in:
Shivam Mishra
2025-07-25 13:59:10 +04:00
committed by GitHub
parent 87313ecc35
commit c09e875c83
2 changed files with 29 additions and 4 deletions

View File

@@ -83,10 +83,18 @@ class SearchService
def message_base_query
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
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
current_account.feature_enabled?('search_with_gin')
end

View File

@@ -213,15 +213,32 @@ describe SearchService do
account_user.update!(role: 'agent')
end
it 'filters by accessible inbox_id' do
# Testing the private method itself seems like the best way to ensure
# that the inboxes are not added to the search query
it 'filters by accessible inbox_id when user has limited access' do
# Create an additional inbox that user is NOT assigned to
create(:inbox, account: account)
base_query = search.send(:message_base_query)
# Should have both time and inbox filters
expect(base_query.to_sql).to include('created_at >= ')
expect(base_query.to_sql).to include('inbox_id')
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