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.

120 lines
4.2 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::AccountsController < ApiController
  3. before_action -> { doorkeeper_authorize! :read }, except: [:follow, :unfollow, :block, :unblock]
  4. before_action -> { doorkeeper_authorize! :follow }, only: [:follow, :unfollow, :block, :unblock]
  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) if @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  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 follow
  43. FollowService.new.call(current_user.account, @account.acct)
  44. set_relationship
  45. render action: :relationship
  46. end
  47. def block
  48. BlockWorker.perform_async(current_user.account_id, @account.id)
  49. @following = { @account.id => false }
  50. @followed_by = { @account.id => false }
  51. @blocking = { @account.id => true }
  52. @requested = { @account.id => false }
  53. render action: :relationship
  54. end
  55. def unfollow
  56. UnfollowService.new.call(current_user.account, @account)
  57. set_relationship
  58. render action: :relationship
  59. end
  60. def unblock
  61. UnblockService.new.call(current_user.account, @account)
  62. set_relationship
  63. render action: :relationship
  64. end
  65. def relationships
  66. ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
  67. @accounts = Account.where(id: ids).select('id')
  68. @following = Account.following_map(ids, current_user.account_id)
  69. @followed_by = Account.followed_by_map(ids, current_user.account_id)
  70. @blocking = Account.blocking_map(ids, current_user.account_id)
  71. @requested = Account.requested_map(ids, current_user.account_id)
  72. end
  73. def search
  74. @accounts = SearchService.new.call(params[:q], limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:resolve] == 'true')
  75. set_account_counters_maps(@accounts) unless @accounts.nil?
  76. render action: :index
  77. end
  78. private
  79. def set_account
  80. @account = Account.find(params[:id])
  81. end
  82. def set_relationship
  83. @following = Account.following_map([@account.id], current_user.account_id)
  84. @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
  85. @blocking = Account.blocking_map([@account.id], current_user.account_id)
  86. @requested = Account.requested_map([@account.id], current_user.account_id)
  87. end
  88. end