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.

86 lines
1.8 KiB

  1. # frozen_string_literal: true
  2. class AccountSearchService < BaseService
  3. attr_reader :query, :limit, :resolve, :account
  4. def call(query, limit, resolve = false, account = nil)
  5. @query = query
  6. @limit = limit
  7. @resolve = resolve
  8. @account = account
  9. search_service_results
  10. end
  11. private
  12. def search_service_results
  13. return [] if query_blank_or_hashtag?
  14. if resolving_non_matching_remote_account?
  15. [FollowRemoteAccountService.new.call("#{query_username}@#{query_domain}")]
  16. else
  17. search_results_and_exact_match.compact.uniq
  18. end
  19. end
  20. def resolving_non_matching_remote_account?
  21. resolve && !exact_match && !domain_is_local?
  22. end
  23. def search_results_and_exact_match
  24. [exact_match] + search_results.to_a
  25. end
  26. def query_blank_or_hashtag?
  27. query.blank? || query.start_with?('#')
  28. end
  29. def split_query_string
  30. @_split_query_string ||= query.gsub(/\A@/, '').split('@')
  31. end
  32. def query_username
  33. @_query_username ||= split_query_string.first
  34. end
  35. def query_domain
  36. @_query_domain ||= query_without_split? ? nil : split_query_string.last
  37. end
  38. def query_without_split?
  39. split_query_string.size == 1
  40. end
  41. def domain_is_local?
  42. @_domain_is_local ||= TagManager.instance.local_domain?(query_domain)
  43. end
  44. def exact_match
  45. @_exact_match ||= Account.find_remote(query_username, query_domain)
  46. end
  47. def search_results
  48. @_search_results ||= if account
  49. advanced_search_results
  50. else
  51. simple_search_results
  52. end
  53. end
  54. def advanced_search_results
  55. Account.advanced_search_for(terms_for_query, account, limit)
  56. end
  57. def simple_search_results
  58. Account.search_for(terms_for_query, limit)
  59. end
  60. def terms_for_query
  61. if domain_is_local?
  62. query_username
  63. else
  64. "#{query_username} #{query_domain}"
  65. end
  66. end
  67. end