about summary refs log tree commit diff
path: root/app/services/search_service.rb
blob: e9a27f136d56a7dec8066d3f515a0b4988f1f5e1 (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 SearchService < BaseService
  def call(query, limit, resolve = false)
    return if query.blank? || query.start_with?('#')

    username, domain = query.gsub(/\A@/, '').split('@')

    if domain.nil?
      exact_match = Account.find_local(username)
      results     = Account.search_for(username)
    else
      exact_match = Account.find_remote(username, domain)
      results     = Account.search_for("#{username} #{domain}")
    end

    results = results.limit(limit).to_a
    results = [exact_match] + results.reject { |a| a.id == exact_match.id } if exact_match

    if resolve && results.empty? && !domain.nil?
      results = [FollowRemoteAccountService.new.call("#{username}@#{domain}")]
    end

    results
  end
end