闭社主体 forked from https://github.com/tootsuite/mastodon
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.

158 lines
5.5 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::StatusesController < ApiController
  3. include Authorization
  4. before_action :authorize_if_got_token, except: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite, :mute, :unmute]
  5. before_action -> { doorkeeper_authorize! :write }, only: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite, :mute, :unmute]
  6. before_action :require_user!, except: [:show, :context, :card, :reblogged_by, :favourited_by]
  7. before_action :set_status, only: [:show, :context, :card, :reblogged_by, :favourited_by, :mute, :unmute]
  8. before_action :set_conversation, only: [:mute, :unmute]
  9. respond_to :json
  10. def show
  11. cached = Rails.cache.read(@status.cache_key)
  12. @status = cached unless cached.nil?
  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 = OpenStruct.new(ancestors: loaded_ancestors, descendants: loaded_descendants)
  20. statuses = [@status] + @context[:ancestors] + @context[:descendants]
  21. set_maps(statuses)
  22. end
  23. def card
  24. @card = PreviewCard.find_by(status: @status)
  25. render_empty if @card.nil?
  26. end
  27. def reblogged_by
  28. @accounts = Account.includes(:statuses)
  29. .references(:statuses)
  30. .merge(Status.where(reblog_of_id: @status.id)
  31. .paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]))
  32. .to_a
  33. next_path = reblogged_by_api_v1_status_url(pagination_params(max_id: @accounts.last.statuses.last.id)) if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  34. prev_path = reblogged_by_api_v1_status_url(pagination_params(since_id: @accounts.first.statuses.first.id)) unless @accounts.empty?
  35. set_pagination_headers(next_path, prev_path)
  36. render :accounts
  37. end
  38. def favourited_by
  39. @accounts = Account.includes(:favourites)
  40. .references(:favourites)
  41. .where(favourites: { status_id: @status.id })
  42. .merge(Favourite.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]))
  43. .to_a
  44. next_path = favourited_by_api_v1_status_url(pagination_params(max_id: @accounts.last.favourites.last.id)) if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  45. prev_path = favourited_by_api_v1_status_url(pagination_params(since_id: @accounts.first.favourites.first.id)) unless @accounts.empty?
  46. set_pagination_headers(next_path, prev_path)
  47. render :accounts
  48. end
  49. def create
  50. @status = PostStatusService.new.call(current_user.account,
  51. status_params[:status],
  52. status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id]),
  53. media_ids: status_params[:media_ids],
  54. sensitive: status_params[:sensitive],
  55. spoiler_text: status_params[:spoiler_text],
  56. visibility: status_params[:visibility],
  57. application: doorkeeper_token.application,
  58. idempotency: request.headers['Idempotency-Key'])
  59. render :show
  60. end
  61. def destroy
  62. @status = Status.where(account_id: current_user.account).find(params[:id])
  63. RemovalWorker.perform_async(@status.id)
  64. render_empty
  65. end
  66. def reblog
  67. @status = ReblogService.new.call(current_user.account, Status.find(params[:id]))
  68. render :show
  69. end
  70. def unreblog
  71. reblog = Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!
  72. @status = reblog.reblog
  73. @reblogs_map = { @status.id => false }
  74. RemovalWorker.perform_async(reblog.id)
  75. render :show
  76. end
  77. def favourite
  78. @status = FavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  79. render :show
  80. end
  81. def unfavourite
  82. @status = Status.find(params[:id])
  83. @favourites_map = { @status.id => false }
  84. UnfavouriteWorker.perform_async(current_user.account_id, @status.id)
  85. render :show
  86. end
  87. def mute
  88. current_account.mute_conversation!(@conversation)
  89. @mutes_map = { @conversation.id => true }
  90. render :show
  91. end
  92. def unmute
  93. current_account.unmute_conversation!(@conversation)
  94. @mutes_map = { @conversation.id => false }
  95. render :show
  96. end
  97. private
  98. def set_status
  99. @status = Status.find(params[:id])
  100. authorize @status, :show?
  101. rescue Mastodon::NotPermittedError
  102. # Reraise in order to get a 404 instead of a 403 error code
  103. raise ActiveRecord::RecordNotFound
  104. end
  105. def set_conversation
  106. @conversation = @status.conversation
  107. raise Mastodon::ValidationError if @conversation.nil?
  108. end
  109. def status_params
  110. params.permit(:status, :in_reply_to_id, :sensitive, :spoiler_text, :visibility, media_ids: [])
  111. end
  112. def pagination_params(core_params)
  113. params.permit(:limit).merge(core_params)
  114. end
  115. def authorize_if_got_token
  116. request_token = Doorkeeper::OAuth::Token.from_request(request, *Doorkeeper.configuration.access_token_methods)
  117. doorkeeper_authorize! :read if request_token
  118. end
  119. end