statuses_controller.rb 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 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 create
  28. @status = PostStatusService.new.call(current_user.account,
  29. text: status_params[:status],
  30. thread: status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id]),
  31. media_ids: status_params[:media_ids],
  32. sensitive: status_params[:sensitive],
  33. spoiler_text: status_params[:spoiler_text],
  34. visibility: status_params[:visibility],
  35. scheduled_at: status_params[:scheduled_at],
  36. application: doorkeeper_token.application,
  37. poll: status_params[:poll],
  38. idempotency: request.headers['Idempotency-Key'])
  39. render json: @status, serializer: @status.is_a?(ScheduledStatus) ? REST::ScheduledStatusSerializer : REST::StatusSerializer
  40. end
  41. def destroy
  42. @status = Status.where(account_id: current_user.account).find(params[:id])
  43. authorize @status, :destroy?
  44. @status.discard
  45. RemovalWorker.perform_async(@status.id, redraft: true)
  46. render json: @status, serializer: REST::StatusSerializer, source_requested: true
  47. end
  48. private
  49. def set_status
  50. @status = Status.find(params[:id])
  51. authorize @status, :show?
  52. rescue Mastodon::NotPermittedError
  53. raise ActiveRecord::RecordNotFound
  54. end
  55. def status_params
  56. params.permit(
  57. :status,
  58. :in_reply_to_id,
  59. :sensitive,
  60. :spoiler_text,
  61. :visibility,
  62. :scheduled_at,
  63. media_ids: [],
  64. poll: [
  65. :multiple,
  66. :hide_totals,
  67. :expires_in,
  68. options: [],
  69. ]
  70. )
  71. end
  72. def pagination_params(core_params)
  73. params.slice(:limit).permit(:limit).merge(core_params)
  74. end
  75. end