2013-05-09 19:37:47 -04:00
|
|
|
# == Mentionable concern
|
|
|
|
#
|
2013-05-30 19:16:49 -04:00
|
|
|
# Contains functionality related to objects that can mention Users, Issues, MergeRequests, or Commits by
|
|
|
|
# GFM references.
|
2013-05-09 19:37:47 -04:00
|
|
|
#
|
2013-05-30 19:16:49 -04:00
|
|
|
# Used by Issue, Note, MergeRequest, and Commit.
|
2013-05-09 19:37:47 -04:00
|
|
|
#
|
|
|
|
module Mentionable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
module ClassMethods
|
|
|
|
# Indicate which attributes of the Mentionable to search for GFM references.
|
2014-09-25 18:07:40 -04:00
|
|
|
def attr_mentionable(*attrs)
|
2013-05-30 19:16:49 -04:00
|
|
|
mentionable_attrs.concat(attrs.map(&:to_s))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Accessor for attributes marked mentionable.
|
|
|
|
def mentionable_attrs
|
|
|
|
@mentionable_attrs ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Generate a GFM back-reference that will construct a link back to this Mentionable when rendered. Must
|
|
|
|
# be overridden if this model object can be referenced directly by GFM notation.
|
|
|
|
def gfm_reference
|
|
|
|
raise NotImplementedError.new("#{self.class} does not implement #gfm_reference")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Construct a String that contains possible GFM references.
|
|
|
|
def mentionable_text
|
|
|
|
self.class.mentionable_attrs.map { |attr| send(attr) || '' }.join
|
|
|
|
end
|
|
|
|
|
|
|
|
# The GFM reference to this Mentionable, which shouldn't be included in its #references.
|
|
|
|
def local_reference
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# Determine whether or not a cross-reference Note has already been created between this Mentionable and
|
|
|
|
# the specified target.
|
2014-09-25 18:07:40 -04:00
|
|
|
def has_mentioned?(target)
|
2013-05-30 19:16:49 -04:00
|
|
|
Note.cross_reference_exists?(target, local_reference)
|
|
|
|
end
|
|
|
|
|
2013-05-09 19:37:47 -04:00
|
|
|
def mentioned_users
|
|
|
|
users = []
|
|
|
|
return users if mentionable_text.blank?
|
|
|
|
has_project = self.respond_to? :project
|
|
|
|
matches = mentionable_text.scan(/@[a-zA-Z][a-zA-Z0-9_\-\.]*/)
|
|
|
|
matches.each do |match|
|
|
|
|
identifier = match.delete "@"
|
2014-06-23 06:28:57 -04:00
|
|
|
if identifier == "all"
|
2014-10-09 03:47:47 -04:00
|
|
|
users.push(*project.team.members.flatten)
|
2015-02-04 11:10:39 -05:00
|
|
|
elsif namespace = Namespace.find_by(path: identifier)
|
|
|
|
if namespace.type == "Group"
|
|
|
|
users.push(*namespace.users)
|
|
|
|
else
|
|
|
|
users << namespace.owner
|
|
|
|
end
|
2013-05-09 19:37:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
users.uniq
|
|
|
|
end
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
# Extract GFM references to other Mentionables from this Mentionable. Always excludes its #local_reference.
|
2014-09-25 18:07:40 -04:00
|
|
|
def references(p = project, text = mentionable_text)
|
2013-05-30 19:16:49 -04:00
|
|
|
return [] if text.blank?
|
|
|
|
ext = Gitlab::ReferenceExtractor.new
|
2014-10-02 14:26:39 -04:00
|
|
|
ext.analyze(text, p)
|
2015-02-02 18:11:19 -05:00
|
|
|
|
|
|
|
(ext.issues_for(p) +
|
|
|
|
ext.merge_requests_for(p) +
|
|
|
|
ext.commits_for(p)).uniq - [local_reference]
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Create a cross-reference Note for each GFM reference to another Mentionable found in +mentionable_text+.
|
2014-09-25 18:07:40 -04:00
|
|
|
def create_cross_references!(p = project, a = author, without = [])
|
2013-05-30 19:16:49 -04:00
|
|
|
refs = references(p) - without
|
|
|
|
refs.each do |ref|
|
|
|
|
Note.create_cross_reference_note(ref, local_reference, a, p)
|
2013-05-09 19:37:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
# If the mentionable_text field is about to change, locate any *added* references and create cross references for
|
|
|
|
# them. Invoke from an observer's #before_save implementation.
|
2014-09-25 18:07:40 -04:00
|
|
|
def notice_added_references(p = project, a = author)
|
2013-05-30 19:16:49 -04:00
|
|
|
ch = changed_attributes
|
|
|
|
original, mentionable_changed = "", false
|
|
|
|
self.class.mentionable_attrs.each do |attr|
|
|
|
|
if ch[attr]
|
|
|
|
original << ch[attr]
|
|
|
|
mentionable_changed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Only proceed if the saved changes actually include a chance to an attr_mentionable field.
|
|
|
|
return unless mentionable_changed
|
|
|
|
|
|
|
|
preexisting = references(p, original)
|
|
|
|
create_cross_references!(p, a, preexisting)
|
|
|
|
end
|
2013-05-09 19:37:47 -04:00
|
|
|
end
|