note.rb 6.28 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
class Note < ActiveRecord::Base
2
  extend ActiveModel::Naming
3
  include Gitlab::CurrentSettings
4
  include Participable
5
  include Mentionable
6
  include Awardable
7
  include Importable
8

9 10 11 12
  # Attribute containing rendered and redacted Markdown as generated by
  # Banzai::ObjectRenderer.
  attr_accessor :note_html

13 14 15 16
  # An Array containing the number of visible references as generated by
  # Banzai::ObjectRenderer
  attr_accessor :user_visible_reference_count

17 18
  default_value_for :system, false

Yorick Peterse's avatar
Yorick Peterse committed
19
  attr_mentionable :note, pipeline: :note
20
  participant :author
21

gitlabhq's avatar
gitlabhq committed
22
  belongs_to :project
23
  belongs_to :noteable, polymorphic: true, touch: true
24
  belongs_to :author, class_name: "User"
25
  belongs_to :updated_by, class_name: "User"
gitlabhq's avatar
gitlabhq committed
26

27
  has_many :todos, dependent: :destroy
28
  has_many :events, as: :target, dependent: :destroy
29

30
  delegate :gfm_reference, :local_reference, to: :noteable
31 32
  delegate :name, to: :project, prefix: true
  delegate :name, :email, to: :author, prefix: true
33
  delegate :title, to: :noteable, allow_nil: true
34

35
  validates :note, :project, presence: true
Z.J. van de Weg's avatar
Z.J. van de Weg committed
36

37 38
  # Attachments are deprecated and are handled by Markdown uploader
  validates :attachment, file_size: { maximum: :max_attachment_size }
gitlabhq's avatar
gitlabhq committed
39

40
  validates :noteable_type, presence: true
41
  validates :noteable_id, presence: true, unless: [:for_commit?, :importing?]
42
  validates :commit_id, presence: true, if: :for_commit?
Valeriy's avatar
Valeriy committed
43
  validates :author, presence: true
44

45
  validate unless: [:for_commit?, :importing?] do |note|
46
    unless note.noteable.try(:project) == note.project
47
      errors.add(:invalid_project, 'Note and noteable project mismatch')
48 49 50
    end
  end

51
  mount_uploader :attachment, AttachmentUploader
Andrew Kumanyaev's avatar
Andrew Kumanyaev committed
52 53

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

Douwe Maan's avatar
Douwe Maan committed
63
  scope :diff_notes, ->{ where(type: ['LegacyDiffNote', 'DiffNote']) }
64 65
  scope :non_diff_notes, ->{ where(type: ['Note', nil]) }

66
  scope :with_associations, -> do
67 68
    # FYI noteable cannot be loaded for LegacyDiffNote for commits
    includes(:author, :noteable, :updated_by,
69
             project: [:project_members, { group: [:group_members] }])
70
  end
gitlabhq's avatar
gitlabhq committed
71

72
  before_validation :nullify_blank_type, :nullify_blank_line_code
73
  after_save :keep_around_commit
74

75
  class << self
76 77 78
    def model_name
      ActiveModel::Name.new(self, nil, 'note')
    end
79

80 81
    def build_discussion_id(noteable_type, noteable_id)
      [:discussion, noteable_type.try(:underscore), noteable_id].join("-")
82
    end
83

84
    def discussions
85
      Discussion.for_notes(all)
86
    end
87

88 89 90
    def grouped_diff_discussions
      notes = diff_notes.fresh.select(&:active?)
      Discussion.for_diff_notes(notes).map { |d| [d.line_code, d] }.to_h
91 92
    end

93 94 95 96
    # Searches for notes matching the given query.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
97 98
    # query   - The search query as a String.
    # as_user - Limit results to those viewable by a specific user
99 100
    #
    # Returns an ActiveRecord::Relation.
101
    def search(query, as_user: nil)
102
      table   = arel_table
103 104
      pattern = "%#{query}%"

105 106 107
      Note.joins('LEFT JOIN issues ON issues.id = noteable_id').
        where(table[:note].matches(pattern)).
        merge(Issue.visible_to_user(as_user))
108
    end
109
  end
110

111
  def cross_reference?
112
    system && SystemNoteService.cross_reference?(note)
113 114
  end

115 116
  def diff_note?
    false
117 118
  end

119 120
  def legacy_diff_note?
    false
121 122
  end

Douwe Maan's avatar
Douwe Maan committed
123 124 125 126
  def new_diff_note?
    false
  end

127
  def active?
128
    true
129 130
  end

131 132 133 134
  def discussion_id
    @discussion_id ||=
      if for_merge_request?
        [:discussion, :note, id].join("-")
135
      else
136
        self.class.build_discussion_id(noteable_type, noteable_id || commit_id)
137 138 139
      end
  end

140 141
  def max_attachment_size
    current_application_settings.max_attachment_size.megabytes.to_i
142 143
  end

144 145
  def hook_attrs
    attributes
146 147 148 149 150 151
  end

  def for_commit?
    noteable_type == "Commit"
  end

Riyad Preukschas's avatar
Riyad Preukschas committed
152 153 154 155
  def for_issue?
    noteable_type == "Issue"
  end

156 157 158 159
  def for_merge_request?
    noteable_type == "MergeRequest"
  end

160
  def for_snippet?
161 162 163
    noteable_type == "Snippet"
  end

164 165 166
  # override to return commits, which are not active record
  def noteable
    if for_commit?
167
      project.commit(commit_id)
168
    else
169
      super
170
    end
171 172
  # Temp fix to prevent app crash
  # if note commit id doesn't exist
173
  rescue
174
    nil
175
  end
176

177
  # FIXME: Hack for polymorphic associations with STI
Steven Burgart's avatar
Steven Burgart committed
178
  #        For more information visit http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Polymorphic+Associations
179 180
  def noteable_type=(noteable_type)
    super(noteable_type.to_s.classify.constantize.base_class.to_s)
181
  end
182 183 184 185 186 187 188 189 190 191 192

  # 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
193
    Event.reset_event_cache_for(self)
194
  end
195

196
  def editable?
197
    !system?
198
  end
199

200
  def cross_reference_not_visible_for?(user)
201 202 203 204 205 206 207 208 209
    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
210 211
  end

212
  def award_emoji?
213
    can_be_award_emoji? && contains_emoji_only?
214 215
  end

216 217 218 219
  def emoji_awardable?
    !system?
  end

220
  def can_be_award_emoji?
Z.J. van de Weg's avatar
Z.J. van de Weg committed
221
    noteable.is_a?(Awardable)
222 223
  end

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

  def award_emoji_name
Dino M's avatar
Dino M committed
229
    note.match(Banzai::Filter::EmojiFilter.emoji_pattern)[1]
230
  end
231 232 233 234 235 236

  private

  def keep_around_commit
    project.repository.keep_around(self.commit_id)
  end
237 238 239 240 241 242 243 244

  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
gitlabhq's avatar
gitlabhq committed
245
end