1 # frozen_string_literal: true
3 class ApiController
< ApplicationController
4 DEFAULT_STATUSES_LIMIT
= 20
5 DEFAULT_ACCOUNTS_LIMIT
= 40
7 protect_from_forgery with
: :null_session
9 skip_before_action
:verify_authenticity_token
11 before_action
:set_rate_limit_headers
13 rescue_from ActiveRecord
::RecordInvalid do |e
|
14 render json
: { error
: e
.to_s
}, status
: 422
17 rescue_from ActiveRecord
::RecordNotFound do
18 render json
: { error
: 'Record not found' }, status
: 404
21 rescue_from Goldfinger
::Error do
22 render json
: { error
: 'Remote account could not be resolved' }, status
: 422
25 rescue_from HTTP
::Error do
26 render json
: { error
: 'Remote data could not be fetched' }, status
: 503
29 rescue_from OpenSSL
::SSL::SSLError do
30 render json
: { error
: 'Remote SSL certificate could not be verified' }, status
: 503
33 def doorkeeper_unauthorized_render_options(error
: nil)
34 { json
: { error
: (error
.try(:description) || 'Not authorized') } }
37 def doorkeeper_forbidden_render_options(*)
38 { json
: { error
: 'This action is outside the authorized scopes' } }
43 def set_rate_limit_headers
44 return if request
.env['rack.attack.throttle_data'].nil?
47 match_data
= request
.env['rack.attack.throttle_data']['api']
49 response
.headers
['X-RateLimit-Limit'] = match_data
[:limit].to_s
50 response
.headers
['X-RateLimit-Remaining'] = (match_data
[:limit] - match_data
[:count]).to_s
51 response
.headers
['X-RateLimit-Reset'] = (now +
(match_data
[:period] - now
.to_i
% match_data
[:period])).iso8601(6)
54 def set_pagination_headers(next_path
= nil, prev_path
= nil)
56 links
<< [next_path
, [%w(rel
next)]] if next_path
57 links
<< [prev_path
, [%w(rel prev
)]] if prev_path
58 response
.headers
['Link'] = LinkHeader
.new(links
)
61 def current_resource_owner
62 @current_user ||= User
.find(doorkeeper_token
.resource_owner_id
) if doorkeeper_token
66 super || current_resource_owner
67 rescue ActiveRecord
::RecordNotFound
72 current_resource_owner
73 rescue ActiveRecord
::RecordNotFound
74 render json
: { error
: 'This method requires an authenticated user' }, status
: 422
78 render json
: {}, status
: 200
81 def set_maps(statuses
) # rubocop:disable Style/AccessorMethodName
82 if current_account
.nil?
88 status_ids
= statuses
.flat_map
{ |s
| [s
.id
, s
.reblog_of_id
] }.compact
.uniq
89 @reblogs_map = Status
.reblogs_map(status_ids
, current_account
)
90 @favourites_map = Status
.favourites_map(status_ids
, current_account
)
93 def set_counters_maps(statuses
) # rubocop:disable Style/AccessorMethodName
94 status_ids
= statuses
.map
{ |s
| s
.reblog
? ? s
.reblog_of_id
: s
.id
}.uniq
95 @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
96 @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
99 def set_account_counters_maps(accounts
) # rubocop:disable Style/AccessorMethodName
100 account_ids
= accounts
.map(&:id)
101 @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
102 @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
103 @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