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.

80 lines
2.7 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. end
  13. def context
  14. ancestors_results = @status.in_reply_to_id.nil? ? [] : @status.ancestors(current_account)
  15. descendants_results = @status.descendants(current_account)
  16. loaded_ancestors = cache_collection(ancestors_results, Status)
  17. loaded_descendants = cache_collection(descendants_results, Status)
  18. @context = OpenStruct.new(ancestors: loaded_ancestors, descendants: loaded_descendants)
  19. statuses = [@status] + @context[:ancestors] + @context[:descendants]
  20. set_maps(statuses)
  21. end
  22. def card
  23. @card = PreviewCard.find_by(status: @status)
  24. render_empty if @card.nil?
  25. end
  26. def create
  27. @status = PostStatusService.new.call(current_user.account,
  28. status_params[:status],
  29. 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. application: doorkeeper_token.application,
  35. idempotency: request.headers['Idempotency-Key'])
  36. render :show
  37. end
  38. def destroy
  39. @status = Status.where(account_id: current_user.account).find(params[:id])
  40. authorize @status, :destroy?
  41. RemovalWorker.perform_async(@status.id)
  42. render_empty
  43. end
  44. private
  45. def set_status
  46. @status = Status.find(params[:id])
  47. authorize @status, :show?
  48. rescue Mastodon::NotPermittedError
  49. # Reraise in order to get a 404 instead of a 403 error code
  50. raise ActiveRecord::RecordNotFound
  51. end
  52. def status_params
  53. params.permit(:status, :in_reply_to_id, :sensitive, :spoiler_text, :visibility, media_ids: [])
  54. end
  55. def pagination_params(core_params)
  56. params.permit(:limit).merge(core_params)
  57. end
  58. def authorize_if_got_token
  59. request_token = Doorkeeper::OAuth::Token.from_request(request, *Doorkeeper.configuration.access_token_methods)
  60. doorkeeper_authorize! :read if request_token
  61. end
  62. end