Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFrancisco Javier López <fjlopez@gitlab.com>2017-11-06 19:52:56 +0300
committerDouwe Maan <douwe@gitlab.com>2017-11-06 19:52:56 +0300
commitbf0331dc72db658b012d58e33ca47da6d8f99d46 (patch)
treef596c4876cd50bf13afd1f3e60ccd57640b61656 /lib
parent34a205b308d80d600c5651cfb9694f978ef01cab (diff)
Resolve "DashboardController#activity.json is slow due to SQL"
Diffstat (limited to 'lib')
-rw-r--r--lib/banzai.rb4
-rw-r--r--lib/banzai/filter/absolute_link_filter.rb34
-rw-r--r--lib/banzai/filter/abstract_reference_filter.rb24
-rw-r--r--lib/banzai/filter/reference_filter.rb2
-rw-r--r--lib/banzai/filter/user_reference_filter.rb15
-rw-r--r--lib/banzai/note_renderer.rb21
-rw-r--r--lib/banzai/object_renderer.rb7
-rw-r--r--lib/banzai/pipeline/post_process_pipeline.rb3
-rw-r--r--lib/banzai/renderer.rb11
-rw-r--r--lib/banzai/request_store_reference_cache.rb27
10 files changed, 88 insertions, 60 deletions
diff --git a/lib/banzai.rb b/lib/banzai.rb
index 35ca234c1ba..5df98f66f3b 100644
--- a/lib/banzai.rb
+++ b/lib/banzai.rb
@@ -3,8 +3,8 @@ module Banzai
Renderer.render(text, context)
end
- def self.render_field(object, field)
- Renderer.render_field(object, field)
+ def self.render_field(object, field, context = {})
+ Renderer.render_field(object, field, context)
end
def self.cache_collection_render(texts_and_contexts)
diff --git a/lib/banzai/filter/absolute_link_filter.rb b/lib/banzai/filter/absolute_link_filter.rb
new file mode 100644
index 00000000000..1ec6201523f
--- /dev/null
+++ b/lib/banzai/filter/absolute_link_filter.rb
@@ -0,0 +1,34 @@
+require 'uri'
+
+module Banzai
+ module Filter
+ # HTML filter that converts relative urls into absolute ones.
+ class AbsoluteLinkFilter < HTML::Pipeline::Filter
+ def call
+ return doc unless context[:only_path] == false
+
+ doc.search('a.gfm').each do |el|
+ process_link_attr el.attribute('href')
+ end
+
+ doc
+ end
+
+ protected
+
+ def process_link_attr(html_attr)
+ return if html_attr.blank?
+ return if html_attr.value.start_with?('//')
+
+ uri = URI(html_attr.value)
+ html_attr.value = absolute_link_attr(uri) if uri.relative?
+ rescue URI::Error
+ # noop
+ end
+
+ def absolute_link_attr(uri)
+ URI.join(Gitlab.config.gitlab.url, uri).to_s
+ end
+ end
+ end
+end
diff --git a/lib/banzai/filter/abstract_reference_filter.rb b/lib/banzai/filter/abstract_reference_filter.rb
index a0f7e4e5ad5..9fef386de16 100644
--- a/lib/banzai/filter/abstract_reference_filter.rb
+++ b/lib/banzai/filter/abstract_reference_filter.rb
@@ -311,30 +311,6 @@ module Banzai
def project_refs_cache
RequestStore[:banzai_project_refs] ||= {}
end
-
- def cached_call(request_store_key, cache_key, path: [])
- if RequestStore.active?
- cache = RequestStore[request_store_key] ||= Hash.new do |hash, key|
- hash[key] = Hash.new { |h, k| h[k] = {} }
- end
-
- cache = cache.dig(*path) if path.any?
-
- get_or_set_cache(cache, cache_key) { yield }
- else
- yield
- end
- end
-
- def get_or_set_cache(cache, key)
- if cache.key?(key)
- cache[key]
- else
- value = yield
- cache[key] = value if key.present?
- value
- end
- end
end
end
end
diff --git a/lib/banzai/filter/reference_filter.rb b/lib/banzai/filter/reference_filter.rb
index c6ae28adf87..b9d5ecf70ec 100644
--- a/lib/banzai/filter/reference_filter.rb
+++ b/lib/banzai/filter/reference_filter.rb
@@ -8,6 +8,8 @@ module Banzai
# :project (required) - Current project, ignored if reference is cross-project.
# :only_path - Generate path-only links.
class ReferenceFilter < HTML::Pipeline::Filter
+ include RequestStoreReferenceCache
+
class << self
attr_accessor :reference_type
end
diff --git a/lib/banzai/filter/user_reference_filter.rb b/lib/banzai/filter/user_reference_filter.rb
index afb6e25963c..c7fa8a8119f 100644
--- a/lib/banzai/filter/user_reference_filter.rb
+++ b/lib/banzai/filter/user_reference_filter.rb
@@ -60,10 +60,14 @@ module Banzai
self.class.references_in(text) do |match, username|
if username == 'all' && !skip_project_check?
link_to_all(link_content: link_content)
- elsif namespace = namespaces[username.downcase]
- link_to_namespace(namespace, link_content: link_content) || match
else
- match
+ cached_call(:banzai_url_for_object, match, path: [User, username.downcase]) do
+ if namespace = namespaces[username.downcase]
+ link_to_namespace(namespace, link_content: link_content) || match
+ else
+ match
+ end
+ end
end
end
end
@@ -74,7 +78,10 @@ module Banzai
# The keys of this Hash are the namespace paths, the values the
# corresponding Namespace objects.
def namespaces
- @namespaces ||= Namespace.where_full_path_in(usernames).index_by(&:full_path).transform_keys(&:downcase)
+ @namespaces ||= Namespace.eager_load(:owner, :route)
+ .where_full_path_in(usernames)
+ .index_by(&:full_path)
+ .transform_keys(&:downcase)
end
# Returns all usernames referenced in the current document.
diff --git a/lib/banzai/note_renderer.rb b/lib/banzai/note_renderer.rb
deleted file mode 100644
index 2b7c10f1a0e..00000000000
--- a/lib/banzai/note_renderer.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-module Banzai
- module NoteRenderer
- # Renders a collection of Note instances.
- #
- # notes - The notes to render.
- # project - The project to use for redacting.
- # user - The user viewing the notes.
- # path - The request path.
- # wiki - The project's wiki.
- # git_ref - The current Git reference.
- def self.render(notes, project, user = nil, path = nil, wiki = nil, git_ref = nil)
- renderer = ObjectRenderer.new(project,
- user,
- requested_path: path,
- project_wiki: wiki,
- ref: git_ref)
-
- renderer.render(notes, :note)
- end
- end
-end
diff --git a/lib/banzai/object_renderer.rb b/lib/banzai/object_renderer.rb
index e40556e869c..9bb8ed913d8 100644
--- a/lib/banzai/object_renderer.rb
+++ b/lib/banzai/object_renderer.rb
@@ -37,7 +37,7 @@ module Banzai
objects.each_with_index do |object, index|
redacted_data = redacted[index]
- object.__send__("redacted_#{attribute}_html=", redacted_data[:document].to_html.html_safe) # rubocop:disable GitlabSecurity/PublicSend
+ object.__send__("redacted_#{attribute}_html=", redacted_data[:document].to_html(save_options).html_safe) # rubocop:disable GitlabSecurity/PublicSend
object.user_visible_reference_count = redacted_data[:visible_reference_count] if object.respond_to?(:user_visible_reference_count)
end
end
@@ -83,5 +83,10 @@ module Banzai
skip_redaction: true
)
end
+
+ def save_options
+ return {} unless base_context[:xhtml]
+ { save_with: Nokogiri::XML::Node::SaveOptions::AS_XHTML }
+ end
end
end
diff --git a/lib/banzai/pipeline/post_process_pipeline.rb b/lib/banzai/pipeline/post_process_pipeline.rb
index 131ac3b0eec..dcd52bc03c7 100644
--- a/lib/banzai/pipeline/post_process_pipeline.rb
+++ b/lib/banzai/pipeline/post_process_pipeline.rb
@@ -3,9 +3,10 @@ module Banzai
class PostProcessPipeline < BasePipeline
def self.filters
FilterArray[
+ Filter::RedactorFilter,
Filter::RelativeLinkFilter,
Filter::IssuableStateFilter,
- Filter::RedactorFilter
+ Filter::AbsoluteLinkFilter
]
end
diff --git a/lib/banzai/renderer.rb b/lib/banzai/renderer.rb
index 5f91884a878..5cb9adf52b0 100644
--- a/lib/banzai/renderer.rb
+++ b/lib/banzai/renderer.rb
@@ -32,12 +32,9 @@ module Banzai
# Convert a Markdown-containing field on an object into an HTML-safe String
# of HTML. This method is analogous to calling render(object.field), but it
# can cache the rendered HTML in the object, rather than Redis.
- #
- # The context to use is managed by the object and cannot be changed.
- # Use #render, passing it the field text, if a custom rendering is needed.
- def self.render_field(object, field)
+ def self.render_field(object, field, context = {})
unless object.respond_to?(:cached_markdown_fields)
- return cacheless_render_field(object, field)
+ return cacheless_render_field(object, field, context)
end
object.refresh_markdown_cache! unless object.cached_html_up_to_date?(field)
@@ -46,9 +43,9 @@ module Banzai
end
# Same as +render_field+, but without consulting or updating the cache field
- def self.cacheless_render_field(object, field, options = {})
+ def self.cacheless_render_field(object, field, context = {})
text = object.__send__(field) # rubocop:disable GitlabSecurity/PublicSend
- context = object.banzai_render_context(field).merge(options)
+ context = context.reverse_merge(object.banzai_render_context(field)) if object.respond_to?(:banzai_render_context)
cacheless_render(text, context)
end
diff --git a/lib/banzai/request_store_reference_cache.rb b/lib/banzai/request_store_reference_cache.rb
new file mode 100644
index 00000000000..426131442a2
--- /dev/null
+++ b/lib/banzai/request_store_reference_cache.rb
@@ -0,0 +1,27 @@
+module Banzai
+ module RequestStoreReferenceCache
+ def cached_call(request_store_key, cache_key, path: [])
+ if RequestStore.active?
+ cache = RequestStore[request_store_key] ||= Hash.new do |hash, key|
+ hash[key] = Hash.new { |h, k| h[k] = {} }
+ end
+
+ cache = cache.dig(*path) if path.any?
+
+ get_or_set_cache(cache, cache_key) { yield }
+ else
+ yield
+ end
+ end
+
+ def get_or_set_cache(cache, key)
+ if cache.key?(key)
+ cache[key]
+ else
+ value = yield
+ cache[key] = value if key.present?
+ value
+ end
+ end
+ end
+end