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
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-03-18 03:09:16 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-03-18 03:09:16 +0300
commit2e31c85a97183814ffa7ba5cc58f7bbad668fb2b (patch)
tree18be488a2381014d05dd0323228d00c153251a0f /lib/banzai
parent154b9bae142ba15fec753f44327654595094b879 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/banzai')
-rw-r--r--lib/banzai/filter/broadcast_message_placeholders_filter.rb57
-rw-r--r--lib/banzai/pipeline/post_process_pipeline.rb3
2 files changed, 59 insertions, 1 deletions
diff --git a/lib/banzai/filter/broadcast_message_placeholders_filter.rb b/lib/banzai/filter/broadcast_message_placeholders_filter.rb
new file mode 100644
index 00000000000..5b5e2f643c5
--- /dev/null
+++ b/lib/banzai/filter/broadcast_message_placeholders_filter.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+module Banzai
+ module Filter
+ # Replaces placeholders for broadcast messages with data from the current
+ # user or the instance.
+ class BroadcastMessagePlaceholdersFilter < HTML::Pipeline::Filter
+ def call
+ return doc unless context[:broadcast_message_placeholders]
+
+ doc.traverse { |node| replace_placeholders(node) }
+ end
+
+ private
+
+ def replace_placeholders(node)
+ if node.text? && !node.content.empty?
+ node.content = replace_content(node.content)
+ elsif href = link_href(node)
+ href.value = replace_content(href.value, url_safe_encoding: true)
+ end
+
+ node
+ end
+
+ def link_href(node)
+ node.element? &&
+ node.name == 'a' &&
+ node.attribute_nodes.find { |a| a.name == "href" }
+ end
+
+ def replace_content(content, url_safe_encoding: false)
+ placeholders.each do |placeholder, method|
+ regex = Regexp.new("{{#{placeholder}}}|#{CGI.escape("{{#{placeholder}}}")}")
+ value = url_safe_encoding ? CGI.escape(method.call.to_s) : method.call.to_s
+ content.gsub!(regex, value)
+ end
+
+ content
+ end
+
+ def placeholders
+ {
+ "email" => -> { current_user.try(:email) },
+ "name" => -> { current_user.try(:name) },
+ "user_id" => -> { current_user.try(:id) },
+ "username" => -> { current_user.try(:username) },
+ "instance_id" => -> { Gitlab::CurrentSettings.try(:uuid) }
+ }
+ end
+
+ def current_user
+ context[:current_user]
+ end
+ end
+ end
+end
diff --git a/lib/banzai/pipeline/post_process_pipeline.rb b/lib/banzai/pipeline/post_process_pipeline.rb
index 5e02d972614..8236b702147 100644
--- a/lib/banzai/pipeline/post_process_pipeline.rb
+++ b/lib/banzai/pipeline/post_process_pipeline.rb
@@ -8,7 +8,8 @@ module Banzai
def self.filters
@filters ||= FilterArray[
*internal_link_filters,
- Filter::AbsoluteLinkFilter
+ Filter::AbsoluteLinkFilter,
+ Filter::BroadcastMessagePlaceholdersFilter
]
end