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.

86 lines
3.0 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::StatusesController < Api::BaseController
  3. include Authorization
  4. before_action :authorize_if_got_token, except: [:create, :destroy]
  5. before_action -> { doorkeeper_authorize! :write }, 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. def show
  10. cached = Rails.cache.read(@status.cache_key)
  11. @status = cached unless cached.nil?
  12. render json: @status, serializer: REST::StatusSerializer
  13. end
  14. def context
  15. ancestors_results = @status.in_reply_to_id.nil? ? [] : @status.ancestors(current_account)
  16. descendants_results = @status.descendants(current_account)
  17. loaded_ancestors = cache_collection(ancestors_results, Status)
  18. loaded_descendants = cache_collection(descendants_results, Status)
  19. @context = Context.new(ancestors: loaded_ancestors, descendants: loaded_descendants)
  20. statuses = [@status] + @context.ancestors + @context.descendants
  21. render json: @context, serializer: REST::ContextSerializer, relationships: StatusRelationshipsPresenter.new(statuses, current_user&.account_id)
  22. end
  23. def card
  24. @card = PreviewCard.find_by(status: @status)
  25. if @card.nil?
  26. render_empty
  27. else
  28. render json: @card, serializer: REST::PreviewCardSerializer
  29. end
  30. end
  31. def create
  32. @status = PostStatusService.new.call(current_user.account,
  33. status_params[:status],
  34. status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id]),
  35. media_ids: status_params[:media_ids],
  36. sensitive: status_params[:sensitive],
  37. spoiler_text: status_params[:spoiler_text],
  38. visibility: status_params[:visibility],
  39. application: doorkeeper_token.application,
  40. idempotency: request.headers['Idempotency-Key'])
  41. render json: @status, serializer: REST::StatusSerializer
  42. end
  43. def destroy
  44. @status = Status.where(account_id: current_user.account).find(params[:id])
  45. authorize @status, :destroy?
  46. RemovalWorker.perform_async(@status.id)
  47. render_empty
  48. end
  49. private
  50. def set_status
  51. @status = Status.find(params[:id])
  52. authorize @status, :show?
  53. rescue Mastodon::NotPermittedError
  54. # Reraise in order to get a 404 instead of a 403 error code
  55. raise ActiveRecord::RecordNotFound
  56. end
  57. def status_params
  58. params.permit(:status, :in_reply_to_id, :sensitive, :spoiler_text, :visibility, media_ids: [])
  59. end
  60. def pagination_params(core_params)
  61. params.permit(:limit).merge(core_params)
  62. end
  63. def authorize_if_got_token
  64. request_token = Doorkeeper::OAuth::Token.from_request(request, *Doorkeeper.configuration.access_token_methods)
  65. doorkeeper_authorize! :read if request_token
  66. end
  67. end