BigW Consortium Gitlab

mentionable.rb 4.18 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
Yorick Peterse committed
26
      participant -> (user, ext) { all_references(user, extractor: ext) }
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

Yorick Peterse committed
46 47 48
  def all_references(current_user = nil, text = nil, extractor: nil)
    extractor ||= Gitlab::ReferenceExtractor.
      new(project, current_user || author)
49

50
    if text
Yorick Peterse committed
51
      extractor.analyze(text, author: author)
52 53
    else
      self.class.mentionable_attrs.each do |attr, options|
Yorick Peterse committed
54 55
        text = __send__(attr)
        options = options.merge(cache_key: [self, attr], author: author)
56

Yorick Peterse committed
57
        extractor.analyze(text, options)
58 59 60
      end
    end

Yorick Peterse committed
61
    extractor
62 63
  end

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

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

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

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

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

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

    return if changes.empty?
99

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

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

  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

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

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

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