2019-07-02 08:10:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SearchService < BaseService
|
|
|
|
def call(query, account, limit, options = {})
|
2020-04-29 23:32:49 +01:00
|
|
|
@query = query&.strip
|
2019-07-02 08:10:25 +01:00
|
|
|
@account = account
|
|
|
|
@options = options
|
|
|
|
@limit = limit.to_i
|
|
|
|
@offset = options[:type].blank? ? 0 : options[:offset].to_i
|
|
|
|
@resolve = options[:resolve] || false
|
2020-08-07 05:05:02 +01:00
|
|
|
@onlyVerified = options[:onlyVerified] || false
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
default_results.tap do |results|
|
2020-04-29 23:32:49 +01:00
|
|
|
next if @query.blank? || @limit.zero?
|
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
if @query.present?
|
2019-07-02 08:10:25 +01:00
|
|
|
results[:accounts] = perform_accounts_search! if account_searchable?
|
2021-01-09 02:46:03 +00:00
|
|
|
results[:statuses] = [] # perform_statuses_search! if full_text_searchable? && !account.nil?
|
2020-11-05 22:08:17 +00:00
|
|
|
results[:links] = [] # perform_links_search! if !account.nil?
|
2020-07-25 00:00:29 +01:00
|
|
|
results[:groups] = perform_groups_search!
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def perform_accounts_search!
|
|
|
|
AccountSearchService.new.call(
|
|
|
|
@query,
|
|
|
|
@account,
|
|
|
|
limit: @limit,
|
|
|
|
resolve: @resolve,
|
2020-08-07 05:05:02 +01:00
|
|
|
offset: @offset,
|
|
|
|
onlyVerified: @onlyVerified
|
2019-07-02 08:10:25 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-01-14 23:13:30 +00:00
|
|
|
def perform_groups_search!
|
|
|
|
Group.search_for(
|
|
|
|
@query.gsub(/\A#/, ''),
|
|
|
|
@offset
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-10-31 23:10:59 +00:00
|
|
|
def perform_links_search!
|
|
|
|
PreviewCard.search_for(
|
|
|
|
@query.gsub(/\A#/, ''),
|
|
|
|
@offset
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
def perform_statuses_search!
|
|
|
|
definition = StatusesIndex.filter(term: { searchable_by: @account.id })
|
|
|
|
.query(multi_match: { type: 'most_fields', query: @query, operator: 'and', fields: %w(text text.stemmed) })
|
|
|
|
|
|
|
|
if @options[:account_id].present?
|
|
|
|
definition = definition.filter(term: { account_id: @options[:account_id] })
|
|
|
|
end
|
|
|
|
|
|
|
|
if @options[:min_id].present? || @options[:max_id].present?
|
|
|
|
range = {}
|
|
|
|
range[:gt] = @options[:min_id].to_i if @options[:min_id].present?
|
|
|
|
range[:lt] = @options[:max_id].to_i if @options[:max_id].present?
|
|
|
|
definition = definition.filter(range: { id: range })
|
|
|
|
end
|
|
|
|
|
|
|
|
results = definition.limit(@limit).offset(@offset).objects.compact
|
|
|
|
account_ids = results.map(&:account_id)
|
|
|
|
account_domains = results.map(&:account_domain)
|
|
|
|
preloaded_relations = relations_map_for_account(@account, account_ids, account_domains)
|
|
|
|
|
|
|
|
results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? }
|
|
|
|
rescue Faraday::ConnectionFailed
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_results
|
2020-11-15 18:48:32 +00:00
|
|
|
{ accounts: [], statuses: [], links: [], groups: [] }
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def full_text_searchable?
|
|
|
|
return false unless Chewy.enabled?
|
|
|
|
|
|
|
|
statuses_search? && !@account.nil? && !((@query.start_with?('#') || @query.include?('@')) && !@query.include?(' '))
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_searchable?
|
|
|
|
account_search? && !(@query.include?('@') && @query.include?(' '))
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_search?
|
|
|
|
@options[:type].blank? || @options[:type] == 'accounts'
|
|
|
|
end
|
|
|
|
|
|
|
|
def statuses_search?
|
|
|
|
@options[:type].blank? || @options[:type] == 'statuses'
|
|
|
|
end
|
|
|
|
|
|
|
|
def relations_map_for_account(account, account_ids, domains)
|
|
|
|
{
|
|
|
|
blocking: Account.blocking_map(account_ids, account.id),
|
|
|
|
blocked_by: Account.blocked_by_map(account_ids, account.id),
|
|
|
|
muting: Account.muting_map(account_ids, account.id),
|
|
|
|
following: Account.following_map(account_ids, account.id),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|