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.

97 lines
2.7 KiB

  1. # frozen_string_literal: true
  2. class Api::BaseController < ApplicationController
  3. DEFAULT_STATUSES_LIMIT = 20
  4. DEFAULT_ACCOUNTS_LIMIT = 40
  5. include RateLimitHeaders
  6. skip_before_action :store_current_location
  7. skip_before_action :require_functional!
  8. before_action :set_cache_headers
  9. protect_from_forgery with: :null_session
  10. rescue_from ActiveRecord::RecordInvalid, Mastodon::ValidationError do |e|
  11. render json: { error: e.to_s }, status: 422
  12. end
  13. rescue_from ActiveRecord::RecordNotFound do
  14. render json: { error: 'Record not found' }, status: 404
  15. end
  16. rescue_from HTTP::Error, Mastodon::UnexpectedResponseError do
  17. render json: { error: 'Remote data could not be fetched' }, status: 503
  18. end
  19. rescue_from OpenSSL::SSL::SSLError do
  20. render json: { error: 'Remote SSL certificate could not be verified' }, status: 503
  21. end
  22. rescue_from Mastodon::NotPermittedError do
  23. render json: { error: 'This action is not allowed' }, status: 403
  24. end
  25. def doorkeeper_unauthorized_render_options(error: nil)
  26. { json: { error: (error.try(:description) || 'Not authorized') } }
  27. end
  28. def doorkeeper_forbidden_render_options(*)
  29. { json: { error: 'This action is outside the authorized scopes' } }
  30. end
  31. protected
  32. def set_pagination_headers(next_path = nil, prev_path = nil)
  33. links = []
  34. links << [next_path, [%w(rel next)]] if next_path
  35. links << [prev_path, [%w(rel prev)]] if prev_path
  36. response.headers['Link'] = LinkHeader.new(links) unless links.empty?
  37. end
  38. def limit_param(default_limit)
  39. return default_limit unless params[:limit]
  40. [params[:limit].to_i.abs, default_limit * 2].min
  41. end
  42. def params_slice(*keys)
  43. params.slice(*keys).permit(*keys)
  44. end
  45. def current_resource_owner
  46. @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
  47. end
  48. def current_user
  49. current_resource_owner || super
  50. rescue ActiveRecord::RecordNotFound
  51. nil
  52. end
  53. def require_user!
  54. if !current_user
  55. render json: { error: 'This method requires an authenticated user' }, status: 422
  56. elsif current_user.disabled?
  57. render json: { error: 'Your login is currently disabled' }, status: 403
  58. elsif !current_user.confirmed?
  59. render json: { error: 'Your login is missing a confirmed e-mail address' }, status: 403
  60. elsif !current_user.approved?
  61. render json: { error: 'Your login is currently pending approval' }, status: 403
  62. else
  63. set_user_activity
  64. end
  65. end
  66. def render_empty
  67. render json: {}, status: 200
  68. end
  69. def authorize_if_got_token!(*scopes)
  70. doorkeeper_authorize!(*scopes) if doorkeeper_token
  71. end
  72. def set_cache_headers
  73. response.headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
  74. end
  75. end