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.

94 lines
3.5 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. 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. thread = status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id])
  29. sender = thread && thread.account.username == 'tree_hole_bot' ? thread.account : current_account
  30. @status = PostStatusService.new.call(sender,
  31. text: status_params[:status],
  32. thread: thread,
  33. media_ids: status_params[:media_ids],
  34. sensitive: status_params[:sensitive],
  35. spoiler_text: status_params[:spoiler_text],
  36. visibility: status_params[:visibility],
  37. scheduled_at: status_params[:scheduled_at],
  38. application: doorkeeper_token.application,
  39. poll: status_params[:poll],
  40. idempotency: request.headers['Idempotency-Key'])
  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. raise ActiveRecord::RecordNotFound
  56. end
  57. def status_params
  58. params.permit(
  59. :status,
  60. :in_reply_to_id,
  61. :sensitive,
  62. :spoiler_text,
  63. :visibility,
  64. :scheduled_at,
  65. media_ids: [],
  66. poll: [
  67. :multiple,
  68. :hide_totals,
  69. :expires_in,
  70. options: [],
  71. ]
  72. )
  73. end
  74. def pagination_params(core_params)
  75. params.slice(:limit).permit(:limit).merge(core_params)
  76. end
  77. end