1
0

statuses_search_service.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # frozen_string_literal: true
  2. class StatusesSearchService < BaseService
  3. def call(query, account = nil, options = {})
  4. @query = query&.strip
  5. @account = account
  6. @options = options
  7. @limit = options[:limit].to_i
  8. @offset = options[:offset].to_i
  9. convert_deprecated_options!
  10. status_search_results
  11. end
  12. private
  13. def status_search_results
  14. request = parsed_query.request
  15. results = request.collapse(field: :id).order(id: { order: :desc }).limit(@limit).offset(@offset).objects.compact
  16. account_ids = results.map(&:account_id)
  17. account_domains = results.map(&:account_domain)
  18. preloaded_relations = @account.relations_map(account_ids, account_domains)
  19. results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? }
  20. rescue Faraday::ConnectionFailed, Parslet::ParseFailed
  21. []
  22. end
  23. def parsed_query
  24. SearchQueryTransformer.new.apply(SearchQueryParser.new.parse(@query), current_account: @account)
  25. end
  26. def convert_deprecated_options!
  27. syntax_options = []
  28. if @options[:account_id]
  29. username = Account.select(:username, :domain).find(@options[:account_id]).acct
  30. syntax_options << "from:@#{username}"
  31. end
  32. if @options[:min_id]
  33. timestamp = Mastodon::Snowflake.to_time(@options[:min_id].to_i)
  34. syntax_options << "after:\"#{timestamp.iso8601}\""
  35. end
  36. if @options[:max_id]
  37. timestamp = Mastodon::Snowflake.to_time(@options[:max_id].to_i)
  38. syntax_options << "before:\"#{timestamp.iso8601}\""
  39. end
  40. @query = "#{@query} #{syntax_options.join(' ')}".strip if syntax_options.any?
  41. end
  42. end