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.

104 lines
3.6 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. end
  21. def reblogged_by
  22. results = @status.reblogs.paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  23. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  24. @accounts = results.map { |r| accounts[r.account_id] }
  25. set_account_counters_maps(@accounts)
  26. next_path = reblogged_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  27. prev_path = reblogged_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
  28. set_pagination_headers(next_path, prev_path)
  29. render action: :accounts
  30. end
  31. def favourited_by
  32. results = @status.favourites.paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  33. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  34. @accounts = results.map { |f| accounts[f.account_id] }
  35. set_account_counters_maps(@accounts)
  36. next_path = favourited_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  37. prev_path = favourited_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
  38. set_pagination_headers(next_path, prev_path)
  39. render action: :accounts
  40. end
  41. def create
  42. @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)
  43. render action: :show
  44. end
  45. def destroy
  46. @status = Status.where(account_id: current_user.account).find(params[:id])
  47. RemovalWorker.perform_async(@status.id)
  48. render_empty
  49. end
  50. def reblog
  51. @status = ReblogService.new.call(current_user.account, Status.find(params[:id]))
  52. render action: :show
  53. end
  54. def unreblog
  55. reblog = Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!
  56. @status = reblog.reblog
  57. @reblogged_map = { @status.id => false }
  58. RemovalWorker.perform_async(reblog.id)
  59. render action: :show
  60. end
  61. def favourite
  62. @status = FavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  63. render action: :show
  64. end
  65. def unfavourite
  66. @status = Status.find(params[:id])
  67. @favourited_map = { @status.id => false }
  68. UnfavouriteWorker.perform_async(current_user.account_id, @status.id)
  69. render action: :show
  70. end
  71. private
  72. def set_status
  73. @status = Status.find(params[:id])
  74. raise ActiveRecord::RecordNotFound unless @status.permitted?(current_account)
  75. end
  76. end