blocks_controller.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # frozen_string_literal: true
  2. class Api::V1::BlocksController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :follow, :'read:blocks' }
  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. paginated_blocks.map(&:target_account)
  13. end
  14. def paginated_blocks
  15. @paginated_blocks ||= Block.eager_load(target_account: :account_stat)
  16. .where(account: current_account)
  17. .paginate_by_max_id(
  18. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  19. params[:max_id],
  20. params[:since_id]
  21. )
  22. end
  23. def insert_pagination_headers
  24. set_pagination_headers(next_path, prev_path)
  25. end
  26. def next_path
  27. if records_continue?
  28. api_v1_blocks_url pagination_params(max_id: pagination_max_id)
  29. end
  30. end
  31. def prev_path
  32. unless paginated_blocks.empty?
  33. api_v1_blocks_url pagination_params(since_id: pagination_since_id)
  34. end
  35. end
  36. def pagination_max_id
  37. paginated_blocks.last.id
  38. end
  39. def pagination_since_id
  40. paginated_blocks.first.id
  41. end
  42. def records_continue?
  43. paginated_blocks.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. end