闭社主体 forked from https://github.com/tootsuite/mastodon
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.

116 lines
4.0 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(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 == 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(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 == 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(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 == 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. BlockService.new.call(current_user.account, @account)
  49. set_relationship
  50. render action: :relationship
  51. end
  52. def unfollow
  53. UnfollowService.new.call(current_user.account, @account)
  54. set_relationship
  55. render action: :relationship
  56. end
  57. def unblock
  58. UnblockService.new.call(current_user.account, @account)
  59. set_relationship
  60. render action: :relationship
  61. end
  62. def relationships
  63. ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
  64. @accounts = Account.where(id: ids).select('id')
  65. @following = Account.following_map(ids, current_user.account_id)
  66. @followed_by = Account.followed_by_map(ids, current_user.account_id)
  67. @blocking = Account.blocking_map(ids, current_user.account_id)
  68. @requested = Account.requested_map(ids, current_user.account_id)
  69. end
  70. def search
  71. limit = params[:limit] ? [DEFAULT_ACCOUNTS_LIMIT, params[:limit].to_i].min : DEFAULT_ACCOUNTS_LIMIT
  72. @accounts = SearchService.new.call(params[:q], limit, params[:resolve] == 'true')
  73. set_account_counters_maps(@accounts)
  74. render action: :index
  75. end
  76. private
  77. def set_account
  78. @account = Account.find(params[:id])
  79. end
  80. def set_relationship
  81. @following = Account.following_map([@account.id], current_user.account_id)
  82. @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
  83. @blocking = Account.blocking_map([@account.id], current_user.account_id)
  84. @requested = Account.requested_map([@account.id], current_user.account_id)
  85. end
  86. end