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.

158 lines
5.9 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::AccountsController < ApiController
  3. before_action -> { doorkeeper_authorize! :read }, except: [:follow, :unfollow, :block, :unblock, :mute, :unmute, :update_credentials]
  4. before_action -> { doorkeeper_authorize! :follow }, only: [:follow, :unfollow, :block, :unblock, :mute, :unmute]
  5. before_action -> { doorkeeper_authorize! :write }, only: [:update_credentials]
  6. before_action :require_user!, except: [:show, :following, :followers, :statuses]
  7. before_action :set_account, except: [:verify_credentials, :update_credentials, :suggestions, :search]
  8. respond_to :json
  9. def show; end
  10. def verify_credentials
  11. @account = current_user.account
  12. render :show
  13. end
  14. def update_credentials
  15. current_account.update!(account_params)
  16. @account = current_account
  17. render :show
  18. end
  19. def following
  20. @accounts = Account.includes(:followers)
  21. .references(:followers)
  22. .merge(Follow.where(account: @account)
  23. .paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]))
  24. .to_a
  25. next_path = following_api_v1_account_url(pagination_params(max_id: @accounts.last.followers.last.id)) if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  26. prev_path = following_api_v1_account_url(pagination_params(since_id: @accounts.first.followers.first.id)) unless @accounts.empty?
  27. set_pagination_headers(next_path, prev_path)
  28. render :index
  29. end
  30. def followers
  31. @accounts = Account.includes(:following)
  32. .references(:following)
  33. .merge(Follow.where(target_account: @account)
  34. .paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT),
  35. params[:max_id],
  36. params[:since_id]))
  37. .to_a
  38. next_path = followers_api_v1_account_url(pagination_params(max_id: @accounts.last.following.last.id)) if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  39. prev_path = followers_api_v1_account_url(pagination_params(since_id: @accounts.first.following.first.id)) unless @accounts.empty?
  40. set_pagination_headers(next_path, prev_path)
  41. render :index
  42. end
  43. def statuses
  44. @statuses = @account.statuses.permitted_for(@account, current_account).paginate_by_max_id(limit_param(DEFAULT_STATUSES_LIMIT), params[:max_id], params[:since_id])
  45. @statuses = @statuses.where(id: MediaAttachment.where(account: @account).where.not(status_id: nil).reorder('').select('distinct status_id')) if params[:only_media]
  46. @statuses = @statuses.without_replies if params[:exclude_replies]
  47. @statuses = cache_collection(@statuses, Status)
  48. set_maps(@statuses)
  49. next_path = statuses_api_v1_account_url(statuses_pagination_params(max_id: @statuses.last.id)) unless @statuses.empty?
  50. prev_path = statuses_api_v1_account_url(statuses_pagination_params(since_id: @statuses.first.id)) unless @statuses.empty?
  51. set_pagination_headers(next_path, prev_path)
  52. end
  53. def follow
  54. FollowService.new.call(current_user.account, @account.acct)
  55. set_relationship
  56. render :relationship
  57. end
  58. def block
  59. BlockService.new.call(current_user.account, @account)
  60. @following = { @account.id => false }
  61. @followed_by = { @account.id => false }
  62. @blocking = { @account.id => true }
  63. @requested = { @account.id => false }
  64. @muting = { @account.id => current_account.muting?(@account.id) }
  65. @domain_blocking = { @account.id => current_account.domain_blocking?(@account.domain) }
  66. render :relationship
  67. end
  68. def mute
  69. MuteService.new.call(current_user.account, @account)
  70. set_relationship
  71. render :relationship
  72. end
  73. def unfollow
  74. UnfollowService.new.call(current_user.account, @account)
  75. set_relationship
  76. render :relationship
  77. end
  78. def unblock
  79. UnblockService.new.call(current_user.account, @account)
  80. set_relationship
  81. render :relationship
  82. end
  83. def unmute
  84. UnmuteService.new.call(current_user.account, @account)
  85. set_relationship
  86. render :relationship
  87. end
  88. def relationships
  89. ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
  90. @accounts = Account.where(id: ids).select('id')
  91. @following = Account.following_map(ids, current_user.account_id)
  92. @followed_by = Account.followed_by_map(ids, current_user.account_id)
  93. @blocking = Account.blocking_map(ids, current_user.account_id)
  94. @muting = Account.muting_map(ids, current_user.account_id)
  95. @requested = Account.requested_map(ids, current_user.account_id)
  96. @domain_blocking = Account.domain_blocking_map(ids, current_user.account_id)
  97. end
  98. def search
  99. @accounts = AccountSearchService.new.call(params[:q], limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:resolve] == 'true', current_account)
  100. render :index
  101. end
  102. private
  103. def set_account
  104. @account = Account.find(params[:id])
  105. end
  106. def set_relationship
  107. @following = Account.following_map([@account.id], current_user.account_id)
  108. @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
  109. @blocking = Account.blocking_map([@account.id], current_user.account_id)
  110. @muting = Account.muting_map([@account.id], current_user.account_id)
  111. @requested = Account.requested_map([@account.id], current_user.account_id)
  112. @domain_blocking = Account.domain_blocking_map([@account.id], current_user.account_id)
  113. end
  114. def pagination_params(core_params)
  115. params.permit(:limit).merge(core_params)
  116. end
  117. def statuses_pagination_params(core_params)
  118. params.permit(:limit, :only_media, :exclude_replies).merge(core_params)
  119. end
  120. def account_params
  121. params.permit(:display_name, :note, :avatar, :header)
  122. end
  123. end