2018-07-23 09:32:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-04-14 22:34:01 +00:00
|
|
|
# HTML filter that replaces user or group references with links.
|
2015-04-03 00:46:43 +00:00
|
|
|
#
|
|
|
|
# A special `@all` reference is also supported.
|
2015-04-14 22:34:01 +00:00
|
|
|
class UserReferenceFilter < ReferenceFilter
|
2016-05-26 11:16:43 +00:00
|
|
|
self.reference_type = :user
|
|
|
|
|
2015-04-03 00:46:43 +00:00
|
|
|
# Public: Find `@user` user references in text
|
|
|
|
#
|
|
|
|
# UserReferenceFilter.references_in(text) do |match, username|
|
|
|
|
# "<a href=...>@#{user}</a>"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# text - String text to search.
|
|
|
|
#
|
|
|
|
# Yields the String match, and the String user name.
|
|
|
|
#
|
|
|
|
# Returns a String replaced with the return of the block.
|
|
|
|
def self.references_in(text)
|
2015-05-14 20:59:39 +00:00
|
|
|
text.gsub(User.reference_pattern) do |match|
|
2015-04-03 00:46:43 +00:00
|
|
|
yield match, $~[:user]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
2017-11-01 17:35:14 +00:00
|
|
|
return doc if project.nil? && group.nil? && !skip_project_check?
|
2016-03-24 14:57:22 +00:00
|
|
|
|
|
|
|
ref_pattern = User.reference_pattern
|
|
|
|
ref_pattern_start = /\A#{ref_pattern}\z/
|
2017-01-20 10:28:40 +00:00
|
|
|
|
2016-05-30 13:56:50 +00:00
|
|
|
nodes.each do |node|
|
2016-03-24 14:57:22 +00:00
|
|
|
if text_node?(node)
|
|
|
|
replace_text_when_pattern_matches(node, ref_pattern) do |content|
|
|
|
|
user_link_filter(content)
|
|
|
|
end
|
|
|
|
elsif element_node?(node)
|
2016-09-30 09:03:16 +00:00
|
|
|
yield_valid_link(node) do |link, inner_html|
|
2016-03-24 14:57:22 +00:00
|
|
|
if link =~ ref_pattern_start
|
|
|
|
replace_link_node_with_href(node, link) do
|
2016-09-30 09:03:16 +00:00
|
|
|
user_link_filter(link, link_content: inner_html)
|
2016-03-24 14:57:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-12-01 15:25:56 +00:00
|
|
|
end
|
2016-03-24 14:57:22 +00:00
|
|
|
|
|
|
|
doc
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Replace `@user` user references in text with links to the referenced
|
|
|
|
# user's profile page.
|
|
|
|
#
|
|
|
|
# text - String text to replace references in.
|
2016-09-30 09:03:16 +00:00
|
|
|
# link_content - Original content of the link being replaced.
|
2015-04-03 00:46:43 +00:00
|
|
|
#
|
|
|
|
# Returns a String with `@user` references replaced with links. All links
|
|
|
|
# have `gfm` and `gfm-project_member` class names attached for styling.
|
2016-09-30 09:03:16 +00:00
|
|
|
def user_link_filter(text, link_content: nil)
|
2015-04-25 22:07:20 +00:00
|
|
|
self.class.references_in(text) do |match, username|
|
2017-01-18 23:37:55 +00:00
|
|
|
if username == 'all' && !skip_project_check?
|
2016-09-30 09:03:16 +00:00
|
|
|
link_to_all(link_content: link_content)
|
2015-04-03 00:46:43 +00:00
|
|
|
else
|
2017-11-06 16:52:56 +00:00
|
|
|
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
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-30 13:56:50 +00:00
|
|
|
# Returns a Hash containing all Namespace objects for the username
|
|
|
|
# references in the current document.
|
|
|
|
#
|
|
|
|
# The keys of this Hash are the namespace paths, the values the
|
|
|
|
# corresponding Namespace objects.
|
|
|
|
def namespaces
|
2017-11-06 16:52:56 +00:00
|
|
|
@namespaces ||= Namespace.eager_load(:owner, :route)
|
|
|
|
.where_full_path_in(usernames)
|
|
|
|
.index_by(&:full_path)
|
|
|
|
.transform_keys(&:downcase)
|
2016-05-30 13:56:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns all usernames referenced in the current document.
|
|
|
|
def usernames
|
|
|
|
refs = Set.new
|
|
|
|
|
|
|
|
nodes.each do |node|
|
|
|
|
node.to_html.scan(User.reference_pattern) do
|
|
|
|
refs << $~[:user]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
refs.to_a
|
|
|
|
end
|
|
|
|
|
2015-04-14 22:34:01 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def urls
|
2016-03-24 16:00:26 +00:00
|
|
|
Gitlab::Routing.url_helpers
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
2015-04-25 22:07:20 +00:00
|
|
|
def link_class
|
2018-12-08 03:12:23 +00:00
|
|
|
reference_class(:project_member, tooltip: false)
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-09-30 09:03:16 +00:00
|
|
|
def link_to_all(link_content: nil)
|
2016-05-26 11:16:43 +00:00
|
|
|
author = context[:author]
|
|
|
|
|
2017-11-01 17:35:14 +00:00
|
|
|
if author && !team_member?(author)
|
2016-09-30 09:03:16 +00:00
|
|
|
link_content
|
2016-09-30 09:14:42 +00:00
|
|
|
else
|
2017-11-01 17:35:14 +00:00
|
|
|
parent_url(link_content, author)
|
2016-09-30 09:14:42 +00:00
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-09-30 09:03:16 +00:00
|
|
|
def link_to_namespace(namespace, link_content: nil)
|
2015-04-25 22:07:20 +00:00
|
|
|
if namespace.is_a?(Group)
|
2017-02-13 11:26:33 +00:00
|
|
|
link_to_group(namespace.full_path, namespace, link_content: link_content)
|
2015-04-25 22:07:20 +00:00
|
|
|
else
|
2016-09-30 09:03:16 +00:00
|
|
|
link_to_user(namespace.path, namespace, link_content: link_content)
|
2015-04-25 22:07:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-30 09:03:16 +00:00
|
|
|
def link_to_group(group, namespace, link_content: nil)
|
2015-04-26 01:26:43 +00:00
|
|
|
url = urls.group_url(group, only_path: context[:only_path])
|
2015-10-07 15:00:48 +00:00
|
|
|
data = data_attribute(group: namespace.id)
|
2016-09-30 09:03:16 +00:00
|
|
|
content = link_content || Group.reference_prefix + group
|
2015-11-17 11:55:16 +00:00
|
|
|
|
2017-03-01 23:07:40 +00:00
|
|
|
link_tag(url, data, content, namespace.full_name)
|
2015-04-25 22:07:20 +00:00
|
|
|
end
|
|
|
|
|
2016-09-30 09:03:16 +00:00
|
|
|
def link_to_user(user, namespace, link_content: nil)
|
2015-04-25 22:07:20 +00:00
|
|
|
url = urls.user_url(user, only_path: context[:only_path])
|
2015-10-07 15:00:48 +00:00
|
|
|
data = data_attribute(user: namespace.owner_id)
|
2016-09-30 09:03:16 +00:00
|
|
|
content = link_content || User.reference_prefix + user
|
2015-11-17 11:55:16 +00:00
|
|
|
|
2016-09-30 09:03:16 +00:00
|
|
|
link_tag(url, data, content, namespace.owner_name)
|
2015-11-17 11:55:16 +00:00
|
|
|
end
|
|
|
|
|
2016-09-30 09:03:16 +00:00
|
|
|
def link_tag(url, data, link_content, title)
|
|
|
|
%(<a href="#{url}" #{data} class="#{link_class}" title="#{escape_once(title)}">#{link_content}</a>)
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
2017-11-01 17:35:14 +00:00
|
|
|
|
|
|
|
def parent
|
|
|
|
context[:project] || context[:group]
|
|
|
|
end
|
|
|
|
|
|
|
|
def parent_group?
|
|
|
|
parent.is_a?(Group)
|
|
|
|
end
|
|
|
|
|
|
|
|
def team_member?(user)
|
|
|
|
if parent_group?
|
|
|
|
parent.member?(user)
|
|
|
|
else
|
|
|
|
parent.team.member?(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parent_url(link_content, author)
|
|
|
|
if parent_group?
|
|
|
|
url = urls.group_url(parent, only_path: context[:only_path])
|
|
|
|
data = data_attribute(group: group.id, author: author.try(:id))
|
|
|
|
else
|
|
|
|
url = urls.project_url(parent, only_path: context[:only_path])
|
|
|
|
data = data_attribute(project: project.id, author: author.try(:id))
|
|
|
|
end
|
|
|
|
|
|
|
|
content = link_content || User.reference_prefix + 'all'
|
|
|
|
link_tag(url, data, content, 'All Project and Group Members')
|
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|