endorsements_controller.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. def index
  7. @accounts = load_accounts
  8. render json: @accounts, each_serializer: REST::AccountSerializer
  9. end
  10. private
  11. def load_accounts
  12. if unlimited?
  13. endorsed_accounts.all
  14. else
  15. endorsed_accounts.paginate_by_max_id(
  16. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  17. params[:max_id],
  18. params[:since_id]
  19. )
  20. end
  21. end
  22. def endorsed_accounts
  23. current_account.endorsed_accounts.includes(:account_stat).without_suspended
  24. end
  25. def insert_pagination_headers
  26. set_pagination_headers(next_path, prev_path)
  27. end
  28. def next_path
  29. return if unlimited?
  30. if records_continue?
  31. api_v1_endorsements_url pagination_params(max_id: pagination_max_id)
  32. end
  33. end
  34. def prev_path
  35. return if unlimited?
  36. unless @accounts.empty?
  37. api_v1_endorsements_url pagination_params(since_id: pagination_since_id)
  38. end
  39. end
  40. def pagination_max_id
  41. @accounts.last.id
  42. end
  43. def pagination_since_id
  44. @accounts.first.id
  45. end
  46. def records_continue?
  47. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  48. end
  49. def pagination_params(core_params)
  50. params.slice(:limit).permit(:limit).merge(core_params)
  51. end
  52. def unlimited?
  53. params[:limit] == '0'
  54. end
  55. end