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.

150 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]
  4. before_action -> { doorkeeper_authorize! :follow }, only: [:follow, :unfollow, :block, :unblock, :mute, :unmute]
  5. before_action :require_user!, except: [:show, :following, :followers, :statuses]
  6. before_action :set_account, except: [:verify_credentials, :suggestions, :search]
  7. respond_to :json
  8. def show; end
  9. def verify_credentials
  10. @account = current_user.account
  11. render action: :show
  12. end
  13. def following
  14. results = Follow.where(account: @account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
  15. accounts = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h
  16. @accounts = results.map { |f| accounts[f.target_account_id] }
  17. set_account_counters_maps(@accounts)
  18. next_path = following_api_v1_account_url(max_id: results.last.id) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  19. prev_path = following_api_v1_account_url(since_id: results.first.id) unless results.empty?
  20. set_pagination_headers(next_path, prev_path)
  21. render action: :index
  22. end
  23. def followers
  24. results = Follow.where(target_account: @account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
  25. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  26. @accounts = results.map { |f| accounts[f.account_id] }
  27. set_account_counters_maps(@accounts)
  28. next_path = followers_api_v1_account_url(max_id: results.last.id) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  29. prev_path = followers_api_v1_account_url(since_id: results.first.id) unless results.empty?
  30. set_pagination_headers(next_path, prev_path)
  31. render action: :index
  32. end
  33. def statuses
  34. @statuses = @account.statuses.permitted_for(@account, current_account).paginate_by_max_id(limit_param(DEFAULT_STATUSES_LIMIT), params[:max_id], params[:since_id])
  35. @statuses = cache_collection(@statuses, Status)
  36. set_maps(@statuses)
  37. set_counters_maps(@statuses)
  38. next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) unless @statuses.empty?
  39. prev_path = statuses_api_v1_account_url(since_id: @statuses.first.id) unless @statuses.empty?
  40. set_pagination_headers(next_path, prev_path)
  41. end
  42. def media_statuses
  43. media_ids = MediaAttachment.where(account: @account).where.not(status_id: nil).reorder('').select('distinct status_id')
  44. @statuses = @account.statuses.where(id: media_ids).permitted_for(@account, current_account).paginate_by_max_id(limit_param(DEFAULT_STATUSES_LIMIT), params[:max_id], params[:since_id])
  45. @statuses = cache_collection(@statuses, Status)
  46. set_maps(@statuses)
  47. set_counters_maps(@statuses)
  48. next_path = media_statuses_api_v1_account_url(max_id: @statuses.last.id) unless @statuses.empty?
  49. prev_path = media_statuses_api_v1_account_url(since_id: @statuses.first.id) unless @statuses.empty?
  50. set_pagination_headers(next_path, prev_path)
  51. render action: :statuses
  52. end
  53. def follow
  54. FollowService.new.call(current_user.account, @account.acct)
  55. set_relationship
  56. render action: :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_user.account.muting?(@account.id) }
  65. render action: :relationship
  66. end
  67. def mute
  68. MuteService.new.call(current_user.account, @account)
  69. set_relationship
  70. render action: :relationship
  71. end
  72. def unfollow
  73. UnfollowService.new.call(current_user.account, @account)
  74. set_relationship
  75. render action: :relationship
  76. end
  77. def unblock
  78. UnblockService.new.call(current_user.account, @account)
  79. set_relationship
  80. render action: :relationship
  81. end
  82. def unmute
  83. UnmuteService.new.call(current_user.account, @account)
  84. set_relationship
  85. render action: :relationship
  86. end
  87. def relationships
  88. ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
  89. @accounts = Account.where(id: ids).select('id')
  90. @following = Account.following_map(ids, current_user.account_id)
  91. @followed_by = Account.followed_by_map(ids, current_user.account_id)
  92. @blocking = Account.blocking_map(ids, current_user.account_id)
  93. @muting = Account.muting_map(ids, current_user.account_id)
  94. @requested = Account.requested_map(ids, current_user.account_id)
  95. end
  96. def search
  97. @accounts = SearchService.new.call(params[:q], limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:resolve] == 'true')
  98. set_account_counters_maps(@accounts) unless @accounts.nil?
  99. render action: :index
  100. end
  101. private
  102. def set_account
  103. @account = Account.find(params[:id])
  104. end
  105. def set_relationship
  106. @following = Account.following_map([@account.id], current_user.account_id)
  107. @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
  108. @blocking = Account.blocking_map([@account.id], current_user.account_id)
  109. @muting = Account.muting_map([@account.id], current_user.account_id)
  110. @requested = Account.requested_map([@account.id], current_user.account_id)
  111. end
  112. end