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

90 lines
2.6 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 HTTP::Error, Mastodon::UnexpectedResponseError do
  15. render json: { error: 'Remote data could not be fetched' }, status: 503
  16. end
  17. rescue_from OpenSSL::SSL::SSLError do
  18. render json: { error: 'Remote SSL certificate could not be verified' }, status: 503
  19. end
  20. rescue_from Mastodon::NotPermittedError do
  21. render json: { error: 'This action is not allowed' }, status: 403
  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_pagination_headers(next_path = nil, prev_path = nil)
  31. links = []
  32. links << [next_path, [%w(rel next)]] if next_path
  33. links << [prev_path, [%w(rel prev)]] if prev_path
  34. response.headers['Link'] = LinkHeader.new(links) unless links.empty?
  35. end
  36. def limit_param(default_limit)
  37. return default_limit unless params[:limit]
  38. [params[:limit].to_i.abs, default_limit * 2].min
  39. end
  40. def current_resource_owner
  41. @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
  42. end
  43. def current_user
  44. current_resource_owner || super
  45. rescue ActiveRecord::RecordNotFound
  46. nil
  47. end
  48. def require_user!
  49. if current_user
  50. set_user_activity
  51. else
  52. render json: { error: 'This method requires an authenticated user' }, status: 422
  53. end
  54. end
  55. def render_empty
  56. render json: {}, status: 200
  57. end
  58. def set_maps(statuses) # rubocop:disable Style/AccessorMethodName
  59. if current_account.nil?
  60. @reblogs_map = {}
  61. @favourites_map = {}
  62. @mutes_map = {}
  63. return
  64. end
  65. status_ids = statuses.compact.flat_map { |s| [s.id, s.reblog_of_id] }.uniq
  66. conversation_ids = statuses.compact.map(&:conversation_id).compact.uniq
  67. @reblogs_map = Status.reblogs_map(status_ids, current_account)
  68. @favourites_map = Status.favourites_map(status_ids, current_account)
  69. @mutes_map = Status.mutes_map(conversation_ids, current_account)
  70. end
  71. end