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.
 
 
 
 

77 lines
1.9 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Accounts::FollowingAccountsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read, :'read:accounts' }
  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 hide_results?
  17. scope = default_accounts
  18. scope = scope.where.not(id: current_account.excluded_from_timeline_account_ids) unless current_account.nil?
  19. scope.merge(paginated_follows).to_a
  20. end
  21. def hide_results?
  22. (@account.user_hides_network? && current_account&.id != @account.id) || (current_account && @account.blocking?(current_account))
  23. end
  24. def default_accounts
  25. Account.includes(:passive_relationships, :account_stat).references(:passive_relationships)
  26. end
  27. def paginated_follows
  28. Follow.where(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. if records_continue?
  39. api_v1_account_following_index_url pagination_params(max_id: pagination_max_id)
  40. end
  41. end
  42. def prev_path
  43. unless @accounts.empty?
  44. api_v1_account_following_index_url pagination_params(since_id: pagination_since_id)
  45. end
  46. end
  47. def pagination_max_id
  48. @accounts.last.passive_relationships.first.id
  49. end
  50. def pagination_since_id
  51. @accounts.first.passive_relationships.first.id
  52. end
  53. def records_continue?
  54. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  55. end
  56. def pagination_params(core_params)
  57. params.slice(:limit).permit(:limit).merge(core_params)
  58. end
  59. end