glitchier-soc/app/services/search_service.rb

27 lines
766 B
Ruby
Raw Normal View History

# frozen_string_literal: true
class SearchService < BaseService
2016-11-12 15:49:28 +02:00
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
2016-11-12 15:49:28 +02:00
if resolve && results.empty? && !domain.nil?
results = [FollowRemoteAccountService.new.call("#{username}@#{domain}")]
end
results
end
end