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.

63 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::BlocksController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :follow }
  4. before_action :require_user!
  5. after_action :insert_pagination_headers
  6. respond_to :json
  7. def index
  8. @accounts = load_accounts
  9. end
  10. private
  11. def load_accounts
  12. default_accounts.merge(paginated_blocks).to_a
  13. end
  14. def default_accounts
  15. Account.includes(:blocked_by).references(:blocked_by)
  16. end
  17. def paginated_blocks
  18. Block.where(account: current_account).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 @accounts.empty?
  34. api_v1_blocks_url pagination_params(since_id: pagination_since_id)
  35. end
  36. end
  37. def pagination_max_id
  38. @accounts.last.blocked_by_ids.last
  39. end
  40. def pagination_since_id
  41. @accounts.first.blocked_by_ids.first
  42. end
  43. def records_continue?
  44. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  45. end
  46. def pagination_params(core_params)
  47. params.permit(:limit).merge(core_params)
  48. end
  49. end