2021-09-07 05:11:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Pagination
|
|
|
|
module CursorBasedKeyset
|
|
|
|
SUPPORTED_ORDERING = {
|
2022-07-13 05:08:30 -04:00
|
|
|
Group => { name: :asc },
|
|
|
|
AuditEvent => { id: :desc }
|
2021-09-07 05:11:43 -04:00
|
|
|
}.freeze
|
|
|
|
|
2022-07-13 05:08:30 -04:00
|
|
|
# Relation types that are enforced in this list
|
|
|
|
# enforce the use of keyset pagination, thus erroring out requests
|
|
|
|
# made with offset pagination above a certain limit.
|
|
|
|
#
|
|
|
|
# In many cases this could introduce a breaking change
|
|
|
|
# so enforcement is optional.
|
|
|
|
ENFORCED_TYPES = [Group].freeze
|
|
|
|
|
2021-09-07 05:11:43 -04:00
|
|
|
def self.available_for_type?(relation)
|
|
|
|
SUPPORTED_ORDERING.key?(relation.klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.available?(cursor_based_request_context, relation)
|
|
|
|
available_for_type?(relation) &&
|
|
|
|
order_satisfied?(relation, cursor_based_request_context)
|
|
|
|
end
|
|
|
|
|
2022-07-13 05:08:30 -04:00
|
|
|
def self.enforced_for_type?(relation)
|
|
|
|
ENFORCED_TYPES.include?(relation.klass)
|
|
|
|
end
|
|
|
|
|
2021-09-07 05:11:43 -04:00
|
|
|
def self.order_satisfied?(relation, cursor_based_request_context)
|
|
|
|
order_by_from_request = cursor_based_request_context.order_by
|
|
|
|
|
|
|
|
SUPPORTED_ORDERING[relation.klass] == order_by_from_request
|
|
|
|
end
|
|
|
|
private_class_method :order_satisfied?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|