summaryrefslogtreecommitdiff
path: root/lib/api/helpers/pagination_strategies.rb
blob: 4e244ea589e3ab6fdf13d1cbdc69934a9a5d721d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

module API
  module Helpers
    module PaginationStrategies
      def paginate_with_strategies(relation, request_scope = nil)
        paginator = paginator(relation, request_scope)

        result = if block_given?
                   yield(paginator.paginate(relation))
                 else
                   paginator.paginate(relation)
                 end

        result.tap do |records, _|
          paginator.finalize(records)
        end
      end

      def paginator(relation, request_scope = nil)
        return keyset_paginator(relation) if keyset_pagination_enabled?

        offset_paginator(relation, request_scope)
      end

      private

      def keyset_paginator(relation)
        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)
          return error!('Keyset pagination is not yet available for this type of request', 405)
        end

        paginator_class.new(request_context)
      end

      def offset_paginator(relation, request_scope)
        offset_limit = limit_for_scope(request_scope)
        if (Gitlab::Pagination::Keyset.available_for_type?(relation) ||
            cursor_based_keyset_pagination_supported?(relation)) &&
            cursor_based_keyset_pagination_enforced?(relation) &&
            offset_limit_exceeded?(offset_limit)

          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

      def cursor_based_keyset_pagination_supported?(relation)
        Gitlab::Pagination::CursorBasedKeyset.available_for_type?(relation)
      end

      def cursor_based_keyset_pagination_enforced?(relation)
        Gitlab::Pagination::CursorBasedKeyset.enforced_for_type?(relation)
      end

      def keyset_pagination_enabled?
        params[:pagination] == 'keyset'
      end

      def limit_for_scope(scope)
        (scope || Plan.default).actual_limits.offset_pagination_limit
      end

      def offset_limit_exceeded?(offset_limit)
        offset_limit > 0 && params[:page] * params[:per_page] > offset_limit
      end
    end
  end
end