2021-11-11 11:23:49 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Pagination
|
|
|
|
module CursorBasedKeyset
|
|
|
|
SUPPORTED_ORDERING = {
|
2022-08-13 15:12:31 +05:30
|
|
|
Group => { name: :asc },
|
2023-03-17 16:20:25 +05:30
|
|
|
AuditEvent => { id: :desc },
|
|
|
|
::Ci::Build => { id: :desc }
|
2021-11-11 11:23:49 +05:30
|
|
|
}.freeze
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
# 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-11-11 11:23:49 +05:30
|
|
|
def self.available_for_type?(relation)
|
|
|
|
SUPPORTED_ORDERING.key?(relation.klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.available?(cursor_based_request_context, relation)
|
|
|
|
available_for_type?(relation) &&
|
2023-03-04 22:38:38 +05:30
|
|
|
order_satisfied?(relation, cursor_based_request_context)
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
def self.enforced_for_type?(relation)
|
|
|
|
ENFORCED_TYPES.include?(relation.klass)
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
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
|