endorsements_controller.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. api_v1_endorsements_url pagination_params(max_id: pagination_max_id) if records_continue?
  31. end
  32. def prev_path
  33. return if unlimited?
  34. api_v1_endorsements_url pagination_params(since_id: pagination_since_id) unless @accounts.empty?
  35. end
  36. def pagination_max_id
  37. @accounts.last.id
  38. end
  39. def pagination_since_id
  40. @accounts.first.id
  41. end
  42. def records_continue?
  43. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  44. end
  45. def pagination_params(core_params)
  46. params.slice(:limit).permit(:limit).merge(core_params)
  47. end
  48. def unlimited?
  49. params[:limit] == '0'
  50. end
  51. end