闭社主体 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.

114 lines
3.8 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
  9. end
  10. def verify_credentials
  11. @account = current_user.account
  12. render action: :show
  13. end
  14. def following
  15. results = Follow.where(account: @account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  16. accounts = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h
  17. @accounts = results.map { |f| accounts[f.target_account_id] }
  18. set_account_counters_maps(@accounts)
  19. next_path = following_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  20. prev_path = following_api_v1_account_url(since_id: results.first.id) unless results.empty?
  21. set_pagination_headers(next_path, prev_path)
  22. render action: :index
  23. end
  24. def followers
  25. results = Follow.where(target_account: @account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  26. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  27. @accounts = results.map { |f| accounts[f.account_id] }
  28. set_account_counters_maps(@accounts)
  29. next_path = followers_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  30. prev_path = followers_api_v1_account_url(since_id: results.first.id) unless results.empty?
  31. set_pagination_headers(next_path, prev_path)
  32. render action: :index
  33. end
  34. def statuses
  35. @statuses = @account.statuses.paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id])
  36. @statuses = cache_collection(@statuses, Status)
  37. set_maps(@statuses)
  38. set_counters_maps(@statuses)
  39. next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
  40. prev_path = statuses_api_v1_account_url(since_id: @statuses.first.id) unless @statuses.empty?
  41. set_pagination_headers(next_path, prev_path)
  42. end
  43. def follow
  44. FollowService.new.call(current_user.account, @account.acct)
  45. set_relationship
  46. render action: :relationship
  47. end
  48. def block
  49. BlockService.new.call(current_user.account, @account)
  50. set_relationship
  51. render action: :relationship
  52. end
  53. def unfollow
  54. UnfollowService.new.call(current_user.account, @account)
  55. set_relationship
  56. render action: :relationship
  57. end
  58. def unblock
  59. UnblockService.new.call(current_user.account, @account)
  60. set_relationship
  61. render action: :relationship
  62. end
  63. def relationships
  64. ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
  65. @accounts = Account.where(id: ids).select('id')
  66. @following = Account.following_map(ids, current_user.account_id)
  67. @followed_by = Account.followed_by_map(ids, current_user.account_id)
  68. @blocking = Account.blocking_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. end
  85. end