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.

138 lines
5.0 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 = @statuses.where(id: MediaAttachment.where(account: @account).where.not(status_id: nil).reorder('').select('distinct status_id')) if params[:only_media]
  36. @statuses = @statuses.without_replies if params[:exclude_replies]
  37. @statuses = cache_collection(@statuses, Status)
  38. set_maps(@statuses)
  39. set_counters_maps(@statuses)
  40. set_account_counters_maps(@statuses.flat_map { |s| [s.account, s.reblog? ? s.reblog.account : nil] }.compact.uniq)
  41. next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) unless @statuses.empty?
  42. prev_path = statuses_api_v1_account_url(since_id: @statuses.first.id) unless @statuses.empty?
  43. set_pagination_headers(next_path, prev_path)
  44. end
  45. def follow
  46. FollowService.new.call(current_user.account, @account.acct)
  47. set_relationship
  48. render action: :relationship
  49. end
  50. def block
  51. BlockService.new.call(current_user.account, @account)
  52. @following = { @account.id => false }
  53. @followed_by = { @account.id => false }
  54. @blocking = { @account.id => true }
  55. @requested = { @account.id => false }
  56. @muting = { @account.id => current_user.account.muting?(@account.id) }
  57. render action: :relationship
  58. end
  59. def mute
  60. MuteService.new.call(current_user.account, @account)
  61. set_relationship
  62. render action: :relationship
  63. end
  64. def unfollow
  65. UnfollowService.new.call(current_user.account, @account)
  66. set_relationship
  67. render action: :relationship
  68. end
  69. def unblock
  70. UnblockService.new.call(current_user.account, @account)
  71. set_relationship
  72. render action: :relationship
  73. end
  74. def unmute
  75. UnmuteService.new.call(current_user.account, @account)
  76. set_relationship
  77. render action: :relationship
  78. end
  79. def relationships
  80. ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
  81. @accounts = Account.where(id: ids).select('id')
  82. @following = Account.following_map(ids, current_user.account_id)
  83. @followed_by = Account.followed_by_map(ids, current_user.account_id)
  84. @blocking = Account.blocking_map(ids, current_user.account_id)
  85. @muting = Account.muting_map(ids, current_user.account_id)
  86. @requested = Account.requested_map(ids, current_user.account_id)
  87. end
  88. def search
  89. @accounts = AccountSearchService.new.call(params[:q], limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:resolve] == 'true', current_account)
  90. set_account_counters_maps(@accounts) unless @accounts.nil?
  91. render action: :index
  92. end
  93. private
  94. def set_account
  95. @account = Account.find(params[:id])
  96. end
  97. def set_relationship
  98. @following = Account.following_map([@account.id], current_user.account_id)
  99. @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
  100. @blocking = Account.blocking_map([@account.id], current_user.account_id)
  101. @muting = Account.muting_map([@account.id], current_user.account_id)
  102. @requested = Account.requested_map([@account.id], current_user.account_id)
  103. end
  104. end