]> cat aescling's git repositories - mastodon.git/blobdiff - app/services/search_service.rb
Optimize some regex matching (#15528)
[mastodon.git] / app / services / search_service.rb
index 04de8a1347cfc2f7dfd6b4d2eef05509d495e1a6..1a76cbb388316cd416a8ff3d41eb490331e55571 100644 (file)
 # frozen_string_literal: true
 
 class SearchService < BaseService
-  def call(query, limit, resolve = false)
-    return if query.blank? || query.start_with?('#')
+  def call(query, account, limit, options = {})
+    @query   = query&.strip
+    @account = account
+    @options = options
+    @limit   = limit.to_i
+    @offset  = options[:type].blank? ? 0 : options[:offset].to_i
+    @resolve = options[:resolve] || false
 
-    username, domain = query.gsub(/\A@/, '').split('@')
+    default_results.tap do |results|
+      next if @query.blank? || @limit.zero?
 
-    if domain.nil?
-      exact_match = Account.find_local(username)
-      results     = Account.search_for(username)
-    else
-      exact_match = Account.find_remote(username, domain)
-      results     = Account.search_for("#{username} #{domain}")
+      if url_query?
+        results.merge!(url_resource_results) unless url_resource.nil? || @offset.positive? || (@options[:type].present? && url_resource_symbol != @options[:type].to_sym)
+      elsif @query.present?
+        results[:accounts] = perform_accounts_search! if account_searchable?
+        results[:statuses] = perform_statuses_search! if full_text_searchable?
+        results[:hashtags] = perform_hashtags_search! if hashtag_searchable?
+      end
     end
+  end
+
+  private
 
-    results = results.limit(limit).to_a
-    results = [exact_match] + results.reject { |a| a.id == exact_match.id } if exact_match
+  def perform_accounts_search!
+    AccountSearchService.new.call(
+      @query,
+      @account,
+      limit: @limit,
+      resolve: @resolve,
+      offset: @offset
+    )
+  end
 
-    if resolve && !exact_match && !domain.nil?
-      results = [FollowRemoteAccountService.new.call("#{username}@#{domain}")]
+  def perform_statuses_search!
+    definition = parsed_query.apply(StatusesIndex.filter(term: { searchable_by: @account.id }))
+
+    if @options[:account_id].present?
+      definition = definition.filter(term: { account_id: @options[:account_id] })
     end
 
-    results
+    if @options[:min_id].present? || @options[:max_id].present?
+      range      = {}
+      range[:gt] = @options[:min_id].to_i if @options[:min_id].present?
+      range[:lt] = @options[:max_id].to_i if @options[:max_id].present?
+      definition = definition.filter(range: { id: range })
+    end
+
+    results             = definition.limit(@limit).offset(@offset).objects.compact
+    account_ids         = results.map(&:account_id)
+    account_domains     = results.map(&:account_domain)
+    preloaded_relations = relations_map_for_account(@account, account_ids, account_domains)
+
+    results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? }
+  rescue Faraday::ConnectionFailed, Parslet::ParseFailed
+    []
+  end
+
+  def perform_hashtags_search!
+    TagSearchService.new.call(
+      @query,
+      limit: @limit,
+      offset: @offset,
+      exclude_unreviewed: @options[:exclude_unreviewed]
+    )
+  end
+
+  def default_results
+    { accounts: [], hashtags: [], statuses: [] }
+  end
+
+  def url_query?
+    @resolve && /\Ahttps?:\/\//.match?(@query)
+  end
+
+  def url_resource_results
+    { url_resource_symbol => [url_resource] }
+  end
+
+  def url_resource
+    @_url_resource ||= ResolveURLService.new.call(@query, on_behalf_of: @account)
+  end
+
+  def url_resource_symbol
+    url_resource.class.name.downcase.pluralize.to_sym
+  end
+
+  def full_text_searchable?
+    return false unless Chewy.enabled?
+
+    statuses_search? && !@account.nil? && !((@query.start_with?('#') || @query.include?('@')) && !@query.include?(' '))
+  end
+
+  def account_searchable?
+    account_search? && !(@query.start_with?('#') || (@query.include?('@') && @query.include?(' ')))
+  end
+
+  def hashtag_searchable?
+    hashtag_search? && !@query.include?('@')
+  end
+
+  def account_search?
+    @options[:type].blank? || @options[:type] == 'accounts'
+  end
+
+  def hashtag_search?
+    @options[:type].blank? || @options[:type] == 'hashtags'
+  end
+
+  def statuses_search?
+    @options[:type].blank? || @options[:type] == 'statuses'
+  end
+
+  def relations_map_for_account(account, account_ids, domains)
+    {
+      blocking: Account.blocking_map(account_ids, account.id),
+      blocked_by: Account.blocked_by_map(account_ids, account.id),
+      muting: Account.muting_map(account_ids, account.id),
+      following: Account.following_map(account_ids, account.id),
+      domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, account.id),
+    }
+  end
+
+  def parsed_query
+    SearchQueryTransformer.new.apply(SearchQueryParser.new.parse(@query))
   end
 end
This page took 0.035597 seconds and 3 git commands to generate.