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