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.

105 lines
3.7 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], sensitive: params[:sensitive], visibility: params[:visibility], application: doorkeeper_token.application)
  44. render action: :show
  45. end
  46. def destroy
  47. @status = Status.where(account_id: current_user.account).find(params[:id])
  48. RemovalWorker.perform_async(@status.id)
  49. render_empty
  50. end
  51. def reblog
  52. @status = ReblogService.new.call(current_user.account, Status.find(params[:id]))
  53. render action: :show
  54. end
  55. def unreblog
  56. reblog = Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!
  57. @status = reblog.reblog
  58. @reblogged_map = { @status.id => false }
  59. RemovalWorker.perform_async(reblog.id)
  60. render action: :show
  61. end
  62. def favourite
  63. @status = FavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  64. render action: :show
  65. end
  66. def unfavourite
  67. @status = Status.find(params[:id])
  68. @favourited_map = { @status.id => false }
  69. UnfavouriteWorker.perform_async(current_user.account_id, @status.id)
  70. render action: :show
  71. end
  72. private
  73. def set_status
  74. @status = Status.find(params[:id])
  75. raise ActiveRecord::RecordNotFound unless @status.permitted?(current_account)
  76. end
  77. end