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.

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