The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

121 lines
3.4 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 :require_authenticated_user!, if: :disallow_unauthenticated_api_access?
  9. before_action :set_cache_headers
  10. protect_from_forgery with: :null_session
  11. skip_around_action :set_locale
  12. rescue_from ActiveRecord::RecordInvalid, Mastodon::ValidationError do |e|
  13. render json: { error: e.to_s }, status: 422
  14. end
  15. rescue_from ActiveRecord::RecordNotUnique do
  16. render json: { error: 'Duplicate record' }, status: 422
  17. end
  18. rescue_from ActiveRecord::RecordNotFound do
  19. render json: { error: 'Record not found' }, status: 404
  20. end
  21. rescue_from HTTP::Error, Mastodon::UnexpectedResponseError do
  22. render json: { error: 'Remote data could not be fetched' }, status: 503
  23. end
  24. rescue_from OpenSSL::SSL::SSLError do
  25. render json: { error: 'Remote SSL certificate could not be verified' }, status: 503
  26. end
  27. rescue_from Mastodon::NotPermittedError do
  28. render json: { error: 'This action is not allowed' }, status: 403
  29. end
  30. rescue_from Mastodon::RaceConditionError do
  31. render json: { error: 'There was a temporary problem serving your request, please try again' }, status: 503
  32. end
  33. rescue_from ActionController::ParameterMissing do |e|
  34. render json: { error: e.to_s }, status: 400
  35. end
  36. def doorkeeper_unauthorized_render_options(error: nil)
  37. { json: { error: (error.try(:description) || 'Not authorized') } }
  38. end
  39. def doorkeeper_forbidden_render_options(*)
  40. { json: { error: 'This action is outside the authorized scopes' } }
  41. end
  42. protected
  43. def set_pagination_headers(next_path = nil, prev_path = nil)
  44. links = []
  45. links << [next_path, [%w(rel next)]] if next_path
  46. links << [prev_path, [%w(rel prev)]] if prev_path
  47. response.headers['Link'] = LinkHeader.new(links) unless links.empty?
  48. end
  49. def limit_param(default_limit)
  50. return default_limit unless params[:limit]
  51. [params[:limit].to_i.abs, default_limit * 2].min
  52. end
  53. def params_slice(*keys)
  54. params.slice(*keys).permit(*keys)
  55. end
  56. def current_resource_owner
  57. @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
  58. end
  59. def current_user
  60. current_resource_owner || super
  61. rescue ActiveRecord::RecordNotFound
  62. nil
  63. end
  64. def require_authenticated_user!
  65. render json: { error: 'This method requires an authenticated user' }, status: 401 unless current_user
  66. end
  67. def require_user!
  68. if !current_user
  69. render json: { error: 'This method requires an authenticated user' }, status: 422
  70. elsif current_user.disabled?
  71. render json: { error: 'Your login is currently disabled' }, status: 403
  72. elsif !current_user.confirmed?
  73. render json: { error: 'Your login is missing a confirmed e-mail address' }, status: 403
  74. elsif !current_user.approved?
  75. render json: { error: 'Your login is currently pending approval' }, status: 403
  76. else
  77. set_user_activity
  78. end
  79. end
  80. def render_empty
  81. render json: {}, status: 200
  82. end
  83. def authorize_if_got_token!(*scopes)
  84. doorkeeper_authorize!(*scopes) if doorkeeper_token
  85. end
  86. def set_cache_headers
  87. response.headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
  88. end
  89. def disallow_unauthenticated_api_access?
  90. authorized_fetch_mode?
  91. end
  92. end