]> cat aescling's git repositories - mastodon.git/blob - app/controllers/api_controller.rb
d2d3bc4a4bb3e2bb55d1c989e211f49766495eec
[mastodon.git] / app / controllers / api_controller.rb
1 # frozen_string_literal: true
2
3 class ApiController < ApplicationController
4 DEFAULT_STATUSES_LIMIT = 20
5 DEFAULT_ACCOUNTS_LIMIT = 40
6
7 protect_from_forgery with: :null_session
8
9 skip_before_action :verify_authenticity_token
10
11 before_action :set_rate_limit_headers
12
13 rescue_from ActiveRecord::RecordInvalid do |e|
14 render json: { error: e.to_s }, status: 422
15 end
16
17 rescue_from ActiveRecord::RecordNotFound do
18 render json: { error: 'Record not found' }, status: 404
19 end
20
21 rescue_from Goldfinger::Error do
22 render json: { error: 'Remote account could not be resolved' }, status: 422
23 end
24
25 rescue_from HTTP::Error do
26 render json: { error: 'Remote data could not be fetched' }, status: 503
27 end
28
29 rescue_from OpenSSL::SSL::SSLError do
30 render json: { error: 'Remote SSL certificate could not be verified' }, status: 503
31 end
32
33 def doorkeeper_unauthorized_render_options(error: nil)
34 { json: { error: (error.try(:description) || 'Not authorized') } }
35 end
36
37 def doorkeeper_forbidden_render_options(*)
38 { json: { error: 'This action is outside the authorized scopes' } }
39 end
40
41 protected
42
43 def set_rate_limit_headers
44 return if request.env['rack.attack.throttle_data'].nil?
45
46 now = Time.now.utc
47 match_data = request.env['rack.attack.throttle_data']['api']
48
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)
52 end
53
54 def set_pagination_headers(next_path = nil, prev_path = nil)
55 links = []
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)
59 end
60
61 def current_resource_owner
62 @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
63 end
64
65 def current_user
66 super || current_resource_owner
67 rescue ActiveRecord::RecordNotFound
68 nil
69 end
70
71 def require_user!
72 current_resource_owner
73 rescue ActiveRecord::RecordNotFound
74 render json: { error: 'This method requires an authenticated user' }, status: 422
75 end
76
77 def render_empty
78 render json: {}, status: 200
79 end
80
81 def set_maps(statuses) # rubocop:disable Style/AccessorMethodName
82 if current_account.nil?
83 @reblogs_map = {}
84 @favourites_map = {}
85 return
86 end
87
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)
91 end
92
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
97 end
98
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
104 end
105 end
This page took 0.100947 seconds and 2 git commands to generate.