闭社主体 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.

104 lines
3.2 KiB

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