discussion.rb 3.4 KB
Newer Older
D
Douwe Maan 已提交
1
# A non-diff discussion on an issue, merge request, commit, or snippet, consisting of `DiscussionNote` notes.
2 3
#
# A discussion of this type can be resolvable.
4
class Discussion
5
  include ResolvableDiscussion
6

7
  attr_reader :notes, :context_noteable
8 9 10 11 12 13 14 15 16 17 18

  delegate  :created_at,
            :project,
            :author,

            :noteable,
            :for_commit?,
            :for_merge_request?,

            to: :first_note

19 20
  def self.build(notes, context_noteable = nil)
    notes.first.discussion_class(context_noteable).new(notes, context_noteable)
21
  end
22

23
  def self.build_collection(notes, context_noteable = nil)
D
Douwe Maan 已提交
24
    grouped_notes = notes.group_by { |n| n.discussion_id(context_noteable) }
25
    grouped_notes.values.map { |notes| build(notes, context_noteable) }
26
  end
27

D
Douwe Maan 已提交
28
  # Returns an alphanumeric discussion ID based on `build_discussion_id`
29 30
  def self.discussion_id(note)
    Digest::SHA1.hexdigest(build_discussion_id(note).join("-"))
31 32
  end

D
Douwe Maan 已提交
33 34 35
  # Returns an array of discussion ID components
  def self.build_discussion_id(note)
    [*base_discussion_id(note), SecureRandom.hex]
36 37
  end

D
Douwe Maan 已提交
38
  def self.base_discussion_id(note)
39 40
    noteable_id = note.noteable_id || note.commit_id
    [:discussion, note.noteable_type.try(:underscore), noteable_id]
41 42
  end

D
Douwe Maan 已提交
43 44 45 46
  # When notes on a commit are displayed in context of a merge request that contains that commit,
  # these notes are to be displayed as if they were part of one discussion, even though they were actually
  # individual notes on the commit with different discussion IDs, so that it's clear that these are not
  # notes on the merge request itself.
D
Douwe Maan 已提交
47 48 49
  #
  # To turn a list of notes into a list of discussions, they are grouped by discussion ID, so to
  # get these out-of-context notes to end up in the same discussion, we need to get them to return the same
D
Douwe Maan 已提交
50 51 52
  # `discussion_id` when this grouping happens. To enable this, `Note#discussion_id` calls out
  # to the `override_discussion_id` method on the appropriate `Discussion` subclass, as determined by
  # the `discussion_class` method on `Note` or a subclass of `Note`.
D
Douwe Maan 已提交
53
  #
D
Douwe Maan 已提交
54 55 56 57
  # If no override is necessary, return `nil`.
  # For the case described above, see `OutOfContextDiscussion.override_discussion_id`.
  def self.override_discussion_id(note)
    nil
58 59
  end

60 61 62 63
  def self.note_class
    DiscussionNote
  end

64
  def initialize(notes, context_noteable = nil)
65
    @notes = notes
66
    @context_noteable = context_noteable
67 68
  end

F
Felipe Artur 已提交
69 70 71 72
  def on_image?
    false
  end

D
Douwe Maan 已提交
73 74
  def ==(other)
    other.class == self.class &&
75
      other.context_noteable == self.context_noteable &&
D
Douwe Maan 已提交
76 77 78 79
      other.id == self.id &&
      other.notes == self.notes
  end

80 81 82 83 84 85 86 87
  def last_updated_at
    last_note.created_at
  end

  def last_updated_by
    last_note.author
  end

88 89 90 91
  def updated?
    last_updated_at != created_at
  end

92
  def id
93
    first_note.discussion_id(context_noteable)
94
  end
95

96 97 98 99 100 101
  def reply_id
    # To reply to this discussion, we need the actual discussion_id from the database,
    # not the potentially overwritten one based on the noteable.
    first_note.discussion_id
  end

D
Douwe Maan 已提交
102
  alias_method :to_param, :id
103 104

  def diff_discussion?
105 106 107
    false
  end

D
Douwe Maan 已提交
108
  def individual_note?
109
    false
110 111
  end

112 113 114 115
  def new_discussion?
    notes.length == 1
  end

116
  def last_note
117
    @last_note ||= notes.last
118
  end
119

D
Douwe Maan 已提交
120
  def collapsed?
D
Douwe Maan 已提交
121
    resolved?
D
Douwe Maan 已提交
122 123
  end

124
  def expanded?
D
Douwe Maan 已提交
125
    !collapsed?
126 127 128
  end

  def reply_attributes
D
Douwe Maan 已提交
129
    first_note.slice(:type, :noteable_type, :noteable_id, :commit_id, :discussion_id)
130 131
  end
end