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.

103 lines
3.0 KiB

  1. # frozen_string_literal: true
  2. class ApiController < ApplicationController
  3. DEFAULT_STATUSES_LIMIT = 20
  4. DEFAULT_ACCOUNTS_LIMIT = 40
  5. protect_from_forgery with: :null_session
  6. skip_before_action :verify_authenticity_token
  7. skip_before_action :store_current_location
  8. before_action :set_rate_limit_headers
  9. rescue_from ActiveRecord::RecordInvalid, Mastodon::ValidationError do |e|
  10. render json: { error: e.to_s }, status: 422
  11. end
  12. rescue_from ActiveRecord::RecordNotFound do
  13. render json: { error: 'Record not found' }, status: 404
  14. end
  15. rescue_from Goldfinger::Error do
  16. render json: { error: 'Remote account could not be resolved' }, status: 422
  17. end
  18. rescue_from HTTP::Error do
  19. render json: { error: 'Remote data could not be fetched' }, status: 503
  20. end
  21. rescue_from OpenSSL::SSL::SSLError do
  22. render json: { error: 'Remote SSL certificate could not be verified' }, status: 503
  23. end
  24. rescue_from Mastodon::NotPermittedError do
  25. render json: { error: 'This action is not allowed' }, status: 403
  26. end
  27. def doorkeeper_unauthorized_render_options(error: nil)
  28. { json: { error: (error.try(:description) || 'Not authorized') } }
  29. end
  30. def doorkeeper_forbidden_render_options(*)
  31. { json: { error: 'This action is outside the authorized scopes' } }
  32. end
  33. protected
  34. def set_rate_limit_headers
  35. return if request.env['rack.attack.throttle_data'].nil?
  36. now = Time.now.utc
  37. match_data = request.env['rack.attack.throttle_data']['api']
  38. response.headers['X-RateLimit-Limit'] = match_data[:limit].to_s
  39. response.headers['X-RateLimit-Remaining'] = (match_data[:limit] - match_data[:count]).to_s
  40. response.headers['X-RateLimit-Reset'] = (now + (match_data[:period] - now.to_i % match_data[:period])).iso8601(6)
  41. end
  42. def set_pagination_headers(next_path = nil, prev_path = nil)
  43. links = []
  44. links << [next_path, [%w(rel next)]] if next_path
  45. links << [prev_path, [%w(rel prev)]] if prev_path
  46. response.headers['Link'] = LinkHeader.new(links)
  47. end
  48. def limit_param(default_limit)
  49. return default_limit unless params[:limit]
  50. [params[:limit].to_i.abs, default_limit * 2].min
  51. end
  52. def current_resource_owner
  53. @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
  54. end
  55. def current_user
  56. current_resource_owner || super
  57. rescue ActiveRecord::RecordNotFound
  58. nil
  59. end
  60. def require_user!
  61. current_resource_owner
  62. set_user_activity
  63. rescue ActiveRecord::RecordNotFound
  64. render json: { error: 'This method requires an authenticated user' }, status: 422
  65. end
  66. def render_empty
  67. render json: {}, status: 200
  68. end
  69. def set_maps(statuses) # rubocop:disable Style/AccessorMethodName
  70. if current_account.nil?
  71. @reblogs_map = {}
  72. @favourites_map = {}
  73. return
  74. end
  75. status_ids = statuses.compact.flat_map { |s| [s.id, s.reblog_of_id] }.uniq
  76. @reblogs_map = Status.reblogs_map(status_ids, current_account)
  77. @favourites_map = Status.favourites_map(status_ids, current_account)
  78. end
  79. end