diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 30d9a186c..56c15b2f3 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -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 diff --git a/spec/services/search_service_spec.rb b/spec/services/search_service_spec.rb index bbab5e05d..9ce1b0785 100644 --- a/spec/services/search_service_spec.rb +++ b/spec/services/search_service_spec.rb @@ -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