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
|
|
|
|
|
2023-09-08 15:25:00 +03:00
|
|
|
convert_deprecated_options!
|
2023-08-24 17:40:04 +03:00
|
|
|
status_search_results
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def status_search_results
|
2023-09-04 18:20:35 +03:00
|
|
|
request = parsed_query.request
|
|
|
|
results = request.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 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
|
2023-09-08 15:25:00 +03:00
|
|
|
|
|
|
|
def convert_deprecated_options!
|
|
|
|
syntax_options = []
|
|
|
|
|
|
|
|
if @options[:account_id]
|
|
|
|
username = Account.select(:username, :domain).find(@options[:account_id]).acct
|
|
|
|
syntax_options << "from:@#{username}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if @options[:min_id]
|
2023-09-15 11:23:24 +03:00
|
|
|
timestamp = Mastodon::Snowflake.to_time(@options[:min_id].to_i)
|
2023-09-08 15:25:00 +03:00
|
|
|
syntax_options << "after:\"#{timestamp.iso8601}\""
|
|
|
|
end
|
|
|
|
|
|
|
|
if @options[:max_id]
|
2023-09-15 11:23:24 +03:00
|
|
|
timestamp = Mastodon::Snowflake.to_time(@options[:max_id].to_i)
|
2023-09-08 15:25:00 +03:00
|
|
|
syntax_options << "before:\"#{timestamp.iso8601}\""
|
|
|
|
end
|
|
|
|
|
|
|
|
@query = "#{@query} #{syntax_options.join(' ')}".strip if syntax_options.any?
|
|
|
|
end
|
2023-08-24 17:40:04 +03:00
|
|
|
end
|