The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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