blocks_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # frozen_string_literal: true
  2. class Api::V1::BlocksController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :follow, :read, :'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. .joins(:target_account)
  17. .merge(Account.without_suspended)
  18. .where(account: current_account)
  19. .paginate_by_max_id(
  20. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  21. params[:max_id],
  22. params[:since_id]
  23. )
  24. end
  25. def insert_pagination_headers
  26. set_pagination_headers(next_path, prev_path)
  27. end
  28. def next_path
  29. api_v1_blocks_url pagination_params(max_id: pagination_max_id) if records_continue?
  30. end
  31. def prev_path
  32. api_v1_blocks_url pagination_params(since_id: pagination_since_id) unless paginated_blocks.empty?
  33. end
  34. def pagination_max_id
  35. paginated_blocks.last.id
  36. end
  37. def pagination_since_id
  38. paginated_blocks.first.id
  39. end
  40. def records_continue?
  41. paginated_blocks.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  42. end
  43. def pagination_params(core_params)
  44. params.slice(:limit).permit(:limit).merge(core_params)
  45. end
  46. end