discussion.rb 2.7 KB
Newer Older
1 2 3
class Discussion
  NUMBER_OF_TRUNCATED_DIFF_LINES = 16

D
Douwe Maan 已提交
4
  attr_reader :first_note, :last_note, :notes
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

  delegate  :created_at,
            :project,
            :author,

            :noteable,
            :for_commit?,
            :for_merge_request?,

            :line_code,
            :diff_file,
            :for_line?,
            :active?,

            to: :first_note

D
Douwe Maan 已提交
21 22 23 24 25
  delegate  :resolved_at,
            :resolved_by,

            to: :last_note

26 27 28 29 30 31 32 33 34 35 36 37
  delegate :blob, :highlighted_diff_lines, to: :diff_file, allow_nil: true

  def self.for_notes(notes)
    notes.group_by(&:discussion_id).values.map { |notes| new(notes) }
  end

  def self.for_diff_notes(notes)
    notes.group_by(&:line_code).values.map { |notes| new(notes) }
  end

  def initialize(notes)
    @first_note = notes.first
D
Douwe Maan 已提交
38
    @last_note = notes.last
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    @notes = notes
  end

  def id
    first_note.discussion_id
  end

  def diff_discussion?
    first_note.diff_note?
  end

  def legacy_diff_discussion?
    notes.any?(&:legacy_diff_note?)
  end

D
Douwe Maan 已提交
54 55 56 57 58 59 60 61 62 63 64 65
  def resolvable?
    diff_discussion? && notes.any?(&:resolvable?)
  end

  def resolved?
    notes.none?(&:to_be_resolved?)
  end

  def to_be_resolved?
    notes.any?(&:to_be_resolved?)
  end

66 67 68 69 70 71 72 73
  def can_resolve?(current_user)
    return false unless current_user
    return false unless resolvable?

    current_user == self.noteable.author ||
      can?(current_user, :push_code, self.project)
  end

74 75 76 77 78 79 80 81 82 83 84 85
  def resolve!(current_user)
    notes.each do |note|
      note.resolve!(current_user) if note.resolvable?
    end
  end

  def unresolve!
    notes.each do |note|
      note.unresolve! if note.resolvable?
    end
  end

86 87 88 89
  def for_target?(target)
    self.noteable == target && !diff_discussion?
  end

D
Douwe Maan 已提交
90 91 92 93 94 95 96 97 98 99 100 101
  def collapsed?
    return false unless diff_discussion?

    if resolvable?
      # New diff discussions only disappear once they are marked resolved
      resolved?
    else
      # Old diff discussions disappear once they become outdated
      !active?
    end
  end

102
  def expanded?
D
Douwe Maan 已提交
103
    !collapsed?
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  end

  def reply_attributes
    data = {
      noteable_type: first_note.noteable_type,
      noteable_id:   first_note.noteable_id,
      commit_id:     first_note.commit_id,
      discussion_id: self.id,
    }

    if diff_discussion?
      data[:note_type] = first_note.type

      data.merge!(first_note.diff_attributes)
    end

    data
  end

  # Returns an array of at most 16 highlighted lines above a diff note
  def truncated_diff_lines
    prev_lines = []

    highlighted_diff_lines.each do |line|
      if line.meta?
        prev_lines.clear
      else
        prev_lines << line

        break if for_line?(line)

        prev_lines.shift if prev_lines.length >= NUMBER_OF_TRUNCATED_DIFF_LINES
      end
    end

    prev_lines
  end
end