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

127 lines
4.7 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]
  4. before_action -> { doorkeeper_authorize! :write }, only: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite]
  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]
  7. respond_to :json
  8. def show
  9. cached = Rails.cache.read(@status.cache_key)
  10. @status = cached unless cached.nil?
  11. end
  12. def context
  13. ancestors_results = @status.in_reply_to_id.nil? ? [] : @status.ancestors(current_account)
  14. descendants_results = @status.descendants(current_account)
  15. loaded_ancestors = cache_collection(ancestors_results, Status)
  16. loaded_descendants = cache_collection(descendants_results, Status)
  17. @context = OpenStruct.new(ancestors: loaded_ancestors, descendants: loaded_descendants)
  18. statuses = [@status] + @context[:ancestors] + @context[:descendants]
  19. set_maps(statuses)
  20. end
  21. def card
  22. @card = PreviewCard.find_by(status: @status)
  23. render_empty if @card.nil?
  24. end
  25. def reblogged_by
  26. results = @status.reblogs.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
  27. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  28. @accounts = results.map { |r| accounts[r.account_id] }
  29. next_path = reblogged_by_api_v1_status_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  30. prev_path = reblogged_by_api_v1_status_url(pagination_params(since_id: results.first.id)) unless results.empty?
  31. set_pagination_headers(next_path, prev_path)
  32. render :accounts
  33. end
  34. def favourited_by
  35. results = @status.favourites.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
  36. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  37. @accounts = results.map { |f| accounts[f.account_id] }
  38. next_path = favourited_by_api_v1_status_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  39. prev_path = favourited_by_api_v1_status_url(pagination_params(since_id: results.first.id)) unless results.empty?
  40. set_pagination_headers(next_path, prev_path)
  41. render :accounts
  42. end
  43. def create
  44. @status = PostStatusService.new.call(current_user.account,
  45. status_params[:status],
  46. status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id]),
  47. media_ids: status_params[:media_ids],
  48. sensitive: status_params[:sensitive],
  49. spoiler_text: status_params[:spoiler_text],
  50. visibility: status_params[:visibility],
  51. application: doorkeeper_token.application,
  52. idempotency: request.headers['Idempotency-Key'])
  53. render :show
  54. end
  55. def destroy
  56. @status = Status.where(account_id: current_user.account).find(params[:id])
  57. RemovalWorker.perform_async(@status.id)
  58. render_empty
  59. end
  60. def reblog
  61. @status = ReblogService.new.call(current_user.account, Status.find(params[:id]))
  62. render :show
  63. end
  64. def unreblog
  65. reblog = Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!
  66. @status = reblog.reblog
  67. @reblogs_map = { @status.id => false }
  68. RemovalWorker.perform_async(reblog.id)
  69. render :show
  70. end
  71. def favourite
  72. @status = FavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  73. render :show
  74. end
  75. def unfavourite
  76. @status = Status.find(params[:id])
  77. @favourites_map = { @status.id => false }
  78. UnfavouriteWorker.perform_async(current_user.account_id, @status.id)
  79. render :show
  80. end
  81. private
  82. def set_status
  83. @status = Status.find(params[:id])
  84. raise ActiveRecord::RecordNotFound unless @status.permitted?(current_account)
  85. end
  86. def status_params
  87. params.permit(:status, :in_reply_to_id, :sensitive, :spoiler_text, :visibility, media_ids: [])
  88. end
  89. def pagination_params(core_params)
  90. params.permit(:limit).merge(core_params)
  91. end
  92. def authorize_if_got_token
  93. request_token = Doorkeeper::OAuth::Token.from_request(request, *Doorkeeper.configuration.access_token_methods)
  94. doorkeeper_authorize! :read if request_token
  95. end
  96. end