statuses_controller.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # frozen_string_literal: true
  2. class Api::V1::StatusesController < Api::BaseController
  3. include Authorization
  4. before_action -> { authorize_if_got_token! :read, :'read:statuses' }, except: [:create, :update, :destroy]
  5. before_action -> { doorkeeper_authorize! :write, :'write:statuses' }, only: [:create, :update, :destroy]
  6. before_action :require_user!, except: [:show, :context]
  7. before_action :set_status, only: [:show, :context]
  8. before_action :set_thread, only: [:create]
  9. override_rate_limit_headers :create, family: :statuses
  10. override_rate_limit_headers :update, family: :statuses
  11. # This API was originally unlimited, pagination cannot be introduced without
  12. # breaking backwards-compatibility. Arbitrarily high number to cover most
  13. # conversations as quasi-unlimited, it would be too much work to render more
  14. # than this anyway
  15. CONTEXT_LIMIT = 4_096
  16. # This remains expensive and we don't want to show everything to logged-out users
  17. ANCESTORS_LIMIT = 40
  18. DESCENDANTS_LIMIT = 60
  19. DESCENDANTS_DEPTH_LIMIT = 20
  20. def show
  21. @status = cache_collection([@status], Status).first
  22. render json: @status, serializer: REST::StatusSerializer
  23. end
  24. def context
  25. ancestors_limit = CONTEXT_LIMIT
  26. descendants_limit = CONTEXT_LIMIT
  27. descendants_depth_limit = nil
  28. if current_account.nil?
  29. ancestors_limit = ANCESTORS_LIMIT
  30. descendants_limit = DESCENDANTS_LIMIT
  31. descendants_depth_limit = DESCENDANTS_DEPTH_LIMIT
  32. end
  33. ancestors_results = @status.in_reply_to_id.nil? ? [] : @status.ancestors(ancestors_limit, current_account)
  34. descendants_results = @status.descendants(descendants_limit, current_account, nil, nil, descendants_depth_limit)
  35. loaded_ancestors = cache_collection(ancestors_results, Status)
  36. loaded_descendants = cache_collection(descendants_results, Status)
  37. @context = Context.new(ancestors: loaded_ancestors, descendants: loaded_descendants)
  38. statuses = [@status] + @context.ancestors + @context.descendants
  39. render json: @context, serializer: REST::ContextSerializer, relationships: StatusRelationshipsPresenter.new(statuses, current_user&.account_id)
  40. end
  41. def create
  42. @status = PostStatusService.new.call(
  43. current_user.account,
  44. text: status_params[:status],
  45. thread: @thread,
  46. media_ids: status_params[:media_ids],
  47. sensitive: status_params[:sensitive],
  48. spoiler_text: status_params[:spoiler_text],
  49. visibility: status_params[:visibility],
  50. language: status_params[:language],
  51. scheduled_at: status_params[:scheduled_at],
  52. application: doorkeeper_token.application,
  53. poll: status_params[:poll],
  54. idempotency: request.headers['Idempotency-Key'],
  55. with_rate_limit: true
  56. )
  57. render json: @status, serializer: @status.is_a?(ScheduledStatus) ? REST::ScheduledStatusSerializer : REST::StatusSerializer
  58. end
  59. def update
  60. @status = Status.where(account: current_account).find(params[:id])
  61. authorize @status, :update?
  62. UpdateStatusService.new.call(
  63. @status,
  64. current_account.id,
  65. text: status_params[:status],
  66. media_ids: status_params[:media_ids],
  67. sensitive: status_params[:sensitive],
  68. language: status_params[:language],
  69. spoiler_text: status_params[:spoiler_text],
  70. poll: status_params[:poll]
  71. )
  72. render json: @status, serializer: REST::StatusSerializer
  73. end
  74. def destroy
  75. @status = Status.where(account: current_account).find(params[:id])
  76. authorize @status, :destroy?
  77. @status.discard_with_reblogs
  78. StatusPin.find_by(status: @status)&.destroy
  79. @status.account.statuses_count = @status.account.statuses_count - 1
  80. json = render_to_body json: @status, serializer: REST::StatusSerializer, source_requested: true
  81. RemovalWorker.perform_async(@status.id, { 'redraft' => true })
  82. render json: json
  83. end
  84. private
  85. def set_status
  86. @status = Status.find(params[:id])
  87. authorize @status, :show?
  88. rescue Mastodon::NotPermittedError
  89. not_found
  90. end
  91. def set_thread
  92. @thread = Status.find(status_params[:in_reply_to_id]) if status_params[:in_reply_to_id].present?
  93. authorize(@thread, :show?) if @thread.present?
  94. rescue ActiveRecord::RecordNotFound, Mastodon::NotPermittedError
  95. render json: { error: I18n.t('statuses.errors.in_reply_not_found') }, status: 404
  96. end
  97. def status_params
  98. params.permit(
  99. :status,
  100. :in_reply_to_id,
  101. :sensitive,
  102. :spoiler_text,
  103. :visibility,
  104. :language,
  105. :scheduled_at,
  106. media_ids: [],
  107. poll: [
  108. :multiple,
  109. :hide_totals,
  110. :expires_in,
  111. options: [],
  112. ]
  113. )
  114. end
  115. def pagination_params(core_params)
  116. params.slice(:limit).permit(:limit).merge(core_params)
  117. end
  118. end