about summary refs log tree commit diff
path: root/app/services/account_search_service.rb
blob: f55439dcba78507252d7f21a08ebdf5e00596ac3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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