2014-09-02 18:07:02 +05:30
|
|
|
# == Mentionable concern
|
|
|
|
#
|
|
|
|
# Contains functionality related to objects that can mention Users, Issues, MergeRequests, or Commits by
|
|
|
|
# GFM references.
|
|
|
|
#
|
|
|
|
# Used by Issue, Note, MergeRequest, and Commit.
|
|
|
|
#
|
|
|
|
module Mentionable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
# Indicate which attributes of the Mentionable to search for GFM references.
|
2015-12-23 02:04:40 +05:30
|
|
|
def attr_mentionable(attr, options = {})
|
|
|
|
attr = attr.to_s
|
|
|
|
mentionable_attrs << [attr, options]
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
included do
|
2014-09-02 18:07:02 +05:30
|
|
|
# Accessor for attributes marked mentionable.
|
2016-08-24 12:49:21 +05:30
|
|
|
cattr_accessor :mentionable_attrs, instance_accessor: false do
|
|
|
|
[]
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
if self < Participable
|
2016-06-16 23:09:34 +05:30
|
|
|
participant -> (user, ext) { all_references(user, extractor: ext) }
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
# 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.
|
|
|
|
def gfm_reference(from_project = nil)
|
|
|
|
# "MergeRequest" > "merge_request" > "Merge request" > "merge request"
|
|
|
|
friendly_name = self.class.to_s.underscore.humanize.downcase
|
|
|
|
|
|
|
|
"#{friendly_name} #{to_reference(from_project)}"
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# The GFM reference to this Mentionable, which shouldn't be included in its #references.
|
|
|
|
def local_reference
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def all_references(current_user = nil, text = nil, extractor: nil)
|
|
|
|
extractor ||= Gitlab::ReferenceExtractor.
|
2016-08-24 12:49:21 +05:30
|
|
|
new(project, current_user)
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
if text
|
2016-06-16 23:09:34 +05:30
|
|
|
extractor.analyze(text, author: author)
|
2015-12-23 02:04:40 +05:30
|
|
|
else
|
|
|
|
self.class.mentionable_attrs.each do |attr, options|
|
2016-06-16 23:09:34 +05:30
|
|
|
text = __send__(attr)
|
|
|
|
options = options.merge(cache_key: [self, attr], author: author)
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
extractor.analyze(text, options)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
extractor
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
def mentioned_users(current_user = nil)
|
|
|
|
all_references(current_user).users
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Extract GFM references to other Mentionables from this Mentionable. Always excludes its #local_reference.
|
2015-12-23 02:04:40 +05:30
|
|
|
def referenced_mentionables(current_user = self.author, text = nil)
|
|
|
|
refs = all_references(current_user, text)
|
|
|
|
refs = (refs.issues + refs.merge_requests + refs.commits)
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
# 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.
|
|
|
|
refs.reject { |ref| ref == local_reference }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
# Create a cross-reference Note for each GFM reference to another Mentionable found in the +mentionable_attrs+.
|
|
|
|
def create_cross_references!(author = self.author, without = [], text = nil)
|
2015-10-24 18:46:33 +05:30
|
|
|
refs = referenced_mentionables(author, text)
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
# 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-24 18:46:33 +05:30
|
|
|
refs.reject! { |ref| without.include?(ref) || cross_reference_exists?(ref) }
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
refs.each do |ref|
|
2015-10-24 18:46:33 +05:30
|
|
|
SystemNoteService.cross_reference(ref, local_reference, author)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
# When a mentionable field is changed, creates cross-reference notes that
|
|
|
|
# don't already exist
|
2015-10-24 18:46:33 +05:30
|
|
|
def create_new_cross_references!(author = self.author)
|
2015-09-11 14:41:01 +05:30
|
|
|
changes = detect_mentionable_changes
|
|
|
|
|
|
|
|
return if changes.empty?
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
original_text = changes.collect { |_, vals| vals.first }.join(' ')
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
preexisting = referenced_mentionables(author, original_text)
|
|
|
|
create_cross_references!(author, preexisting)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
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-12-23 02:04:40 +05:30
|
|
|
mentionable = self.class.mentionable_attrs.map { |attr, options| attr }
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
# Only include changed fields that are mentionable
|
|
|
|
source.select { |key, val| mentionable.include?(key) }
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
# 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
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|