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.

100 lines
3.7 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. render json: @status, serializer: REST::StatusSerializer, source_requested: true
  49. end
  50. private
  51. def set_status
  52. @status = Status.find(params[:id])
  53. authorize @status, :show?
  54. rescue Mastodon::NotPermittedError
  55. not_found
  56. end
  57. def set_thread
  58. @thread = status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id])
  59. rescue ActiveRecord::RecordNotFound
  60. render json: { error: I18n.t('statuses.errors.in_reply_not_found') }, status: 404
  61. end
  62. def status_params
  63. params.permit(
  64. :status,
  65. :in_reply_to_id,
  66. :sensitive,
  67. :spoiler_text,
  68. :visibility,
  69. :scheduled_at,
  70. media_ids: [],
  71. poll: [
  72. :multiple,
  73. :hide_totals,
  74. :expires_in,
  75. options: [],
  76. ]
  77. )
  78. end
  79. def pagination_params(core_params)
  80. params.slice(:limit).permit(:limit).merge(core_params)
  81. end
  82. end