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.

149 lines
5.4 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 action: :show
  13. end
  14. def update_credentials
  15. current_account.update!(account_params)
  16. @account = current_account
  17. render action: :show
  18. end
  19. def following
  20. results = Follow.where(account: @account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
  21. accounts = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h
  22. @accounts = results.map { |f| accounts[f.target_account_id] }
  23. next_path = following_api_v1_account_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  24. prev_path = following_api_v1_account_url(pagination_params(since_id: results.first.id)) unless results.empty?
  25. set_pagination_headers(next_path, prev_path)
  26. render action: :index
  27. end
  28. def followers
  29. results = Follow.where(target_account: @account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
  30. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  31. @accounts = results.map { |f| accounts[f.account_id] }
  32. next_path = followers_api_v1_account_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  33. prev_path = followers_api_v1_account_url(pagination_params(since_id: results.first.id)) unless results.empty?
  34. set_pagination_headers(next_path, prev_path)
  35. render action: :index
  36. end
  37. def statuses
  38. @statuses = @account.statuses.permitted_for(@account, current_account).paginate_by_max_id(limit_param(DEFAULT_STATUSES_LIMIT), params[:max_id], params[:since_id])
  39. @statuses = @statuses.where(id: MediaAttachment.where(account: @account).where.not(status_id: nil).reorder('').select('distinct status_id')) if params[:only_media]
  40. @statuses = @statuses.without_replies if params[:exclude_replies]
  41. @statuses = cache_collection(@statuses, Status)
  42. set_maps(@statuses)
  43. next_path = statuses_api_v1_account_url(statuses_pagination_params(max_id: @statuses.last.id)) unless @statuses.empty?
  44. prev_path = statuses_api_v1_account_url(statuses_pagination_params(since_id: @statuses.first.id)) unless @statuses.empty?
  45. set_pagination_headers(next_path, prev_path)
  46. end
  47. def follow
  48. FollowService.new.call(current_user.account, @account.acct)
  49. set_relationship
  50. render action: :relationship
  51. end
  52. def block
  53. BlockService.new.call(current_user.account, @account)
  54. @following = { @account.id => false }
  55. @followed_by = { @account.id => false }
  56. @blocking = { @account.id => true }
  57. @requested = { @account.id => false }
  58. @muting = { @account.id => current_user.account.muting?(@account.id) }
  59. render action: :relationship
  60. end
  61. def mute
  62. MuteService.new.call(current_user.account, @account)
  63. set_relationship
  64. render action: :relationship
  65. end
  66. def unfollow
  67. UnfollowService.new.call(current_user.account, @account)
  68. set_relationship
  69. render action: :relationship
  70. end
  71. def unblock
  72. UnblockService.new.call(current_user.account, @account)
  73. set_relationship
  74. render action: :relationship
  75. end
  76. def unmute
  77. UnmuteService.new.call(current_user.account, @account)
  78. set_relationship
  79. render action: :relationship
  80. end
  81. def relationships
  82. ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
  83. @accounts = Account.where(id: ids).select('id')
  84. @following = Account.following_map(ids, current_user.account_id)
  85. @followed_by = Account.followed_by_map(ids, current_user.account_id)
  86. @blocking = Account.blocking_map(ids, current_user.account_id)
  87. @muting = Account.muting_map(ids, current_user.account_id)
  88. @requested = Account.requested_map(ids, current_user.account_id)
  89. end
  90. def search
  91. @accounts = AccountSearchService.new.call(params[:q], limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:resolve] == 'true', current_account)
  92. render action: :index
  93. end
  94. private
  95. def set_account
  96. @account = Account.find(params[:id])
  97. end
  98. def set_relationship
  99. @following = Account.following_map([@account.id], current_user.account_id)
  100. @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
  101. @blocking = Account.blocking_map([@account.id], current_user.account_id)
  102. @muting = Account.muting_map([@account.id], current_user.account_id)
  103. @requested = Account.requested_map([@account.id], current_user.account_id)
  104. end
  105. def pagination_params(core_params)
  106. params.permit(:limit).merge(core_params)
  107. end
  108. def statuses_pagination_params(core_params)
  109. params.permit(:limit, :only_media, :exclude_replies).merge(core_params)
  110. end
  111. def account_params
  112. params.permit(:display_name, :note, :avatar, :header)
  113. end
  114. end