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.
 
 
 
 

75 lines
1.7 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. default_accounts.merge(paginated_follows).to_a
  18. end
  19. def hide_results?
  20. (@account.user_hides_network? && current_account.id != @account.id) || (current_account && @account.blocking?(current_account))
  21. end
  22. def default_accounts
  23. Account.includes(:passive_relationships, :account_stat).references(:passive_relationships)
  24. end
  25. def paginated_follows
  26. Follow.where(account: @account).paginate_by_max_id(
  27. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  28. params[:max_id],
  29. params[:since_id]
  30. )
  31. end
  32. def insert_pagination_headers
  33. set_pagination_headers(next_path, prev_path)
  34. end
  35. def next_path
  36. if records_continue?
  37. api_v1_account_following_index_url pagination_params(max_id: pagination_max_id)
  38. end
  39. end
  40. def prev_path
  41. unless @accounts.empty?
  42. api_v1_account_following_index_url pagination_params(since_id: pagination_since_id)
  43. end
  44. end
  45. def pagination_max_id
  46. @accounts.last.passive_relationships.first.id
  47. end
  48. def pagination_since_id
  49. @accounts.first.passive_relationships.first.id
  50. end
  51. def records_continue?
  52. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  53. end
  54. def pagination_params(core_params)
  55. params.slice(:limit).permit(:limit).merge(core_params)
  56. end
  57. end