following_accounts_controller.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # frozen_string_literal: true
  2. class Api::V1::Accounts::FollowingAccountsController < 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. @accounts = load_accounts
  8. render json: @accounts, each_serializer: REST::AccountSerializer
  9. end
  10. private
  11. def set_account
  12. @account = Account.find(params[:account_id])
  13. end
  14. def load_accounts
  15. return [] if hide_results?
  16. scope = default_accounts
  17. scope = scope.where.not(id: current_account.excluded_from_timeline_account_ids) unless current_account.nil? || current_account.id == @account.id
  18. scope.merge(paginated_follows).to_a
  19. end
  20. def hide_results?
  21. @account.suspended? || (@account.hides_following? && current_account&.id != @account.id) || (current_account && @account.blocking?(current_account))
  22. end
  23. def default_accounts
  24. Account.includes(:passive_relationships, :account_stat).references(:passive_relationships)
  25. end
  26. def paginated_follows
  27. Follow.where(account: @account).paginate_by_max_id(
  28. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  29. params[:max_id],
  30. params[:since_id]
  31. )
  32. end
  33. def insert_pagination_headers
  34. set_pagination_headers(next_path, prev_path)
  35. end
  36. def next_path
  37. if records_continue?
  38. api_v1_account_following_index_url pagination_params(max_id: pagination_max_id)
  39. end
  40. end
  41. def prev_path
  42. unless @accounts.empty?
  43. api_v1_account_following_index_url pagination_params(since_id: pagination_since_id)
  44. end
  45. end
  46. def pagination_max_id
  47. @accounts.last.passive_relationships.first.id
  48. end
  49. def pagination_since_id
  50. @accounts.first.passive_relationships.first.id
  51. end
  52. def records_continue?
  53. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  54. end
  55. def pagination_params(core_params)
  56. params.slice(:limit).permit(:limit).merge(core_params)
  57. end
  58. end