2016-11-15 16:56:29 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-12 14:33:21 +01:00
|
|
|
class SearchService < BaseService
|
2017-05-05 17:26:04 +02:00
|
|
|
attr_accessor :query
|
|
|
|
|
2017-03-17 20:47:38 +01:00
|
|
|
def call(query, limit, resolve = false, account = nil)
|
2017-05-05 17:26:04 +02:00
|
|
|
@query = query
|
2016-11-12 14:33:21 +01:00
|
|
|
|
2017-05-05 17:26:04 +02:00
|
|
|
default_results.tap do |results|
|
|
|
|
if url_query?
|
|
|
|
results.merge!(remote_resource_results) unless remote_resource.nil?
|
|
|
|
elsif query.present?
|
|
|
|
results[:accounts] = AccountSearchService.new.call(query, limit, resolve, account)
|
|
|
|
results[:hashtags] = Tag.search_for(query.gsub(/\A#/, ''), limit) unless query.start_with?('@')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-31 22:44:12 +02:00
|
|
|
|
2017-05-05 17:26:04 +02:00
|
|
|
def default_results
|
|
|
|
{ accounts: [], hashtags: [], statuses: [] }
|
|
|
|
end
|
2016-11-12 14:33:21 +01:00
|
|
|
|
2017-05-05 17:26:04 +02:00
|
|
|
def url_query?
|
|
|
|
query =~ /\Ahttps?:\/\//
|
|
|
|
end
|
|
|
|
|
|
|
|
def remote_resource_results
|
|
|
|
{ remote_resource_symbol => [remote_resource] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def remote_resource
|
|
|
|
@_remote_resource ||= FetchRemoteResourceService.new.call(query)
|
|
|
|
end
|
2016-11-12 14:33:21 +01:00
|
|
|
|
2017-05-05 17:26:04 +02:00
|
|
|
def remote_resource_symbol
|
|
|
|
remote_resource.class.name.downcase.pluralize.to_sym
|
2016-11-12 14:33:21 +01:00
|
|
|
end
|
|
|
|
end
|