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.

72 lines
1.4 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::EndorsementsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read, :'read:accounts' }
  4. before_action :require_user!
  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 load_accounts
  13. if unlimited?
  14. endorsed_accounts.all
  15. else
  16. endorsed_accounts.paginate_by_max_id(
  17. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  18. params[:max_id],
  19. params[:since_id]
  20. )
  21. end
  22. end
  23. def endorsed_accounts
  24. current_account.endorsed_accounts.includes(:account_stat)
  25. end
  26. def insert_pagination_headers
  27. set_pagination_headers(next_path, prev_path)
  28. end
  29. def next_path
  30. return if unlimited?
  31. if records_continue?
  32. api_v1_endorsements_url pagination_params(max_id: pagination_max_id)
  33. end
  34. end
  35. def prev_path
  36. return if unlimited?
  37. unless @accounts.empty?
  38. api_v1_endorsements_url pagination_params(since_id: pagination_since_id)
  39. end
  40. end
  41. def pagination_max_id
  42. @accounts.last.id
  43. end
  44. def pagination_since_id
  45. @accounts.first.id
  46. end
  47. def records_continue?
  48. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  49. end
  50. def pagination_params(core_params)
  51. params.slice(:limit).permit(:limit).merge(core_params)
  52. end
  53. def unlimited?
  54. params[:limit] == '0'
  55. end
  56. end