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.

115 lines
4.4 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, 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. super || current_resource_owner
  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. return
  73. end
  74. status_ids = statuses.compact.flat_map { |s| [s.id, s.reblog_of_id] }.uniq
  75. @reblogs_map = Status.reblogs_map(status_ids, current_account)
  76. @favourites_map = Status.favourites_map(status_ids, current_account)
  77. end
  78. def set_counters_maps(statuses) # rubocop:disable Style/AccessorMethodName
  79. status_ids = statuses.compact.map { |s| s.reblog? ? s.reblog_of_id : s.id }.uniq
  80. @favourites_counts_map = Favourite.select('status_id, COUNT(id) AS favourites_count').group('status_id').where(status_id: status_ids).map { |f| [f.status_id, f.favourites_count] }.to_h
  81. @reblogs_counts_map = Status.select('statuses.id, COUNT(reblogs.id) AS reblogs_count').joins('LEFT OUTER JOIN statuses AS reblogs ON statuses.id = reblogs.reblog_of_id').where(id: status_ids).group('statuses.id').map { |r| [r.id, r.reblogs_count] }.to_h
  82. end
  83. def set_account_counters_maps(accounts) # rubocop:disable Style/AccessorMethodName
  84. account_ids = accounts.compact.map(&:id).uniq
  85. @followers_counts_map = Follow.unscoped.select('target_account_id, COUNT(account_id) AS followers_count').group('target_account_id').where(target_account_id: account_ids).map { |f| [f.target_account_id, f.followers_count] }.to_h
  86. @following_counts_map = Follow.unscoped.select('account_id, COUNT(target_account_id) AS following_count').group('account_id').where(account_id: account_ids).map { |f| [f.account_id, f.following_count] }.to_h
  87. @statuses_counts_map = Status.unscoped.select('account_id, COUNT(id) AS statuses_count').group('account_id').where(account_id: account_ids).map { |s| [s.account_id, s.statuses_count] }.to_h
  88. end
  89. end