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.

101 lines
3.8 KiB

  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, :destroy]
  5. before_action -> { doorkeeper_authorize! :write, :'write:statuses' }, only: [:create, :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. # This API was originally unlimited, pagination cannot be introduced without
  11. # breaking backwards-compatibility. Arbitrarily high number to cover most
  12. # conversations as quasi-unlimited, it would be too much work to render more
  13. # than this anyway
  14. CONTEXT_LIMIT = 4_096
  15. def show
  16. @status = cache_collection([@status], Status).first
  17. render json: @status, serializer: REST::StatusSerializer
  18. end
  19. def context
  20. ancestors_results = @status.in_reply_to_id.nil? ? [] : @status.ancestors(CONTEXT_LIMIT, current_account)
  21. descendants_results = @status.descendants(CONTEXT_LIMIT, current_account)
  22. loaded_ancestors = cache_collection(ancestors_results, Status)
  23. loaded_descendants = cache_collection(descendants_results, Status)
  24. @context = Context.new(ancestors: loaded_ancestors, descendants: loaded_descendants)
  25. statuses = [@status] + @context.ancestors + @context.descendants
  26. render json: @context, serializer: REST::ContextSerializer, relationships: StatusRelationshipsPresenter.new(statuses, current_user&.account_id)
  27. end
  28. def create
  29. @status = PostStatusService.new.call(current_user.account,
  30. text: status_params[:status],
  31. thread: @thread,
  32. media_ids: status_params[:media_ids],
  33. sensitive: status_params[:sensitive],
  34. spoiler_text: status_params[:spoiler_text],
  35. visibility: status_params[:visibility],
  36. scheduled_at: status_params[:scheduled_at],
  37. application: doorkeeper_token.application,
  38. poll: status_params[:poll],
  39. idempotency: request.headers['Idempotency-Key'],
  40. with_rate_limit: true)
  41. render json: @status, serializer: @status.is_a?(ScheduledStatus) ? REST::ScheduledStatusSerializer : REST::StatusSerializer
  42. end
  43. def destroy
  44. @status = Status.where(account_id: current_user.account).find(params[:id])
  45. authorize @status, :destroy?
  46. @status.discard
  47. RemovalWorker.perform_async(@status.id, redraft: true)
  48. @status.account.statuses_count = @status.account.statuses_count - 1
  49. render json: @status, serializer: REST::StatusSerializer, source_requested: true
  50. end
  51. private
  52. def set_status
  53. @status = Status.find(params[:id])
  54. authorize @status, :show?
  55. rescue Mastodon::NotPermittedError
  56. not_found
  57. end
  58. def set_thread
  59. @thread = status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id])
  60. rescue ActiveRecord::RecordNotFound
  61. render json: { error: I18n.t('statuses.errors.in_reply_not_found') }, status: 404
  62. end
  63. def status_params
  64. params.permit(
  65. :status,
  66. :in_reply_to_id,
  67. :sensitive,
  68. :spoiler_text,
  69. :visibility,
  70. :scheduled_at,
  71. media_ids: [],
  72. poll: [
  73. :multiple,
  74. :hide_totals,
  75. :expires_in,
  76. options: [],
  77. ]
  78. )
  79. end
  80. def pagination_params(core_params)
  81. params.slice(:limit).permit(:limit).merge(core_params)
  82. end
  83. end