discussion.rb 3.1 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 24
  def self.build_collection(notes, context_noteable = nil)
    notes.group_by { |n| n.discussion_id(context_noteable) }.values.map { |notes| build(notes, context_noteable) }
25
  end
26

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

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

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

D
Douwe Maan 已提交
42 43 44 45
  # 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 已提交
46 47 48
  #
  # 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 已提交
49 50 51
  # `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 已提交
52
  #
D
Douwe Maan 已提交
53 54 55 56
  # If no override is necessary, return `nil`.
  # For the case described above, see `OutOfContextDiscussion.override_discussion_id`.
  def self.override_discussion_id(note)
    nil
57 58
  end

59 60 61 62
  def self.note_class
    DiscussionNote
  end

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

D
Douwe Maan 已提交
68 69
  def ==(other)
    other.class == self.class &&
70
      other.context_noteable == self.context_noteable &&
D
Douwe Maan 已提交
71 72 73 74
      other.id == self.id &&
      other.notes == self.notes
  end

75 76 77 78 79 80 81 82
  def last_updated_at
    last_note.created_at
  end

  def last_updated_by
    last_note.author
  end

83
  def id
84
    first_note.discussion_id(context_noteable)
85
  end
86

D
Douwe Maan 已提交
87
  alias_method :to_param, :id
88 89

  def diff_discussion?
90 91 92
    false
  end

D
Douwe Maan 已提交
93
  def individual_note?
94
    false
95 96
  end

97 98 99 100
  def new_discussion?
    notes.length == 1
  end

101
  def last_note
102
    @last_note ||= notes.last
103
  end
104

D
Douwe Maan 已提交
105
  def collapsed?
D
Douwe Maan 已提交
106
    resolved?
D
Douwe Maan 已提交
107 108
  end

109
  def expanded?
D
Douwe Maan 已提交
110
    !collapsed?
111 112 113
  end

  def reply_attributes
D
Douwe Maan 已提交
114
    first_note.slice(:type, :noteable_type, :noteable_id, :commit_id, :discussion_id)
115 116
  end
end