follower_accounts_controller.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # frozen_string_literal: true
  2. class Api::V1::Accounts::FollowerAccountsController < Api::BaseController
  3. before_action -> { authorize_if_got_token! :read, :'read:accounts' }
  4. before_action :set_account
  5. after_action :insert_pagination_headers
  6. def index
  7. cache_if_unauthenticated!
  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 hide_results?
  17. scope = default_accounts
  18. scope = scope.where.not(id: current_account.excluded_from_timeline_account_ids) unless current_account.nil? || current_account.id == @account.id
  19. scope.merge(paginated_follows).to_a
  20. end
  21. def hide_results?
  22. @account.unavailable? || (@account.hides_followers? && current_account&.id != @account.id) || (current_account && @account.blocking?(current_account))
  23. end
  24. def default_accounts
  25. Account.includes(:active_relationships, :account_stat).references(:active_relationships)
  26. end
  27. def paginated_follows
  28. Follow.where(target_account: @account).paginate_by_max_id(
  29. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  30. params[:max_id],
  31. params[:since_id]
  32. )
  33. end
  34. def insert_pagination_headers
  35. set_pagination_headers(next_path, prev_path)
  36. end
  37. def next_path
  38. api_v1_account_followers_url pagination_params(max_id: pagination_max_id) if records_continue?
  39. end
  40. def prev_path
  41. api_v1_account_followers_url pagination_params(since_id: pagination_since_id) unless @accounts.empty?
  42. end
  43. def pagination_max_id
  44. @accounts.last.active_relationships.first.id
  45. end
  46. def pagination_since_id
  47. @accounts.first.active_relationships.first.id
  48. end
  49. def records_continue?
  50. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  51. end
  52. def pagination_params(core_params)
  53. params.slice(:limit).permit(:limit).merge(core_params)
  54. end
  55. end