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.

62 lines
1.5 KiB

  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. 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. paginated_blocks.map(&:target_account)
  14. end
  15. def paginated_blocks
  16. @paginated_blocks ||= Block.eager_load(:target_account)
  17. .where(account: current_account)
  18. .paginate_by_max_id(
  19. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  20. params[:max_id],
  21. params[:since_id]
  22. )
  23. end
  24. def insert_pagination_headers
  25. set_pagination_headers(next_path, prev_path)
  26. end
  27. def next_path
  28. if records_continue?
  29. api_v1_blocks_url pagination_params(max_id: pagination_max_id)
  30. end
  31. end
  32. def prev_path
  33. unless paginated_blocks.empty?
  34. api_v1_blocks_url pagination_params(since_id: pagination_since_id)
  35. end
  36. end
  37. def pagination_max_id
  38. paginated_blocks.last.id
  39. end
  40. def pagination_since_id
  41. paginated_blocks.first.id
  42. end
  43. def records_continue?
  44. paginated_blocks.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  45. end
  46. def pagination_params(core_params)
  47. params.slice(:limit).permit(:limit).merge(core_params)
  48. end
  49. end