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.

105 lines
3.5 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]
  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.to_a
  16. next_path = following_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  17. prev_path = following_api_v1_account_url(since_id: results.first.id) if results.size > 0
  18. set_pagination_headers(next_path, prev_path)
  19. render action: :index
  20. end
  21. def followers
  22. results = Follow.where(target_account: @account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  23. @accounts = Account.where(id: results.map(&:account_id)).with_counters.to_a
  24. next_path = following_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  25. prev_path = following_api_v1_account_url(since_id: results.first.id) if results.size > 0
  26. set_pagination_headers(next_path, prev_path)
  27. render action: :index
  28. end
  29. def common_followers
  30. @accounts = @account.common_followers_with(current_user.account)
  31. render action: :index
  32. end
  33. def suggestions
  34. @accounts = FollowSuggestion.get(current_user.account_id)
  35. render action: :index
  36. end
  37. def statuses
  38. @statuses = @account.statuses.with_includes.with_counters.paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a
  39. set_maps(@statuses)
  40. next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
  41. prev_path = statuses_api_v1_account_url(since_id: @statuses.first.id) if @statuses.size > 0
  42. set_pagination_headers(next_path, prev_path)
  43. end
  44. def follow
  45. FollowService.new.call(current_user.account, @account.acct)
  46. set_relationship
  47. render action: :relationship
  48. end
  49. def block
  50. BlockService.new.call(current_user.account, @account)
  51. set_relationship
  52. render action: :relationship
  53. end
  54. def unfollow
  55. UnfollowService.new.call(current_user.account, @account)
  56. set_relationship
  57. render action: :relationship
  58. end
  59. def unblock
  60. UnblockService.new.call(current_user.account, @account)
  61. set_relationship
  62. render action: :relationship
  63. end
  64. def relationships
  65. ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
  66. @accounts = Account.where(id: ids).select('id')
  67. @following = Account.following_map(ids, current_user.account_id)
  68. @followed_by = Account.followed_by_map(ids, current_user.account_id)
  69. @blocking = Account.blocking_map(ids, current_user.account_id)
  70. end
  71. private
  72. def set_account
  73. @account = Account.find(params[:id])
  74. end
  75. def set_relationship
  76. @following = Account.following_map([@account.id], current_user.account_id)
  77. @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
  78. @blocking = Account.blocking_map([@account.id], current_user.account_id)
  79. end
  80. end