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.

87 lines
3.2 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. cached = Rails.cache.read(@status.cache_key)
  16. @status = cached unless cached.nil?
  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 card
  29. @card = @status.preview_cards.first
  30. if @card.nil?
  31. render_empty
  32. else
  33. render json: @card, serializer: REST::PreviewCardSerializer
  34. end
  35. end
  36. def create
  37. @status = PostStatusService.new.call(current_user.account,
  38. status_params[:status],
  39. status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id]),
  40. media_ids: status_params[:media_ids],
  41. sensitive: status_params[:sensitive],
  42. spoiler_text: status_params[:spoiler_text],
  43. visibility: status_params[:visibility],
  44. application: doorkeeper_token.application,
  45. idempotency: request.headers['Idempotency-Key'])
  46. render json: @status, serializer: REST::StatusSerializer
  47. end
  48. def destroy
  49. @status = Status.where(account_id: current_user.account).find(params[:id])
  50. authorize @status, :destroy?
  51. RemovalWorker.perform_async(@status.id)
  52. render_empty
  53. end
  54. private
  55. def set_status
  56. @status = Status.find(params[:id])
  57. authorize @status, :show?
  58. rescue Mastodon::NotPermittedError
  59. # Reraise in order to get a 404 instead of a 403 error code
  60. raise ActiveRecord::RecordNotFound
  61. end
  62. def status_params
  63. params.permit(:status, :in_reply_to_id, :sensitive, :spoiler_text, :visibility, media_ids: [])
  64. end
  65. def pagination_params(core_params)
  66. params.slice(:limit).permit(:limit).merge(core_params)
  67. end
  68. end