2020-01-10 10:07:47 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
module Helpers
|
|
|
|
module PaginationStrategies
|
2021-09-07 05:11:43 -04:00
|
|
|
def paginate_with_strategies(relation, request_scope = nil)
|
2020-05-05 17:09:42 -04:00
|
|
|
paginator = paginator(relation, request_scope)
|
2020-01-10 10:07:47 -05:00
|
|
|
|
2021-09-07 05:11:43 -04:00
|
|
|
result = if block_given?
|
|
|
|
yield(paginator.paginate(relation))
|
|
|
|
else
|
|
|
|
paginator.paginate(relation)
|
|
|
|
end
|
|
|
|
|
|
|
|
result.tap do |records, _|
|
2020-01-10 10:07:47 -05:00
|
|
|
paginator.finalize(records)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-05 17:09:42 -04:00
|
|
|
def paginator(relation, request_scope = nil)
|
|
|
|
return keyset_paginator(relation) if keyset_pagination_enabled?
|
2020-01-10 10:07:47 -05:00
|
|
|
|
2020-05-05 17:09:42 -04:00
|
|
|
offset_paginator(relation, request_scope)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2020-01-10 10:07:47 -05:00
|
|
|
|
2020-05-05 17:09:42 -04:00
|
|
|
def keyset_paginator(relation)
|
2021-09-07 05:11:43 -04:00
|
|
|
if cursor_based_keyset_pagination_supported?(relation)
|
|
|
|
request_context_class = Gitlab::Pagination::Keyset::CursorBasedRequestContext
|
|
|
|
paginator_class = Gitlab::Pagination::Keyset::CursorPager
|
|
|
|
availability_checker = Gitlab::Pagination::CursorBasedKeyset
|
|
|
|
else
|
|
|
|
request_context_class = Gitlab::Pagination::Keyset::RequestContext
|
|
|
|
paginator_class = Gitlab::Pagination::Keyset::Pager
|
|
|
|
availability_checker = Gitlab::Pagination::Keyset
|
|
|
|
end
|
|
|
|
|
|
|
|
request_context = request_context_class.new(self)
|
|
|
|
|
|
|
|
unless availability_checker.available?(request_context, relation)
|
2020-01-10 10:07:47 -05:00
|
|
|
return error!('Keyset pagination is not yet available for this type of request', 405)
|
|
|
|
end
|
|
|
|
|
2021-09-07 05:11:43 -04:00
|
|
|
paginator_class.new(request_context)
|
2020-01-10 10:07:47 -05:00
|
|
|
end
|
|
|
|
|
2020-05-05 17:09:42 -04:00
|
|
|
def offset_paginator(relation, request_scope)
|
|
|
|
offset_limit = limit_for_scope(request_scope)
|
2021-09-07 05:11:43 -04:00
|
|
|
if (Gitlab::Pagination::Keyset.available_for_type?(relation) ||
|
|
|
|
cursor_based_keyset_pagination_supported?(relation)) &&
|
|
|
|
offset_limit_exceeded?(offset_limit)
|
|
|
|
|
2020-05-05 17:09:42 -04:00
|
|
|
return error!("Offset pagination has a maximum allowed offset of #{offset_limit} " \
|
|
|
|
"for requests that return objects of type #{relation.klass}. " \
|
|
|
|
"Remaining records can be retrieved using keyset pagination.", 405)
|
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::Pagination::OffsetPagination.new(self)
|
|
|
|
end
|
2020-01-10 10:07:47 -05:00
|
|
|
|
2021-09-07 05:11:43 -04:00
|
|
|
def cursor_based_keyset_pagination_supported?(relation)
|
|
|
|
Gitlab::Pagination::CursorBasedKeyset.available_for_type?(relation)
|
|
|
|
end
|
|
|
|
|
2020-01-10 10:07:47 -05:00
|
|
|
def keyset_pagination_enabled?
|
2020-01-22 13:08:47 -05:00
|
|
|
params[:pagination] == 'keyset'
|
2020-01-10 10:07:47 -05:00
|
|
|
end
|
2020-05-05 17:09:42 -04:00
|
|
|
|
|
|
|
def limit_for_scope(scope)
|
|
|
|
(scope || Plan.default).actual_limits.offset_pagination_limit
|
|
|
|
end
|
|
|
|
|
|
|
|
def offset_limit_exceeded?(offset_limit)
|
2020-08-05 11:09:59 -04:00
|
|
|
offset_limit > 0 && params[:page] * params[:per_page] > offset_limit
|
2020-05-05 17:09:42 -04:00
|
|
|
end
|
2020-01-10 10:07:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|