summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/references/reference_cache.rb
blob: 816ce973cadf97c080a4ee492d2fb747787a3d99 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# frozen_string_literal: true

module Banzai
  module Filter
    module References
      class ReferenceCache
        include Gitlab::Utils::StrongMemoize
        include RequestStoreReferenceCache

        def initialize(filter, context)
          @filter = filter
          @context = context
        end

        def load_reference_cache(nodes)
          load_references_per_parent(nodes)
          load_parent_per_reference
          load_records_per_parent

          @cache_loaded = true
        end

        # Loads all object references (e.g. issue IDs) per
        # project/group they belong to.
        def load_references_per_parent(nodes)
          @references_per_parent ||= {}

          @references_per_parent[parent_type] ||= begin
            refs = Hash.new { |hash, key| hash[key] = Set.new }

            if Feature.enabled?(:milestone_reference_pattern, default_enabled: :yaml)
              doc_search(refs)
            else
              node_search(nodes, refs)
            end
          end
        end

        def references_per_parent
          @references_per_parent[parent_type]
        end

        # Returns a Hash containing referenced projects grouped per their full
        # path.
        def load_parent_per_reference
          @per_reference ||= {}

          @per_reference[parent_type] ||= begin
            refs = references_per_parent.keys
            parent_ref = {}

            # if we already have a parent, no need to query it again
            refs.each do |ref|
              next unless ref

              if context[:project]&.full_path == ref
                parent_ref[ref] = context[:project]
              elsif context[:group]&.full_path == ref
                parent_ref[ref] = context[:group]
              end

              refs -= [ref] if parent_ref[ref]
            end

            find_for_paths(refs).index_by(&:full_path).merge(parent_ref)
          end
        end

        def parent_per_reference
          @per_reference[parent_type]
        end

        def load_records_per_parent
          @_records_per_project ||= {}

          @_records_per_project[filter.object_class.to_s.underscore] ||= begin
            hash = Hash.new { |h, k| h[k] = {} }

            parent_per_reference.each do |path, parent|
              record_ids = references_per_parent[path]

              filter.parent_records(parent, record_ids).each do |record|
                hash[parent][filter.record_identifier(record)] = record
              end
            end

            hash
          end
        end

        def records_per_parent
          @_records_per_project[filter.object_class.to_s.underscore]
        end

        def objects_for_paths(paths)
          klass = parent_type.to_s.camelize.constantize
          result = klass.where_full_path_in(paths)
          return result if parent_type == :group

          result.includes(namespace: :route) if parent_type == :project
        end

        # Returns projects for the given paths.
        def find_for_paths(paths)
          if Gitlab::SafeRequestStore.active?
            cache = refs_cache
            to_query = paths - cache.keys

            unless to_query.empty?
              records = objects_for_paths(to_query)

              found = []
              records.each do |record|
                ref = record.full_path
                get_or_set_cache(cache, ref) { record }
                found << ref
              end

              not_found = to_query - found
              not_found.each do |ref|
                get_or_set_cache(cache, ref) { nil }
              end
            end

            cache.slice(*paths).values.compact
          else
            objects_for_paths(paths)
          end
        end

        def current_parent_path
          strong_memoize(:current_parent_path) do
            parent&.full_path
          end
        end

        def current_project_namespace_path
          strong_memoize(:current_project_namespace_path) do
            project&.namespace&.full_path
          end
        end

        def full_project_path(namespace, project_ref)
          return current_parent_path unless project_ref

          namespace_ref = namespace || current_project_namespace_path
          "#{namespace_ref}/#{project_ref}"
        end

        def full_group_path(group_ref)
          return current_parent_path unless group_ref

          group_ref
        end

        def cache_loaded?
          !!@cache_loaded
        end

        private

        attr_accessor :filter, :context

        delegate :project, :group, :parent, :parent_type, to: :filter

        # Deprecated: https://gitlab.com/gitlab-org/gitlab/-/issues/336268
        def node_search(nodes, refs)
          nodes.each do |node|
            prepare_node_for_scan(node).scan(regex) do
              parent_path = if parent_type == :project
                              full_project_path($~[:namespace], $~[:project])
                            else
                              full_group_path($~[:group])
                            end

              ident = filter.identifier($~)
              refs[parent_path] << ident if ident
            end
          end

          refs
        end

        def doc_search(refs)
          prepare_doc_for_scan(filter.doc).to_enum(:scan, regex).each do
            parent_path = if parent_type == :project
                            full_project_path($~[:namespace], $~[:project])
                          else
                            full_group_path($~[:group])
                          end

            ident = filter.identifier($~)
            refs[parent_path] << ident if ident
          end

          refs
        end

        def regex
          strong_memoize(:regex) do
            [
              filter.object_class.link_reference_pattern,
              filter.object_class.reference_pattern
            ].compact.reduce { |a, b| Regexp.union(a, b) }
          end
        end

        def refs_cache
          Gitlab::SafeRequestStore["banzai_#{parent_type}_refs".to_sym] ||= {}
        end

        def prepare_doc_for_scan(doc)
          html = doc.to_html

          filter.requires_unescaping? ? unescape_html_entities(html) : html
        end

        # Deprecated: https://gitlab.com/gitlab-org/gitlab/-/issues/336268
        def prepare_node_for_scan(node)
          html = node.to_html

          filter.requires_unescaping? ? unescape_html_entities(html) : html
        end

        def unescape_html_entities(text)
          CGI.unescapeHTML(text.to_s)
        end
      end
    end
  end
end

Banzai::Filter::References::ReferenceCache.prepend_mod