BigW Consortium Gitlab

note.rb 8.51 KB
Newer Older
1 2 3 4 5 6 7 8
# == Schema Information
#
# Table name: notes
#
#  id            :integer          not null, primary key
#  note          :text
#  noteable_type :string(255)
#  author_id     :integer
Dmitriy Zaporozhets committed
9 10
#  created_at    :datetime
#  updated_at    :datetime
11 12 13
#  project_id    :integer
#  attachment    :string(255)
#  line_code     :string(255)
14 15
#  commit_id     :string(255)
#  noteable_id   :integer
Dmitriy Zaporozhets committed
16
#  system        :boolean          default(FALSE), not null
Dmitriy Zaporozhets committed
17
#  st_diff       :text
Stan Hu committed
18
#  updated_by_id :integer
19 20
#

gitlabhq committed
21 22 23 24
require 'carrierwave/orm/activerecord'
require 'file_size_validator'

class Note < ActiveRecord::Base
25
  include Mentionable
26
  include Gitlab::CurrentSettings
27
  include Participable
28

29 30
  default_value_for :system, false

31
  attr_mentionable :note
32
  participant :author, :mentioned_users
33

gitlabhq committed
34
  belongs_to :project
35
  belongs_to :noteable, polymorphic: true
36
  belongs_to :author, class_name: "User"
37
  belongs_to :updated_by, class_name: "User"
gitlabhq committed
38

39 40
  delegate :name, to: :project, prefix: true
  delegate :name, :email, to: :author, prefix: true
41

42
  validates :note, :project, presence: true
43
  validates :line_code, format: { with: /\A[a-z0-9]+_\d+_\d+\Z/ }, allow_blank: true
44 45
  # Attachments are deprecated and are handled by Markdown uploader
  validates :attachment, file_size: { maximum: :max_attachment_size }
gitlabhq committed
46

47 48 49
  validates :noteable_id, presence: true, if: ->(n) { n.noteable_type.present? && n.noteable_type != 'Commit' }
  validates :commit_id, presence: true, if: ->(n) { n.noteable_type == 'Commit' }

50
  mount_uploader :attachment, AttachmentUploader
Andrey Kumanyaev committed
51 52

  # Scopes
53
  scope :for_commit_id, ->(commit_id) { where(noteable_type: "Commit", commit_id: commit_id) }
54 55
  scope :inline, ->{ where("line_code IS NOT NULL") }
  scope :not_inline, ->{ where(line_code: [nil, '']) }
56
  scope :system, ->{ where(system: true) }
57
  scope :user, ->{ where(system: false) }
58
  scope :common, ->{ where(noteable_type: ["", nil]) }
59
  scope :fresh, ->{ order(created_at: :asc, id: :asc) }
60 61
  scope :inc_author_project, ->{ includes(:project, :author) }
  scope :inc_author, ->{ includes(:author) }
gitlabhq committed
62

63
  serialize :st_diff
64
  before_create :set_diff, if: ->(n) { n.line_code.present? }
65
  after_update :set_references
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  class << self
    def discussions_from_notes(notes)
      discussion_ids = []
      discussions = []

      notes.each do |note|
        next if discussion_ids.include?(note.discussion_id)

        # don't group notes for the main target
        if !note.for_diff_line? && note.noteable_type == "MergeRequest"
          discussions << [note]
        else
          discussions << notes.select do |other_note|
            note.discussion_id == other_note.discussion_id
          end
          discussion_ids << note.discussion_id
        end
      end

      discussions
    end
88

89 90 91 92
    def build_discussion_id(type, id, line_code)
      [:discussion, type.try(:underscore), id, line_code].join("-").to_sym
    end

93
    def search(query)
94
      where("LOWER(note) like :query", query: "%#{query.downcase}%")
95
    end
96
  end
97

98
  def cross_reference?
99
    system && SystemNoteService.cross_reference?(note)
100 101
  end

102 103 104 105
  def max_attachment_size
    current_application_settings.max_attachment_size.megabytes.to_i
  end

106
  def find_diff
107
    return nil unless noteable && noteable.diffs.present?
108 109 110

    @diff ||= noteable.diffs.find do |d|
      Digest::SHA1.hexdigest(d.new_path) == diff_file_index if d.new_path
111
    end
112 113
  end

114 115 116 117
  def hook_attrs
    attributes
  end

118 119 120
  def set_diff
    # First lets find notes with same diff
    # before iterating over all mr diffs
121
    diff = diff_for_line_code unless for_merge_request?
122 123 124 125 126 127 128 129 130
    diff ||= find_diff

    self.st_diff = diff.to_hash if diff
  end

  def diff
    @diff ||= Gitlab::Git::Diff.new(st_diff) if st_diff.respond_to?(:map)
  end

131 132 133 134
  def diff_for_line_code
    Note.where(noteable_id: noteable_id, noteable_type: noteable_type, line_code: line_code).last.try(:diff)
  end

135 136 137
  # Check if such line of code exists in merge request diff
  # If exists - its active discussion
  # If not - its outdated diff
138
  def active?
139
    return true unless self.diff
140
    return false unless noteable
141

142 143 144
    noteable.diffs.each do |mr_diff|
      next unless mr_diff.new_path == self.diff.new_path

145
      lines = Gitlab::Diff::Parser.new.parse(mr_diff.diff.lines.to_a)
146 147 148

      lines.each do |line|
        if line.text == diff_line
149 150 151 152 153 154 155 156 157 158
          return true
        end
      end
    end

    false
  end

  def outdated?
    !active?
159 160
  end

161
  def diff_file_index
162
    line_code.split('_')[0] if line_code
163 164 165
  end

  def diff_file_name
166
    diff.new_path if diff
167 168
  end

169 170 171 172 173 174 175 176
  def file_path
    if diff.new_path.present?
      diff.new_path
    elsif diff.old_path.present?
      diff.old_path
    end
  end

177
  def diff_old_line
178
    line_code.split('_')[1].to_i if line_code
179 180 181
  end

  def diff_new_line
182
    line_code.split('_')[2].to_i if line_code
183 184
  end

185 186 187 188
  def generate_line_code(line)
    Gitlab::Diff::LineCode.generate(file_path, line.new_pos, line.old_pos)
  end

189
  def diff_line
190 191
    return @diff_line if @diff_line

192
    if diff
193
      diff_lines.each do |line|
194 195 196
        if generate_line_code(line) == self.line_code
          @diff_line = line.text
        end
197
      end
198
    end
199 200

    @diff_line
201 202
  end

203 204 205 206 207 208 209 210 211 212 213 214 215 216
  def diff_line_type
    return @diff_line_type if @diff_line_type

    if diff
      diff_lines.each do |line|
        if generate_line_code(line) == self.line_code
          @diff_line_type = line.type
        end
      end
    end

    @diff_line_type
  end

217 218 219 220 221 222
  def truncated_diff_lines
    max_number_of_lines = 16
    prev_match_line = nil
    prev_lines = []

    diff_lines.each do |line|
223 224 225
      if line.type == "match"
        prev_lines.clear
        prev_match_line = line
226 227
      else
        prev_lines << line
228

229 230 231
        break if generate_line_code(line) == self.line_code

        prev_lines.shift if prev_lines.length >= max_number_of_lines
232 233
      end
    end
234 235

    prev_lines
236 237 238
  end

  def diff_lines
239
    @diff_lines ||= Gitlab::Diff::Parser.new.parse(diff.diff.lines.to_a)
240 241
  end

242
  def discussion_id
243
    @discussion_id ||= Note.build_discussion_id(noteable_type, noteable_id || commit_id, line_code)
244 245 246 247 248 249 250 251 252 253 254 255 256 257
  end

  def for_commit?
    noteable_type == "Commit"
  end

  def for_commit_diff_line?
    for_commit? && for_diff_line?
  end

  def for_diff_line?
    line_code.present?
  end

Riyad Preukschas committed
258 259 260 261
  def for_issue?
    noteable_type == "Issue"
  end

262 263 264 265 266 267
  def for_merge_request?
    noteable_type == "MergeRequest"
  end

  def for_merge_request_diff_line?
    for_merge_request? && for_diff_line?
268
  end
269

270 271 272 273
  def for_project_snippet?
    noteable_type == "Snippet"
  end

274 275 276
  # override to return commits, which are not active record
  def noteable
    if for_commit?
277
      project.commit(commit_id)
278
    else
279
      super
280
    end
281 282
  # Temp fix to prevent app crash
  # if note commit id doesn't exist
283
  rescue
284
    nil
285
  end
286

287 288 289 290 291 292 293 294 295 296
  DOWNVOTES = %w(-1 :-1: :thumbsdown: :thumbs_down_sign:)

  # Check if the note is a downvote
  def downvote?
    votable? && note.start_with?(*DOWNVOTES)
  end

  UPVOTES = %w(+1 :+1: :thumbsup: :thumbs_up_sign:)

  # Check if the note is an upvote
297
  def upvote?
298
    votable? && note.start_with?(*UPVOTES)
Riyad Preukschas committed
299 300
  end

301 302
  def superceded?(notes)
    return false unless vote?
303

304 305
    notes.each do |note|
      next if note == self
306

307
      if note.vote? &&
308 309
        self[:author_id] == note[:author_id] &&
        self[:created_at] <= note[:created_at]
310 311 312
        return true
      end
    end
313

314 315 316 317 318 319 320
    false
  end

  def vote?
    upvote? || downvote?
  end

Riyad Preukschas committed
321 322
  def votable?
    for_issue? || (for_merge_request? && !for_diff_line?)
323
  end
324

325
  # Mentionable override.
326 327
  def gfm_reference(from_project = nil)
    noteable.gfm_reference(from_project)
328 329 330 331 332 333 334
  end

  # Mentionable override.
  def local_reference
    noteable
  end

335 336 337 338 339
  def noteable_type_name
    if noteable_type.present?
      noteable_type.downcase
    end
  end
Andrew8xx8 committed
340 341

  # FIXME: Hack for polymorphic associations with STI
Steven Burgart committed
342
  #        For more information visit http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Polymorphic+Associations
Andrew8xx8 committed
343 344 345
  def noteable_type=(sType)
    super(sType.to_s.classify.constantize.base_class.to_s)
  end
346 347 348 349 350 351 352 353 354 355 356

  # Reset notes events cache
  #
  # Since we do cache @event we need to reset cache in special cases:
  # * when a note is updated
  # * when a note is removed
  # Events cache stored like  events/23-20130109142513.
  # The cache key includes updated_at timestamp.
  # Thus it will automatically generate a new fragment
  # when the event is updated because the key changes.
  def reset_events_cache
357
    Event.reset_event_cache_for(self)
358
  end
359 360

  def set_references
361
    create_new_cross_references!(project, author)
362
  end
363

364 365 366 367
  def system?
    read_attribute(:system)
  end

368
  def editable?
369
    !read_attribute(:system)
370
  end
gitlabhq committed
371
end