mentionable.rb 4.17 KB
Newer Older
1 2
# == Mentionable concern
#
3 4
# Contains functionality related to objects that can mention Users, Issues, MergeRequests, or Commits by
# GFM references.
5
#
6
# Used by Issue, Note, MergeRequest, and Commit.
7 8 9 10
#
module Mentionable
  extend ActiveSupport::Concern

11 12
  module ClassMethods
    # Indicate which attributes of the Mentionable to search for GFM references.
13 14 15
    def attr_mentionable(attr, options = {})
      attr = attr.to_s
      mentionable_attrs << [attr, options]
16 17 18 19 20 21 22 23
    end

    # Accessor for attributes marked mentionable.
    def mentionable_attrs
      @mentionable_attrs ||= []
    end
  end

24 25
  included do
    if self < Participable
26
      participant ->(current_user) { mentioned_users(current_user) }
27 28 29
    end
  end

30 31 32 33
  # 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.
34
  def gfm_reference(from_project = nil)
35
    # "MergeRequest" > "merge_request" > "Merge request" > "merge request"
36 37
    friendly_name = self.class.to_s.underscore.humanize.downcase

38
    "#{friendly_name} #{to_reference(from_project)}"
39 40 41 42 43 44 45
  end

  # The GFM reference to this Mentionable, which shouldn't be included in its #references.
  def local_reference
    self
  end

46
  def all_references(current_user = self.author, text = nil)
47
    ext = Gitlab::ReferenceExtractor.new(self.project, current_user, self.author)
48

49 50 51 52 53
    if text
      ext.analyze(text)
    else
      self.class.mentionable_attrs.each do |attr, options|
        text = send(attr)
54 55 56 57 58

        context = options.dup
        context[:cache_key] = [self, attr] if context.delete(:cache) && self.persisted?

        ext.analyze(text, context)
59 60 61
      end
    end

62
    ext
63 64
  end

65 66
  def mentioned_users(current_user = nil)
    all_references(current_user).users
67 68
  end

69
  # Extract GFM references to other Mentionables from this Mentionable. Always excludes its #local_reference.
70 71
  def referenced_mentionables(current_user = self.author, text = nil)
    refs = all_references(current_user, text)
Douwe Maan's avatar
Douwe Maan committed
72 73 74 75 76
    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.
77
    refs.reject { |ref| ref == local_reference }
78
  end
79

80 81
  # 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)
82
    refs = referenced_mentionables(author, text)
Douwe Maan's avatar
Douwe Maan committed
83

84 85 86
    # 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.
87
    refs.reject! { |ref| without.include?(ref) || cross_reference_exists?(ref) }
88

89
    refs.each do |ref|
90
      SystemNoteService.cross_reference(ref, local_reference, author)
91 92 93
    end
  end

94 95
  # When a mentionable field is changed, creates cross-reference notes that
  # don't already exist
96
  def create_new_cross_references!(author = self.author)
97 98 99
    changes = detect_mentionable_changes

    return if changes.empty?
100

101
    original_text = changes.collect { |_, vals| vals.first }.join(' ')
102

103 104
    preexisting = referenced_mentionables(author, original_text)
    create_cross_references!(author, preexisting)
105
  end
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

  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

121
    mentionable = self.class.mentionable_attrs.map { |attr, options| attr }
122 123 124 125

    # Only include changed fields that are mentionable
    source.select { |key, val| mentionable.include?(key) }
  end
Douwe Maan's avatar
Douwe Maan committed
126

127 128 129 130 131
  # 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
132
end