2023-08-24 17:40:04 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class StatusesSearchService < BaseService
|
|
|
|
def call(query, account = nil, options = {})
|
|
|
|
@query = query&.strip
|
|
|
|
@account = account
|
|
|
|
@options = options
|
|
|
|
@limit = options[:limit].to_i
|
|
|
|
@offset = options[:offset].to_i
|
|
|
|
|
|
|
|
status_search_results
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def status_search_results
|
|
|
|
definition = parsed_query.apply(
|
2023-08-25 18:01:56 +03:00
|
|
|
Chewy::Search::Request.new(StatusesIndex, PublicStatusesIndex).filter(
|
2023-08-24 17:40:04 +03:00
|
|
|
bool: {
|
|
|
|
should: [
|
|
|
|
publicly_searchable,
|
|
|
|
non_publicly_searchable,
|
|
|
|
],
|
|
|
|
|
|
|
|
minimum_should_match: 1,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-08-25 19:16:08 +03:00
|
|
|
results = definition.collapse(field: :id).order(id: { order: :desc }).limit(@limit).offset(@offset).objects.compact
|
2023-08-24 17:40:04 +03:00
|
|
|
account_ids = results.map(&:account_id)
|
|
|
|
account_domains = results.map(&:account_domain)
|
|
|
|
preloaded_relations = @account.relations_map(account_ids, account_domains)
|
|
|
|
|
|
|
|
results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? }
|
|
|
|
rescue Faraday::ConnectionFailed, Parslet::ParseFailed
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
def publicly_searchable
|
|
|
|
{
|
2023-08-25 18:01:56 +03:00
|
|
|
term: { _index: PublicStatusesIndex.index_name },
|
2023-08-24 17:40:04 +03:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def non_publicly_searchable
|
|
|
|
{
|
|
|
|
bool: {
|
|
|
|
must: [
|
|
|
|
{
|
2023-08-25 18:01:56 +03:00
|
|
|
term: { _index: StatusesIndex.index_name },
|
2023-08-24 17:40:04 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
term: { searchable_by: @account.id },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def parsed_query
|
2023-08-28 13:43:00 +03:00
|
|
|
SearchQueryTransformer.new.apply(SearchQueryParser.new.parse(@query), current_account: @account)
|
2023-08-24 17:40:04 +03:00
|
|
|
end
|
|
|
|
end
|