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.

113 lines
3.9 KiB

  1. class Api::V1::AccountsController < ApiController
  2. before_action -> { doorkeeper_authorize! :read }, except: [:follow, :unfollow, :block, :unblock]
  3. before_action -> { doorkeeper_authorize! :follow }, only: [:follow, :unfollow, :block, :unblock]
  4. before_action :require_user!, except: [:show, :following, :followers, :statuses]
  5. before_action :set_account, except: [:verify_credentials, :suggestions, :search]
  6. respond_to :json
  7. def show
  8. 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(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  15. accounts = Account.where(id: results.map(&:target_account_id)).with_counters.map { |a| [a.id, a] }.to_h
  16. @accounts = results.map { |f| accounts[f.target_account_id] }
  17. next_path = following_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  18. prev_path = following_api_v1_account_url(since_id: results.first.id) if results.size > 0
  19. set_pagination_headers(next_path, prev_path)
  20. render action: :index
  21. end
  22. def followers
  23. results = Follow.where(target_account: @account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  24. accounts = Account.where(id: results.map(&:account_id)).with_counters.map { |a| [a.id, a] }.to_h
  25. @accounts = results.map { |f| accounts[f.account_id] }
  26. next_path = followers_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  27. prev_path = followers_api_v1_account_url(since_id: results.first.id) if results.size > 0
  28. set_pagination_headers(next_path, prev_path)
  29. render action: :index
  30. end
  31. def common_followers
  32. @accounts = @account.common_followers_with(current_user.account)
  33. render action: :index
  34. end
  35. def suggestions
  36. @accounts = FollowSuggestion.get(current_user.account_id)
  37. render action: :index
  38. end
  39. def statuses
  40. @statuses = @account.statuses.with_includes.with_counters.paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a
  41. set_maps(@statuses)
  42. next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
  43. prev_path = statuses_api_v1_account_url(since_id: @statuses.first.id) if @statuses.size > 0
  44. set_pagination_headers(next_path, prev_path)
  45. end
  46. def follow
  47. FollowService.new.call(current_user.account, @account.acct)
  48. set_relationship
  49. render action: :relationship
  50. end
  51. def block
  52. BlockService.new.call(current_user.account, @account)
  53. set_relationship
  54. render action: :relationship
  55. end
  56. def unfollow
  57. UnfollowService.new.call(current_user.account, @account)
  58. set_relationship
  59. render action: :relationship
  60. end
  61. def unblock
  62. UnblockService.new.call(current_user.account, @account)
  63. set_relationship
  64. render action: :relationship
  65. end
  66. def relationships
  67. ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
  68. @accounts = Account.where(id: ids).select('id')
  69. @following = Account.following_map(ids, current_user.account_id)
  70. @followed_by = Account.followed_by_map(ids, current_user.account_id)
  71. @blocking = Account.blocking_map(ids, current_user.account_id)
  72. end
  73. def search
  74. limit = params[:limit] ? [DEFAULT_ACCOUNTS_LIMIT, params[:limit].to_i].min : DEFAULT_ACCOUNTS_LIMIT
  75. @accounts = SearchService.new.call(params[:q], limit, params[:resolve] == 'true')
  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. end
  87. end