The code powering m.abunchtell.com https://m.abunchtell.com
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
3.9 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)).with_counters.map { |a| [a.id, a] }.to_h
  17. @accounts = results.map { |f| accounts[f.target_account_id] }
  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)).with_counters.map { |a| [a.id, a] }.to_h
  26. @accounts = results.map { |f| accounts[f.account_id] }
  27. next_path = followers_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  28. prev_path = followers_api_v1_account_url(since_id: results.first.id) unless results.empty?
  29. set_pagination_headers(next_path, prev_path)
  30. render action: :index
  31. end
  32. def common_followers
  33. @accounts = @account.common_followers_with(current_user.account)
  34. render action: :index
  35. end
  36. def suggestions
  37. @accounts = FollowSuggestion.get(current_user.account_id)
  38. render action: :index
  39. end
  40. def statuses
  41. @statuses = @account.statuses.with_includes.with_counters.paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a
  42. set_maps(@statuses)
  43. next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
  44. prev_path = statuses_api_v1_account_url(since_id: @statuses.first.id) unless @statuses.empty?
  45. set_pagination_headers(next_path, prev_path)
  46. end
  47. def follow
  48. FollowService.new.call(current_user.account, @account.acct)
  49. set_relationship
  50. render action: :relationship
  51. end
  52. def block
  53. BlockService.new.call(current_user.account, @account)
  54. set_relationship
  55. render action: :relationship
  56. end
  57. def unfollow
  58. UnfollowService.new.call(current_user.account, @account)
  59. set_relationship
  60. render action: :relationship
  61. end
  62. def unblock
  63. UnblockService.new.call(current_user.account, @account)
  64. set_relationship
  65. render action: :relationship
  66. end
  67. def relationships
  68. ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
  69. @accounts = Account.where(id: ids).select('id')
  70. @following = Account.following_map(ids, current_user.account_id)
  71. @followed_by = Account.followed_by_map(ids, current_user.account_id)
  72. @blocking = Account.blocking_map(ids, current_user.account_id)
  73. end
  74. def search
  75. limit = params[:limit] ? [DEFAULT_ACCOUNTS_LIMIT, params[:limit].to_i].min : DEFAULT_ACCOUNTS_LIMIT
  76. @accounts = SearchService.new.call(params[:q], limit, params[:resolve] == 'true')
  77. render action: :index
  78. end
  79. private
  80. def set_account
  81. @account = Account.find(params[:id])
  82. end
  83. def set_relationship
  84. @following = Account.following_map([@account.id], current_user.account_id)
  85. @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
  86. @blocking = Account.blocking_map([@account.id], current_user.account_id)
  87. end
  88. end