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

118 lines
4.8 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. # set_counters_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. # set_account_counters_maps(@accounts)
  31. next_path = reblogged_by_api_v1_status_url(max_id: results.last.id) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  32. prev_path = reblogged_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
  33. set_pagination_headers(next_path, prev_path)
  34. render action: :accounts
  35. end
  36. def favourited_by
  37. results = @status.favourites.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
  38. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  39. @accounts = results.map { |f| accounts[f.account_id] }
  40. # set_account_counters_maps(@accounts)
  41. next_path = favourited_by_api_v1_status_url(max_id: results.last.id) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  42. prev_path = favourited_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
  43. set_pagination_headers(next_path, prev_path)
  44. render action: :accounts
  45. end
  46. def create
  47. @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],
  48. sensitive: status_params[:sensitive],
  49. spoiler_text: status_params[:spoiler_text],
  50. visibility: status_params[:visibility],
  51. application: doorkeeper_token.application)
  52. render action: :show
  53. end
  54. def destroy
  55. @status = Status.where(account_id: current_user.account).find(params[:id])
  56. RemovalWorker.perform_async(@status.id)
  57. render_empty
  58. end
  59. def reblog
  60. @status = ReblogService.new.call(current_user.account, Status.find(params[:id]))
  61. render action: :show
  62. end
  63. def unreblog
  64. reblog = Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!
  65. @status = reblog.reblog
  66. @reblogged_map = { @status.id => false }
  67. RemovalWorker.perform_async(reblog.id)
  68. render action: :show
  69. end
  70. def favourite
  71. @status = FavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  72. render action: :show
  73. end
  74. def unfavourite
  75. @status = Status.find(params[:id])
  76. @favourited_map = { @status.id => false }
  77. UnfavouriteWorker.perform_async(current_user.account_id, @status.id)
  78. render action: :show
  79. end
  80. private
  81. def set_status
  82. @status = Status.find(params[:id])
  83. raise ActiveRecord::RecordNotFound unless @status.permitted?(current_account)
  84. end
  85. def status_params
  86. params.permit(:status, :in_reply_to_id, :sensitive, :spoiler_text, :visibility, media_ids: [])
  87. end
  88. end