diff options
author | happycoloredbanana <happycoloredbanana@users.noreply.github.com> | 2017-05-27 01:55:08 +0300 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2017-05-27 00:55:08 +0200 |
commit | 9642601126541347cbf6f49105ba7254e0ab234a (patch) | |
tree | a500f6e9b8d831e5670f37d2cbc3ff143212d1b9 | |
parent | 3836d293a155b491299854149a1b3dc5adcd1b7f (diff) |
Avoid comparing domains when looking for an exact match of a local account (#3336)
-rw-r--r-- | app/services/account_search_service.rb | 8 | ||||
-rw-r--r-- | spec/services/account_search_service_spec.rb | 31 |
2 files changed, 32 insertions, 7 deletions
diff --git a/app/services/account_search_service.rb b/app/services/account_search_service.rb index 04b2ed958..2b77ad7c6 100644 --- a/app/services/account_search_service.rb +++ b/app/services/account_search_service.rb @@ -59,7 +59,13 @@ class AccountSearchService < BaseService end def exact_match - @_exact_match ||= Account.find_remote(query_username, query_domain) + @_exact_match ||= begin + if domain_is_local? + Account.find_local(query_username) + else + Account.find_remote(query_username, query_domain) + end + end end def search_results diff --git a/spec/services/account_search_service_spec.rb b/spec/services/account_search_service_spec.rb index b7c281a56..697ac8f23 100644 --- a/spec/services/account_search_service_spec.rb +++ b/spec/services/account_search_service_spec.rb @@ -33,25 +33,25 @@ describe AccountSearchService do describe 'searching local and remote users' do describe "when only '@'" do before do - allow(Account).to receive(:find_remote) + allow(Account).to receive(:find_local) allow(Account).to receive(:search_for) subject.call('@', 10) end - it 'uses find_remote with empty query to look for local accounts' do - expect(Account).to have_received(:find_remote).with('', nil) + it 'uses find_local with empty query to look for local accounts' do + expect(Account).to have_received(:find_local).with('') end end describe 'when no domain' do before do - allow(Account).to receive(:find_remote) + allow(Account).to receive(:find_local) allow(Account).to receive(:search_for) subject.call('one', 10) end - it 'uses find_remote with nil domain to look for local accounts' do - expect(Account).to have_received(:find_remote).with('one', nil) + it 'uses find_local to look for local accounts' do + expect(Account).to have_received(:find_local).with('one') end it 'uses search_for to find matches' do @@ -101,6 +101,25 @@ describe AccountSearchService do end end + describe 'when there is a local domain' do + around do |example| + before = Rails.configuration.x.local_domain + example.run + Rails.configuration.x.local_domain = before + end + + it 'returns exact match first' do + remote = Fabricate(:account, username: 'a', domain: 'remote', display_name: 'e') + remote_too = Fabricate(:account, username: 'b', domain: 'remote', display_name: 'e') + exact = Fabricate(:account, username: 'e') + Rails.configuration.x.local_domain = 'example.com' + + results = subject.call('e@example.com', 2) + expect(results.size).to eq 2 + expect(results).to eq([exact, remote]).or eq([exact, remote_too]) + end + end + describe 'when there is a domain but no exact match' do it 'follows the remote account when resolve is true' do service = double(call: nil) |