1
0

search_service.rb 766 B

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