You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
960 B

  1. # frozen_string_literal: true
  2. class SearchService < BaseService
  3. attr_accessor :query
  4. def call(query, limit, resolve = false, account = nil)
  5. @query = query
  6. default_results.tap do |results|
  7. if url_query?
  8. results.merge!(remote_resource_results) unless remote_resource.nil?
  9. elsif query.present?
  10. results[:accounts] = AccountSearchService.new.call(query, limit, resolve, account)
  11. results[:hashtags] = Tag.search_for(query.gsub(/\A#/, ''), limit) unless query.start_with?('@')
  12. end
  13. end
  14. end
  15. def default_results
  16. { accounts: [], hashtags: [], statuses: [] }
  17. end
  18. def url_query?
  19. query =~ /\Ahttps?:\/\//
  20. end
  21. def remote_resource_results
  22. { remote_resource_symbol => [remote_resource] }
  23. end
  24. def remote_resource
  25. @_remote_resource ||= FetchRemoteResourceService.new.call(query)
  26. end
  27. def remote_resource_symbol
  28. remote_resource.class.name.downcase.pluralize.to_sym
  29. end
  30. end