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.

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