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.

110 lines
4.4 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. @context = OpenStruct.new(ancestors: @status.in_reply_to_id.nil? ? [] : @status.ancestors(current_account), descendants: @status.descendants(current_account))
  14. statuses = [@status] + @context[:ancestors] + @context[:descendants]
  15. set_maps(statuses)
  16. set_counters_maps(statuses)
  17. end
  18. def card
  19. @card = PreviewCard.find_by(status: @status)
  20. render_empty if @card.nil?
  21. end
  22. def reblogged_by
  23. results = @status.reblogs.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
  24. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  25. @accounts = results.map { |r| accounts[r.account_id] }
  26. set_account_counters_maps(@accounts)
  27. next_path = reblogged_by_api_v1_status_url(max_id: results.last.id) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  28. prev_path = reblogged_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
  29. set_pagination_headers(next_path, prev_path)
  30. render action: :accounts
  31. end
  32. def favourited_by
  33. results = @status.favourites.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
  34. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  35. @accounts = results.map { |f| accounts[f.account_id] }
  36. set_account_counters_maps(@accounts)
  37. next_path = favourited_by_api_v1_status_url(max_id: results.last.id) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  38. prev_path = favourited_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
  39. set_pagination_headers(next_path, prev_path)
  40. render action: :accounts
  41. end
  42. def create
  43. @status = PostStatusService.new.call(current_user.account, params[:status], params[:in_reply_to_id].blank? ? nil : Status.find(params[:in_reply_to_id]), media_ids: params[:media_ids],
  44. sensitive: params[:sensitive],
  45. spoiler_text: params[:spoiler_text],
  46. visibility: params[:visibility],
  47. application: doorkeeper_token.application)
  48. render action: :show
  49. end
  50. def destroy
  51. @status = Status.where(account_id: current_user.account).find(params[:id])
  52. RemovalWorker.perform_async(@status.id)
  53. render_empty
  54. end
  55. def reblog
  56. @status = ReblogService.new.call(current_user.account, Status.find(params[:id]))
  57. render action: :show
  58. end
  59. def unreblog
  60. reblog = Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!
  61. @status = reblog.reblog
  62. @reblogged_map = { @status.id => false }
  63. RemovalWorker.perform_async(reblog.id)
  64. render action: :show
  65. end
  66. def favourite
  67. @status = FavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  68. render action: :show
  69. end
  70. def unfavourite
  71. @status = Status.find(params[:id])
  72. @favourited_map = { @status.id => false }
  73. UnfavouriteWorker.perform_async(current_user.account_id, @status.id)
  74. render action: :show
  75. end
  76. private
  77. def set_status
  78. @status = Status.find(params[:id])
  79. raise ActiveRecord::RecordNotFound unless @status.permitted?(current_account)
  80. end
  81. end