search_service.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # frozen_string_literal: true
  2. class SearchService < BaseService
  3. def call(query, account, limit, options = {})
  4. @query = query&.strip
  5. @account = account
  6. @options = options
  7. @limit = limit.to_i
  8. @offset = options[:type].blank? ? 0 : options[:offset].to_i
  9. @resolve = options[:resolve] || false
  10. default_results.tap do |results|
  11. next if @query.blank? || @limit.zero?
  12. if url_query?
  13. results.merge!(url_resource_results) unless url_resource.nil? || @offset.positive? || (@options[:type].present? && url_resource_symbol != @options[:type].to_sym)
  14. elsif @query.present?
  15. results[:accounts] = perform_accounts_search! if account_searchable?
  16. results[:statuses] = perform_statuses_search! if full_text_searchable?
  17. results[:hashtags] = perform_hashtags_search! if hashtag_searchable?
  18. end
  19. end
  20. end
  21. private
  22. def perform_accounts_search!
  23. AccountSearchService.new.call(
  24. @query,
  25. @account,
  26. limit: @limit,
  27. resolve: @resolve,
  28. offset: @offset
  29. )
  30. end
  31. def perform_statuses_search!
  32. definition = parsed_query.apply(StatusesIndex.filter(term: { searchable_by: @account.id }))
  33. if @options[:account_id].present?
  34. definition = definition.filter(term: { account_id: @options[:account_id] })
  35. end
  36. if @options[:min_id].present? || @options[:max_id].present?
  37. range = {}
  38. range[:gt] = @options[:min_id].to_i if @options[:min_id].present?
  39. range[:lt] = @options[:max_id].to_i if @options[:max_id].present?
  40. definition = definition.filter(range: { id: range })
  41. end
  42. results = definition.limit(@limit).offset(@offset).objects.compact
  43. account_ids = results.map(&:account_id)
  44. account_domains = results.map(&:account_domain)
  45. preloaded_relations = relations_map_for_account(@account, account_ids, account_domains)
  46. results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? }
  47. rescue Faraday::ConnectionFailed, Parslet::ParseFailed
  48. []
  49. end
  50. def perform_hashtags_search!
  51. TagSearchService.new.call(
  52. @query,
  53. limit: @limit,
  54. offset: @offset,
  55. exclude_unreviewed: @options[:exclude_unreviewed]
  56. )
  57. end
  58. def default_results
  59. { accounts: [], hashtags: [], statuses: [] }
  60. end
  61. def url_query?
  62. @resolve && @query =~ /\Ahttps?:\/\//
  63. end
  64. def url_resource_results
  65. { url_resource_symbol => [url_resource] }
  66. end
  67. def url_resource
  68. @_url_resource ||= ResolveURLService.new.call(@query, on_behalf_of: @account)
  69. end
  70. def url_resource_symbol
  71. url_resource.class.name.downcase.pluralize.to_sym
  72. end
  73. def full_text_searchable?
  74. return false unless Chewy.enabled?
  75. statuses_search? && !@account.nil? && !((@query.start_with?('#') || @query.include?('@')) && !@query.include?(' '))
  76. end
  77. def account_searchable?
  78. account_search? && !(@query.start_with?('#') || (@query.include?('@') && @query.include?(' ')))
  79. end
  80. def hashtag_searchable?
  81. hashtag_search? && !@query.include?('@')
  82. end
  83. def account_search?
  84. @options[:type].blank? || @options[:type] == 'accounts'
  85. end
  86. def hashtag_search?
  87. @options[:type].blank? || @options[:type] == 'hashtags'
  88. end
  89. def statuses_search?
  90. @options[:type].blank? || @options[:type] == 'statuses'
  91. end
  92. def relations_map_for_account(account, account_ids, domains)
  93. {
  94. blocking: Account.blocking_map(account_ids, account.id),
  95. blocked_by: Account.blocked_by_map(account_ids, account.id),
  96. muting: Account.muting_map(account_ids, account.id),
  97. following: Account.following_map(account_ids, account.id),
  98. domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, account.id),
  99. }
  100. end
  101. def parsed_query
  102. SearchQueryTransformer.new.apply(SearchQueryParser.new.parse(@query))
  103. end
  104. end