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.

93 lines
2.7 KiB

  1. # frozen_string_literal: true
  2. class Api::BaseController < ApplicationController
  3. DEFAULT_STATUSES_LIMIT = 20
  4. DEFAULT_ACCOUNTS_LIMIT = 40
  5. include RateLimitHeaders
  6. skip_before_action :verify_authenticity_token
  7. skip_before_action :store_current_location
  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_pagination_headers(next_path = nil, prev_path = nil)
  34. links = []
  35. links << [next_path, [%w(rel next)]] if next_path
  36. links << [prev_path, [%w(rel prev)]] if prev_path
  37. response.headers['Link'] = LinkHeader.new(links)
  38. end
  39. def limit_param(default_limit)
  40. return default_limit unless params[:limit]
  41. [params[:limit].to_i.abs, default_limit * 2].min
  42. end
  43. def current_resource_owner
  44. @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
  45. end
  46. def current_user
  47. current_resource_owner || super
  48. rescue ActiveRecord::RecordNotFound
  49. nil
  50. end
  51. def require_user!
  52. current_resource_owner
  53. set_user_activity
  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. @mutes_map = {}
  65. return
  66. end
  67. status_ids = statuses.compact.flat_map { |s| [s.id, s.reblog_of_id] }.uniq
  68. conversation_ids = statuses.compact.map(&:conversation_id).compact.uniq
  69. @reblogs_map = Status.reblogs_map(status_ids, current_account)
  70. @favourites_map = Status.favourites_map(status_ids, current_account)
  71. @mutes_map = Status.mutes_map(conversation_ids, current_account)
  72. end
  73. end