1
0
mirror of https://github.com/funamitech/mastodon synced 2024-12-05 10:18:45 +09:00
YuruToot/app/services/account_search_service.rb
Eugen Rochko 05cf086766 New API method: /api/v1/search
Returns accounts, statuses, hashtags arrays
2017-03-22 02:32:27 +01:00

27 lines
982 B
Ruby

# frozen_string_literal: true
class AccountSearchService < BaseService
def call(query, limit, resolve = false, account = nil)
return [] if query.blank? || query.start_with?('#')
username, domain = query.gsub(/\A@/, '').split('@')
domain = nil if TagManager.instance.local_domain?(domain)
if domain.nil?
exact_match = Account.find_local(username)
results = account.nil? ? Account.search_for(username, limit) : Account.advanced_search_for(username, account, limit)
else
exact_match = Account.find_remote(username, domain)
results = account.nil? ? Account.search_for("#{username} #{domain}", limit) : Account.advanced_search_for("#{username} #{domain}", account, limit)
end
results = [exact_match] + results.reject { |a| a.id == exact_match.id } if exact_match
if resolve && !exact_match && !domain.nil?
results = [FollowRemoteAccountService.new.call("#{username}@#{domain}")]
end
results
end
end