From c5661b0f92dfb74845fdfdc556e91083f9906882 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 11 Aug 2019 13:14:51 +0200 Subject: [PATCH] Fix account search always returning exact match on paginated results (#11525) Fix #11365 --- app/services/account_search_service.rb | 44 ++++++++++++++++++-------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/app/services/account_search_service.rb b/app/services/account_search_service.rb index 7bdffbbd22c..e1874d0450f 100644 --- a/app/services/account_search_service.rb +++ b/app/services/account_search_service.rb @@ -21,18 +21,22 @@ class AccountSearchService < BaseService if resolving_non_matching_remote_account? [ResolveAccountService.new.call("#{query_username}@#{query_domain}")].compact else - search_results_and_exact_match.compact.uniq.slice(0, limit) + search_results_and_exact_match.compact.uniq end end def resolving_non_matching_remote_account? - options[:resolve] && !exact_match && !domain_is_local? + offset.zero? && options[:resolve] && !exact_match? && !domain_is_local? end def search_results_and_exact_match - exact = [exact_match] - return exact if !exact[0].nil? && limit == 1 - exact + search_results.to_a + return search_results.to_a unless offset.zero? + + results = [exact_match] + + return results if exact_match? && limit == 1 + + results + search_results.to_a end def query_blank_or_hashtag? @@ -40,15 +44,15 @@ class AccountSearchService < BaseService end def split_query_string - @_split_query_string ||= query.gsub(/\A@/, '').split('@') + @split_query_string ||= query.gsub(/\A@/, '').split('@') end def query_username - @_query_username ||= split_query_string.first || '' + @query_username ||= split_query_string.first || '' end def query_domain - @_query_domain ||= query_without_split? ? nil : split_query_string.last + @query_domain ||= query_without_split? ? nil : split_query_string.last end def query_without_split? @@ -56,15 +60,21 @@ class AccountSearchService < BaseService end def domain_is_local? - @_domain_is_local ||= TagManager.instance.local_domain?(query_domain) + @domain_is_local ||= TagManager.instance.local_domain?(query_domain) end def search_from options[:following] && account ? account.following : Account end + def exact_match? + exact_match.present? + end + def exact_match - @_exact_match ||= begin + return @exact_match if defined?(@exact_match) + + @exact_match = begin if domain_is_local? search_from.without_suspended.find_local(query_username) else @@ -74,7 +84,7 @@ class AccountSearchService < BaseService end def search_results - @_search_results ||= begin + @search_results ||= begin if account advanced_search_results else @@ -84,11 +94,19 @@ class AccountSearchService < BaseService end def advanced_search_results - Account.advanced_search_for(terms_for_query, account, limit, options[:following], offset) + Account.advanced_search_for(terms_for_query, account, limit_for_non_exact_results, options[:following], offset) end def simple_search_results - Account.search_for(terms_for_query, limit, offset) + Account.search_for(terms_for_query, limit_for_non_exact_results, offset) + end + + def limit_for_non_exact_results + if offset.zero? && exact_match? + limit - 1 + else + limit + end end def terms_for_query