闭社主体 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.

153 lines
5.4 KiB

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