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

105 lines
4.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. 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(error: nil)
  24. { json: { error: (error.try(:description) || '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 ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
  46. end
  47. def current_user
  48. @user ||= 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. def set_counters_maps(statuses) # rubocop:disable Style/AccessorMethodName
  71. status_ids = statuses.map { |s| s.reblog? ? s.reblog_of_id : s.id }.uniq
  72. @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
  73. @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
  74. end
  75. def set_account_counters_maps(accounts) # rubocop:disable Style/AccessorMethodName
  76. account_ids = accounts.map(&:id)
  77. @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
  78. @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
  79. @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
  80. end
  81. end