2016-11-16 02:56:29 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-13 00:33:21 +11:00
|
|
|
class SearchService < BaseService
|
2017-03-18 06:47:38 +11:00
|
|
|
def call(query, limit, resolve = false, account = nil)
|
2016-11-27 01:32:29 +11:00
|
|
|
return if query.blank? || query.start_with?('#')
|
2016-11-13 00:33:21 +11:00
|
|
|
|
2016-11-27 01:32:29 +11:00
|
|
|
username, domain = query.gsub(/\A@/, '').split('@')
|
2017-02-27 10:15:00 +11:00
|
|
|
domain = nil if TagManager.instance.local_domain?(domain)
|
2016-11-13 00:33:21 +11:00
|
|
|
|
2016-12-03 00:14:49 +11:00
|
|
|
if domain.nil?
|
|
|
|
exact_match = Account.find_local(username)
|
2017-03-18 06:47:38 +11:00
|
|
|
results = account.nil? ? Account.search_for(username, limit) : Account.advanced_search_for(username, account, limit)
|
2016-12-03 00:14:49 +11:00
|
|
|
else
|
|
|
|
exact_match = Account.find_remote(username, domain)
|
2017-03-18 06:47:38 +11:00
|
|
|
results = account.nil? ? Account.search_for("#{username} #{domain}", limit) : Account.advanced_search_for("#{username} #{domain}", account, limit)
|
2016-12-03 00:14:49 +11:00
|
|
|
end
|
2016-11-13 00:33:21 +11:00
|
|
|
|
2016-12-03 00:14:49 +11:00
|
|
|
results = [exact_match] + results.reject { |a| a.id == exact_match.id } if exact_match
|
2016-11-13 00:33:21 +11:00
|
|
|
|
2017-02-05 13:01:23 +11:00
|
|
|
if resolve && !exact_match && !domain.nil?
|
2016-11-13 00:49:28 +11:00
|
|
|
results = [FollowRemoteAccountService.new.call("#{username}@#{domain}")]
|
|
|
|
end
|
2016-11-13 00:33:21 +11:00
|
|
|
|
|
|
|
results
|
|
|
|
end
|
|
|
|
end
|