search_controller.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # frozen_string_literal: true
  2. class Api::V2::SearchController < Api::BaseController
  3. include Authorization
  4. RESULTS_LIMIT = 20
  5. before_action -> { authorize_if_got_token! :read, :'read:search' }
  6. before_action :validate_search_params!
  7. def index
  8. @search = Search.new(search_results)
  9. render json: @search, serializer: REST::SearchSerializer
  10. rescue Mastodon::SyntaxError
  11. unprocessable_entity
  12. rescue ActiveRecord::RecordNotFound
  13. not_found
  14. end
  15. private
  16. def validate_search_params!
  17. params.require(:q)
  18. return if user_signed_in?
  19. return render json: { error: 'Search queries pagination is not supported without authentication' }, status: 401 if params[:offset].present?
  20. render json: { error: 'Search queries that resolve remote resources are not supported without authentication' }, status: 401 if truthy_param?(:resolve)
  21. end
  22. def search_results
  23. SearchService.new.call(
  24. params[:q],
  25. current_account,
  26. limit_param(RESULTS_LIMIT),
  27. search_params.merge(resolve: truthy_param?(:resolve), exclude_unreviewed: truthy_param?(:exclude_unreviewed))
  28. )
  29. end
  30. def search_params
  31. params.permit(:type, :offset, :min_id, :max_id, :account_id)
  32. end
  33. end