BigW Consortium Gitlab

note.rb 8.89 KB
Newer Older
1 2 3
# A note on the root of an issue, merge request, commit, or snippet.
#
# A note of this type is never resolvable.
gitlabhq committed
4
class Note < ActiveRecord::Base
5
  extend ActiveModel::Naming
6
  include Gitlab::CurrentSettings
7
  include Participable
8
  include Mentionable
9
  include Awardable
10
  include Importable
11
  include FasterCacheKeys
12
  include CacheMarkdownField
13
  include AfterCommitQueue
14
  include ResolvableNote
15 16 17
  include IgnorableColumn

  ignore_column :original_discussion_id
18 19

  cache_markdown_field :note, pipeline: :note
20

21 22
  # Attribute containing rendered and redacted Markdown as generated by
  # Banzai::ObjectRenderer.
23
  attr_accessor :redacted_note_html
24

25 26 27 28
  # An Array containing the number of visible references as generated by
  # Banzai::ObjectRenderer
  attr_accessor :user_visible_reference_count

29 30 31
  # Attribute used to store the attributes that have ben changed by slash commands.
  attr_accessor :commands_changes

32 33
  default_value_for :system, false

Yorick Peterse committed
34
  attr_mentionable :note, pipeline: :note
35
  participant :author
36

gitlabhq committed
37
  belongs_to :project
38
  belongs_to :noteable, polymorphic: true, touch: true
39
  belongs_to :author, class_name: "User"
40
  belongs_to :updated_by, class_name: "User"
gitlabhq committed
41

42
  has_many :todos, dependent: :destroy
43
  has_many :events, as: :target, dependent: :destroy
44
  has_one :system_note_metadata
45

46
  delegate :gfm_reference, :local_reference, to: :noteable
47 48
  delegate :name, to: :project, prefix: true
  delegate :name, :email, to: :author, prefix: true
49
  delegate :title, to: :noteable, allow_nil: true
50

51 52
  validates :note, presence: true
  validates :project, presence: true, unless: :for_personal_snippet?
53

54 55
  # Attachments are deprecated and are handled by Markdown uploader
  validates :attachment, file_size: { maximum: :max_attachment_size }
gitlabhq committed
56

57
  validates :noteable_type, presence: true
58
  validates :noteable_id, presence: true, unless: [:for_commit?, :importing?]
59
  validates :commit_id, presence: true, if: :for_commit?
Valery Sizov committed
60
  validates :author, presence: true
61
  validates :discussion_id, presence: true, format: { with: /\A\h{40}\z/ }
62

63
  validate unless: [:for_commit?, :importing?, :for_personal_snippet?] do |note|
64
    unless note.noteable.try(:project) == note.project
Douwe Maan committed
65
      errors.add(:project, 'does not match noteable project')
66 67 68
    end
  end

69
  mount_uploader :attachment, AttachmentUploader
Andrey Kumanyaev committed
70 71

  # Scopes
72
  scope :for_commit_id, ->(commit_id) { where(noteable_type: "Commit", commit_id: commit_id) }
73
  scope :system, ->{ where(system: true) }
74
  scope :user, ->{ where(system: false) }
75
  scope :common, ->{ where(noteable_type: ["", nil]) }
76
  scope :fresh, ->{ order(created_at: :asc, id: :asc) }
Douwe Maan committed
77
  scope :updated_after, ->(time){ where('updated_at > ?', time) }
78 79
  scope :inc_author_project, ->{ includes(:project, :author) }
  scope :inc_author, ->{ includes(:author) }
80 81 82
  scope :inc_relations_for_view, -> do
    includes(:project, :author, :updated_by, :resolved_by, :award_emoji, :system_note_metadata)
  end
gitlabhq committed
83

Douwe Maan committed
84
  scope :diff_notes, ->{ where(type: %w(LegacyDiffNote DiffNote)) }
85
  scope :new_diff_notes, ->{ where(type: 'DiffNote') }
86
  scope :non_diff_notes, ->{ where(type: ['Note', 'DiscussionNote', nil]) }
87

88
  scope :with_associations, -> do
89 90
    # FYI noteable cannot be loaded for LegacyDiffNote for commits
    includes(:author, :noteable, :updated_by,
91
             project: [:project_members, { group: [:group_members] }])
92
  end
gitlabhq committed
93

94
  after_initialize :ensure_discussion_id
95
  before_validation :nullify_blank_type, :nullify_blank_line_code
96
  before_validation :set_discussion_id, on: :create
97
  after_save :keep_around_commit, unless: :for_personal_snippet?
98
  after_save :expire_etag_cache
99

100
  class << self
101 102 103
    def model_name
      ActiveModel::Name.new(self, nil, 'note')
    end
104

105 106
    def discussions(noteable = nil)
      Discussion.build_collection(fresh, noteable)
107
    end
108

109 110 111 112 113
    def find_discussion(discussion_id)
      notes = where(discussion_id: discussion_id).fresh.to_a
      return if notes.empty?

      Discussion.build(notes)
114
    end
115

116
    def grouped_diff_discussions
117 118
      diff_notes.
        fresh.
119
        discussions.
120
        select(&:active?).
121
        group_by(&:line_code)
122
    end
123 124

    def count_for_collection(ids, type)
125 126 127
      user.select('noteable_id', 'COUNT(*) as count').
        group(:noteable_id).
        where(noteable_type: type, noteable_id: ids)
128
    end
129
  end
130

131
  def cross_reference?
132
    system? && SystemNoteService.cross_reference?(note)
133 134
  end

135 136
  def diff_note?
    false
137 138
  end

139
  def active?
140
    true
141 142
  end

143 144
  def max_attachment_size
    current_application_settings.max_attachment_size.megabytes.to_i
145 146
  end

147 148
  def hook_attrs
    attributes
149 150 151 152 153 154
  end

  def for_commit?
    noteable_type == "Commit"
  end

Riyad Preukschas committed
155 156 157 158
  def for_issue?
    noteable_type == "Issue"
  end

159 160 161 162
  def for_merge_request?
    noteable_type == "MergeRequest"
  end

163
  def for_snippet?
164 165 166
    noteable_type == "Snippet"
  end

167
  def for_personal_snippet?
Jarka Kadlecova committed
168 169 170 171 172
    noteable.is_a?(PersonalSnippet)
  end

  def skip_project_check?
    for_personal_snippet?
173 174
  end

175 176 177
  # override to return commits, which are not active record
  def noteable
    if for_commit?
178
      project.commit(commit_id)
179
    else
180
      super
181
    end
182 183
  # Temp fix to prevent app crash
  # if note commit id doesn't exist
184
  rescue
185
    nil
186
  end
187

Andrew8xx8 committed
188
  # FIXME: Hack for polymorphic associations with STI
Steven Burgart committed
189
  #        For more information visit http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Polymorphic+Associations
190 191
  def noteable_type=(noteable_type)
    super(noteable_type.to_s.classify.constantize.base_class.to_s)
Andrew8xx8 committed
192
  end
193

194
  def editable?
195
    !system?
196
  end
197

198
  def cross_reference_not_visible_for?(user)
199 200 201 202 203 204 205 206 207
    cross_reference? && !has_referenced_mentionables?(user)
  end

  def has_referenced_mentionables?(user)
    if user_visible_reference_count.present?
      user_visible_reference_count > 0
    else
      referenced_mentionables(user).any?
    end
208 209
  end

210
  def award_emoji?
211
    can_be_award_emoji? && contains_emoji_only?
212 213
  end

214 215 216 217
  def emoji_awardable?
    !system?
  end

218
  def can_be_award_emoji?
219
    noteable.is_a?(Awardable) && !part_of_discussion?
220 221
  end

222
  def contains_emoji_only?
223
    note =~ /\A#{Banzai::Filter::EmojiFilter.emoji_pattern}\s?\Z/
224 225
  end

Jarka Kadlecova committed
226 227 228 229
  def to_ability_name
    for_personal_snippet? ? 'personal_snippet' : noteable_type.underscore
  end

230
  def can_be_discussion_note?
231
    self.noteable.supports_discussions? && !part_of_discussion?
232 233
  end

234 235
  def discussion_class(noteable = nil)
    # When commit notes are rendered on an MR's Discussion page, they are
Douwe Maan committed
236 237
    # displayed in one discussion instead of individually.
    # See also `#discussion_id` and `Discussion.override_discussion_id`.
Douwe Maan committed
238 239
    if noteable && noteable != self.noteable
      OutOfContextDiscussion
240 241 242 243 244
    else
      IndividualNoteDiscussion
    end
  end

Douwe Maan committed
245
  # See `Discussion.override_discussion_id` for details.
246 247 248 249
  def discussion_id(noteable = nil)
    discussion_class(noteable).override_discussion_id(self) || super()
  end

Douwe Maan committed
250 251 252 253
  # Returns a discussion containing just this note.
  # This method exists as an alternative to `#discussion` to use when the methods
  # we intend to call on the Discussion object don't require it to have all of its notes,
  # and just depend on the first note or the type of discussion. This saves us a DB query.
254 255 256 257
  def to_discussion(noteable = nil)
    Discussion.build([self], noteable)
  end

Douwe Maan committed
258 259 260
  # Returns the entire discussion this note is part of.
  # Consider using `#to_discussion` if we do not need to render the discussion
  # and all its notes and if we don't care about the discussion's resolvability status.
261
  def discussion
Douwe Maan committed
262 263
    full_discussion = self.noteable.notes.find_discussion(self.discussion_id) if part_of_discussion?
    full_discussion || to_discussion
264 265 266
  end

  def part_of_discussion?
Douwe Maan committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    !to_discussion.individual_note?
  end

  def in_reply_to?(other)
    case other
    when Note
      if part_of_discussion?
        in_reply_to?(other.noteable) && in_reply_to?(other.to_discussion)
      else
        in_reply_to?(other.noteable)
      end
    when Discussion
      self.discussion_id == other.id
    when Noteable
      self.noteable == other
    else
      false
    end
285 286
  end

287 288 289 290 291
  private

  def keep_around_commit
    project.repository.keep_around(self.commit_id)
  end
292 293 294 295 296 297 298 299

  def nullify_blank_type
    self.type = nil if self.type.blank?
  end

  def nullify_blank_line_code
    self.line_code = nil if self.line_code.blank?
  end
300 301 302

  def ensure_discussion_id
    return unless self.persisted?
303 304
    # Needed in case the SELECT statement doesn't ask for `discussion_id`
    return unless self.has_attribute?(:discussion_id)
305 306 307 308 309 310 311
    return if self.discussion_id

    set_discussion_id
    update_column(:discussion_id, self.discussion_id)
  end

  def set_discussion_id
312
    self.discussion_id ||= discussion_class.discussion_id(self)
313 314
  end

315 316 317 318 319 320 321 322 323 324 325
  def expire_etag_cache
    return unless for_issue?

    key = Gitlab::Routing.url_helpers.namespace_project_noteable_notes_path(
      noteable.project.namespace,
      noteable.project,
      target_type: noteable_type.underscore,
      target_id: noteable.id
    )
    Gitlab::EtagCaching::Store.new.touch(key)
  end
gitlabhq committed
326
end