闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
982 B

  1. # frozen_string_literal: true
  2. class AccountSearchService < BaseService
  3. def call(query, limit, resolve = false, account = nil)
  4. return [] if query.blank? || query.start_with?('#')
  5. username, domain = query.gsub(/\A@/, '').split('@')
  6. domain = nil if TagManager.instance.local_domain?(domain)
  7. if domain.nil?
  8. exact_match = Account.find_local(username)
  9. results = account.nil? ? Account.search_for(username, limit) : Account.advanced_search_for(username, account, limit)
  10. else
  11. exact_match = Account.find_remote(username, domain)
  12. results = account.nil? ? Account.search_for("#{username} #{domain}", limit) : Account.advanced_search_for("#{username} #{domain}", account, limit)
  13. end
  14. results = [exact_match] + results.reject { |a| a.id == exact_match.id } if exact_match
  15. if resolve && !exact_match && !domain.nil?
  16. results = [FollowRemoteAccountService.new.call("#{username}@#{domain}")]
  17. end
  18. results
  19. end
  20. end