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.

109 lines
4.1 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. rescue_from Mastodon::NotPermitted 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 current_resource_owner
  48. @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
  49. end
  50. def current_user
  51. super || current_resource_owner
  52. rescue ActiveRecord::RecordNotFound
  53. nil
  54. end
  55. def require_user!
  56. current_resource_owner
  57. rescue ActiveRecord::RecordNotFound
  58. render json: { error: 'This method requires an authenticated user' }, status: 422
  59. end
  60. def render_empty
  61. render json: {}, status: 200
  62. end
  63. def set_maps(statuses) # rubocop:disable Style/AccessorMethodName
  64. if current_account.nil?
  65. @reblogs_map = {}
  66. @favourites_map = {}
  67. return
  68. end
  69. status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.compact.uniq
  70. @reblogs_map = Status.reblogs_map(status_ids, current_account)
  71. @favourites_map = Status.favourites_map(status_ids, current_account)
  72. end
  73. def set_counters_maps(statuses) # rubocop:disable Style/AccessorMethodName
  74. status_ids = statuses.map { |s| s.reblog? ? s.reblog_of_id : s.id }.uniq
  75. @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
  76. @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
  77. end
  78. def set_account_counters_maps(accounts) # rubocop:disable Style/AccessorMethodName
  79. account_ids = accounts.map(&:id)
  80. @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
  81. @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
  82. @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
  83. end
  84. end