2015-04-03 00:46:43 +00:00
|
|
|
module Gitlab
|
|
|
|
module Markdown
|
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
|
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)
|
|
|
|
text.gsub(USER_PATTERN) do |match|
|
|
|
|
yield match, $~[:user]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Pattern used to extract `@user` user references from text
|
|
|
|
USER_PATTERN = /@(?<user>#{Gitlab::Regex::NAMESPACE_REGEX_STR})/
|
|
|
|
|
|
|
|
def call
|
2015-04-14 22:34:01 +00:00
|
|
|
replace_text_nodes_matching(USER_PATTERN) do |content|
|
|
|
|
user_link_filter(content)
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Replace `@user` user references in text with links to the referenced
|
|
|
|
# user's profile page.
|
|
|
|
#
|
|
|
|
# text - String text to replace references in.
|
|
|
|
#
|
|
|
|
# Returns a String with `@user` references replaced with links. All links
|
|
|
|
# have `gfm` and `gfm-project_member` class names attached for styling.
|
|
|
|
def user_link_filter(text)
|
|
|
|
project = context[:project]
|
|
|
|
|
|
|
|
self.class.references_in(text) do |match, user|
|
2015-04-14 22:34:01 +00:00
|
|
|
klass = reference_class(:project_member)
|
2015-04-03 00:46:43 +00:00
|
|
|
|
|
|
|
if user == 'all'
|
2015-04-15 19:56:45 +00:00
|
|
|
# FIXME (rspeicher): Law of Demeter
|
2015-04-25 20:46:06 +00:00
|
|
|
push_result(:user, *project.team.members.flatten)
|
2015-04-15 19:56:45 +00:00
|
|
|
|
2015-04-03 00:46:43 +00:00
|
|
|
url = link_to_all(project)
|
|
|
|
|
|
|
|
%(<a href="#{url}" class="#{klass}">@#{user}</a>)
|
|
|
|
elsif namespace = Namespace.find_by(path: user)
|
|
|
|
if namespace.is_a?(Group)
|
2015-04-14 20:31:40 +00:00
|
|
|
if user_can_reference_group?(namespace)
|
2015-04-25 20:46:06 +00:00
|
|
|
push_result(:user, *namespace.users)
|
2015-04-15 19:56:45 +00:00
|
|
|
|
2015-04-13 19:43:58 +00:00
|
|
|
url = group_url(user, only_path: context[:only_path])
|
|
|
|
%(<a href="#{url}" class="#{klass}">@#{user}</a>)
|
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
else
|
2015-04-15 19:56:45 +00:00
|
|
|
push_result(:user, namespace.owner)
|
|
|
|
|
2015-04-03 00:46:43 +00:00
|
|
|
url = user_url(user, only_path: context[:only_path])
|
2015-04-13 19:43:58 +00:00
|
|
|
%(<a href="#{url}" class="#{klass}">@#{user}</a>)
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-14 22:34:01 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def urls
|
|
|
|
Rails.application.routes.url_helpers
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def group_url(*args)
|
2015-04-14 22:34:01 +00:00
|
|
|
urls.group_url(*args)
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_url(*args)
|
2015-04-14 22:34:01 +00:00
|
|
|
urls.user_url(*args)
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_to_all(project)
|
2015-04-14 22:34:01 +00:00
|
|
|
urls.namespace_project_url(project.namespace, project,
|
|
|
|
only_path: context[:only_path])
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
2015-04-13 19:43:58 +00:00
|
|
|
|
2015-04-14 20:31:40 +00:00
|
|
|
def user_can_reference_group?(group)
|
2015-04-13 19:43:58 +00:00
|
|
|
Ability.abilities.allowed?(context[:current_user], :read_group, group)
|
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|