Fix account search always returning exact match on paginated results (#11525)

Fix #11365
signup-info-prompt
Eugen Rochko 2019-08-11 13:14:51 +02:00 committed by GitHub
parent 7a1f8a58df
commit c5661b0f92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 13 deletions

View File

@ -21,18 +21,22 @@ class AccountSearchService < BaseService
if resolving_non_matching_remote_account? if resolving_non_matching_remote_account?
[ResolveAccountService.new.call("#{query_username}@#{query_domain}")].compact [ResolveAccountService.new.call("#{query_username}@#{query_domain}")].compact
else else
search_results_and_exact_match.compact.uniq.slice(0, limit) search_results_and_exact_match.compact.uniq
end end
end end
def resolving_non_matching_remote_account? def resolving_non_matching_remote_account?
options[:resolve] && !exact_match && !domain_is_local? offset.zero? && options[:resolve] && !exact_match? && !domain_is_local?
end end
def search_results_and_exact_match def search_results_and_exact_match
exact = [exact_match] return search_results.to_a unless offset.zero?
return exact if !exact[0].nil? && limit == 1
exact + search_results.to_a results = [exact_match]
return results if exact_match? && limit == 1
results + search_results.to_a
end end
def query_blank_or_hashtag? def query_blank_or_hashtag?
@ -40,15 +44,15 @@ class AccountSearchService < BaseService
end end
def split_query_string def split_query_string
@_split_query_string ||= query.gsub(/\A@/, '').split('@') @split_query_string ||= query.gsub(/\A@/, '').split('@')
end end
def query_username def query_username
@_query_username ||= split_query_string.first || '' @query_username ||= split_query_string.first || ''
end end
def query_domain def query_domain
@_query_domain ||= query_without_split? ? nil : split_query_string.last @query_domain ||= query_without_split? ? nil : split_query_string.last
end end
def query_without_split? def query_without_split?
@ -56,15 +60,21 @@ class AccountSearchService < BaseService
end end
def domain_is_local? def domain_is_local?
@_domain_is_local ||= TagManager.instance.local_domain?(query_domain) @domain_is_local ||= TagManager.instance.local_domain?(query_domain)
end end
def search_from def search_from
options[:following] && account ? account.following : Account options[:following] && account ? account.following : Account
end end
def exact_match?
exact_match.present?
end
def exact_match def exact_match
@_exact_match ||= begin return @exact_match if defined?(@exact_match)
@exact_match = begin
if domain_is_local? if domain_is_local?
search_from.without_suspended.find_local(query_username) search_from.without_suspended.find_local(query_username)
else else
@ -74,7 +84,7 @@ class AccountSearchService < BaseService
end end
def search_results def search_results
@_search_results ||= begin @search_results ||= begin
if account if account
advanced_search_results advanced_search_results
else else
@ -84,11 +94,19 @@ class AccountSearchService < BaseService
end end
def advanced_search_results 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 end
def simple_search_results 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 end
def terms_for_query def terms_for_query