闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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