2016-11-16 02:56:29 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-13 00:33:21 +11:00
|
|
|
class SearchService < BaseService
|
2017-05-06 01:26:04 +10:00
|
|
|
attr_accessor :query
|
|
|
|
|
2017-03-18 06:47:38 +11:00
|
|
|
def call(query, limit, resolve = false, account = nil)
|
2017-05-06 01:26:04 +10:00
|
|
|
@query = query
|
2016-11-13 00:33:21 +11:00
|
|
|
|
2017-05-06 01:26:04 +10:00
|
|
|
default_results.tap do |results|
|
|
|
|
if url_query?
|
2018-01-23 00:24:22 +11:00
|
|
|
results.merge!(url_resource_results) unless url_resource.nil?
|
2017-05-06 01:26:04 +10:00
|
|
|
elsif query.present?
|
2017-12-06 09:02:27 +11:00
|
|
|
results[:accounts] = AccountSearchService.new.call(query, limit, account, resolve: resolve)
|
2017-05-06 01:26:04 +10:00
|
|
|
results[:hashtags] = Tag.search_for(query.gsub(/\A#/, ''), limit) unless query.start_with?('@')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-01 07:44:12 +11:00
|
|
|
|
2017-05-06 01:26:04 +10:00
|
|
|
def default_results
|
|
|
|
{ accounts: [], hashtags: [], statuses: [] }
|
|
|
|
end
|
2016-11-13 00:33:21 +11:00
|
|
|
|
2017-05-06 01:26:04 +10:00
|
|
|
def url_query?
|
|
|
|
query =~ /\Ahttps?:\/\//
|
|
|
|
end
|
|
|
|
|
2018-01-23 00:24:22 +11:00
|
|
|
def url_resource_results
|
|
|
|
{ url_resource_symbol => [url_resource] }
|
2017-05-06 01:26:04 +10:00
|
|
|
end
|
|
|
|
|
2018-01-23 00:24:22 +11:00
|
|
|
def url_resource
|
|
|
|
@_url_resource ||= ResolveURLService.new.call(query)
|
2017-05-06 01:26:04 +10:00
|
|
|
end
|
2016-11-13 00:33:21 +11:00
|
|
|
|
2018-01-23 00:24:22 +11:00
|
|
|
def url_resource_symbol
|
|
|
|
url_resource.class.name.downcase.pluralize.to_sym
|
2016-11-13 00:33:21 +11:00
|
|
|
end
|
|
|
|
end
|