2013-05-09 19:37:47 -04:00
|
|
|
# == Mentionable concern
|
|
|
|
#
|
2016-11-17 15:46:31 -05:00
|
|
|
# Contains functionality related to objects that can mention Users, Issues, MergeRequests, Commits or Snippets by
|
2013-05-30 19:16:49 -04:00
|
|
|
# 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.
|
2015-10-14 15:29:35 -04:00
|
|
|
def attr_mentionable(attr, options = {})
|
|
|
|
attr = attr.to_s
|
|
|
|
mentionable_attrs << [attr, options]
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
2016-07-13 23:28:58 -04:00
|
|
|
end
|
2013-05-30 19:16:49 -04:00
|
|
|
|
2016-07-13 23:28:58 -04:00
|
|
|
included do
|
2013-05-30 19:16:49 -04:00
|
|
|
# Accessor for attributes marked mentionable.
|
2016-07-13 23:28:58 -04:00
|
|
|
cattr_accessor :mentionable_attrs, instance_accessor: false do
|
|
|
|
[]
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-10-14 10:20:11 -04:00
|
|
|
if self < Participable
|
2016-05-26 07:34:06 -04:00
|
|
|
participant -> (user, ext) { all_references(user, extractor: ext) }
|
2015-10-14 10:20:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-11 15:56:00 -04:00
|
|
|
# Returns the text used as the body of a Note when this object is referenced
|
|
|
|
#
|
|
|
|
# By default this will be the class name and the result of calling
|
|
|
|
# `to_reference` on the object.
|
2015-05-26 16:30:07 -04:00
|
|
|
def gfm_reference(from_project = nil)
|
2015-05-14 17:09:02 -04:00
|
|
|
# "MergeRequest" > "merge_request" > "Merge request" > "merge request"
|
2015-05-11 15:56:00 -04:00
|
|
|
friendly_name = self.class.to_s.underscore.humanize.downcase
|
|
|
|
|
2015-05-26 16:30:07 -04:00
|
|
|
"#{friendly_name} #{to_reference(from_project)}"
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# The GFM reference to this Mentionable, which shouldn't be included in its #references.
|
|
|
|
def local_reference
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2016-09-29 10:28:45 -04:00
|
|
|
def all_references(current_user = nil, extractor: nil)
|
2016-05-26 07:34:06 -04:00
|
|
|
extractor ||= Gitlab::ReferenceExtractor.
|
2016-06-27 08:24:08 -04:00
|
|
|
new(project, current_user)
|
2015-12-15 09:51:16 -05:00
|
|
|
|
2016-09-29 10:28:45 -04:00
|
|
|
self.class.mentionable_attrs.each do |attr, options|
|
|
|
|
text = __send__(attr)
|
|
|
|
options = options.merge(cache_key: [self, attr], author: author)
|
2016-01-06 07:33:11 -05:00
|
|
|
|
2016-09-29 10:28:45 -04:00
|
|
|
extractor.analyze(text, options)
|
2015-10-14 15:29:35 -04:00
|
|
|
end
|
|
|
|
|
2016-05-26 07:34:06 -04:00
|
|
|
extractor
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-12-15 09:51:16 -05:00
|
|
|
def mentioned_users(current_user = nil)
|
|
|
|
all_references(current_user).users
|
2013-05-09 19:37:47 -04:00
|
|
|
end
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
# Extract GFM references to other Mentionables from this Mentionable. Always excludes its #local_reference.
|
2016-09-29 10:28:45 -04:00
|
|
|
def referenced_mentionables(current_user = self.author)
|
|
|
|
refs = all_references(current_user)
|
2015-11-30 16:43:54 -05:00
|
|
|
refs = (refs.issues + refs.merge_requests + refs.commits)
|
|
|
|
|
|
|
|
# We're using this method instead of Array diffing because that requires
|
|
|
|
# both of the object's `hash` values to be the same, which may not be the
|
|
|
|
# case for otherwise identical Commit objects.
|
2015-12-01 10:15:53 -05:00
|
|
|
refs.reject { |ref| ref == local_reference }
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
2015-06-08 18:13:14 -04:00
|
|
|
|
2015-10-14 16:42:59 -04:00
|
|
|
# Create a cross-reference Note for each GFM reference to another Mentionable found in the +mentionable_attrs+.
|
2016-09-29 10:28:45 -04:00
|
|
|
def create_cross_references!(author = self.author, without = [])
|
|
|
|
refs = referenced_mentionables(author)
|
2015-11-30 16:43:54 -05:00
|
|
|
|
2015-06-08 18:13:14 -04:00
|
|
|
# We're using this method instead of Array diffing because that requires
|
|
|
|
# both of the object's `hash` values to be the same, which may not be the
|
|
|
|
# case for otherwise identical Commit objects.
|
2015-10-12 05:54:46 -04:00
|
|
|
refs.reject! { |ref| without.include?(ref) || cross_reference_exists?(ref) }
|
2015-06-08 18:13:14 -04:00
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
refs.each do |ref|
|
2015-10-12 05:54:46 -04:00
|
|
|
SystemNoteService.cross_reference(ref, local_reference, author)
|
2013-05-09 19:37:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-13 18:33:50 -04:00
|
|
|
# When a mentionable field is changed, creates cross-reference notes that
|
|
|
|
# don't already exist
|
2015-10-12 05:54:46 -04:00
|
|
|
def create_new_cross_references!(author = self.author)
|
2015-07-13 18:28:10 -04:00
|
|
|
changes = detect_mentionable_changes
|
|
|
|
|
|
|
|
return if changes.empty?
|
2013-05-30 19:16:49 -04:00
|
|
|
|
2016-09-29 10:28:45 -04:00
|
|
|
create_cross_references!(author)
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
2015-07-13 18:28:10 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Returns a Hash of changed mentionable fields
|
|
|
|
#
|
|
|
|
# Preference is given to the `changes` Hash, but falls back to
|
|
|
|
# `previous_changes` if it's empty (i.e., the changes have already been
|
|
|
|
# persisted).
|
|
|
|
#
|
|
|
|
# See ActiveModel::Dirty.
|
|
|
|
#
|
|
|
|
# Returns a Hash.
|
|
|
|
def detect_mentionable_changes
|
|
|
|
source = (changes.present? ? changes : previous_changes).dup
|
|
|
|
|
2015-10-14 15:29:35 -04:00
|
|
|
mentionable = self.class.mentionable_attrs.map { |attr, options| attr }
|
2015-07-13 18:28:10 -04:00
|
|
|
|
|
|
|
# Only include changed fields that are mentionable
|
|
|
|
source.select { |key, val| mentionable.include?(key) }
|
|
|
|
end
|
2015-11-30 16:43:54 -05:00
|
|
|
|
2015-10-14 03:27:30 -04:00
|
|
|
# Determine whether or not a cross-reference Note has already been created between this Mentionable and
|
|
|
|
# the specified target.
|
|
|
|
def cross_reference_exists?(target)
|
|
|
|
SystemNoteService.cross_reference_exists?(target, local_reference)
|
|
|
|
end
|
2013-05-09 19:37:47 -04:00
|
|
|
end
|