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.

70 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Accounts::FollowerAccountsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read }
  4. before_action :set_account
  5. after_action :insert_pagination_headers
  6. respond_to :json
  7. def index
  8. @accounts = load_accounts
  9. render json: @accounts, each_serializer: REST::AccountSerializer
  10. end
  11. private
  12. def set_account
  13. @account = Account.find(params[:account_id])
  14. end
  15. def load_accounts
  16. return [] if @account.user_hides_network? && current_account.id != @account.id
  17. default_accounts.merge(paginated_follows).to_a
  18. end
  19. def default_accounts
  20. Account.includes(:active_relationships).references(:active_relationships)
  21. end
  22. def paginated_follows
  23. Follow.where(target_account: @account).paginate_by_max_id(
  24. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  25. params[:max_id],
  26. params[:since_id]
  27. )
  28. end
  29. def insert_pagination_headers
  30. set_pagination_headers(next_path, prev_path)
  31. end
  32. def next_path
  33. if records_continue?
  34. api_v1_account_followers_url pagination_params(max_id: pagination_max_id)
  35. end
  36. end
  37. def prev_path
  38. unless @accounts.empty?
  39. api_v1_account_followers_url pagination_params(since_id: pagination_since_id)
  40. end
  41. end
  42. def pagination_max_id
  43. @accounts.last.active_relationships.first.id
  44. end
  45. def pagination_since_id
  46. @accounts.first.active_relationships.first.id
  47. end
  48. def records_continue?
  49. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  50. end
  51. def pagination_params(core_params)
  52. params.slice(:limit).permit(:limit).merge(core_params)
  53. end
  54. end