search_service.rb 3.6 KB

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