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

117 lines
4.9 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::StatusesController < ApiController
  3. before_action -> { doorkeeper_authorize! :read }, 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 action: :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 action: :accounts
  42. end
  43. def create
  44. @status = PostStatusService.new.call(current_user.account, status_params[:status], status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id]), media_ids: status_params[:media_ids],
  45. sensitive: status_params[:sensitive],
  46. spoiler_text: status_params[:spoiler_text],
  47. visibility: status_params[:visibility],
  48. application: doorkeeper_token.application)
  49. render action: :show
  50. end
  51. def destroy
  52. @status = Status.where(account_id: current_user.account).find(params[:id])
  53. RemovalWorker.perform_async(@status.id)
  54. render_empty
  55. end
  56. def reblog
  57. @status = ReblogService.new.call(current_user.account, Status.find(params[:id]))
  58. render action: :show
  59. end
  60. def unreblog
  61. reblog = Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!
  62. @status = reblog.reblog
  63. @reblogged_map = { @status.id => false }
  64. RemovalWorker.perform_async(reblog.id)
  65. render action: :show
  66. end
  67. def favourite
  68. @status = FavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  69. render action: :show
  70. end
  71. def unfavourite
  72. @status = Status.find(params[:id])
  73. @favourited_map = { @status.id => false }
  74. UnfavouriteWorker.perform_async(current_user.account_id, @status.id)
  75. render action: :show
  76. end
  77. private
  78. def set_status
  79. @status = Status.find(params[:id])
  80. raise ActiveRecord::RecordNotFound unless @status.permitted?(current_account)
  81. end
  82. def status_params
  83. params.permit(:status, :in_reply_to_id, :sensitive, :spoiler_text, :visibility, media_ids: [])
  84. end
  85. def pagination_params(core_params)
  86. params.permit(:limit).merge(core_params)
  87. end
  88. end