"Docs/Flags/[object Object]" did not exist on "983bb74cda3ddb87d52e8320df2ef69038fef6ca"
note.rb 6.07 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

7 8
  default_value_for :system, false

9
  attr_mentionable :note, cache: true, pipeline: :note
10
  participant :author
11

gitlabhq's avatar
gitlabhq committed
12
  belongs_to :project
13
  belongs_to :noteable, polymorphic: true, touch: true
Nihad Abbasov's avatar
Nihad Abbasov committed
14
  belongs_to :author, class_name: "User"
15
  belongs_to :updated_by, class_name: "User"
gitlabhq's avatar
gitlabhq committed
16

17
  has_many :todos, dependent: :destroy
18

19
  delegate :gfm_reference, :local_reference, to: :noteable
Nihad Abbasov's avatar
Nihad Abbasov committed
20 21
  delegate :name, to: :project, prefix: true
  delegate :name, :email, to: :author, prefix: true
22
  delegate :title, to: :noteable, allow_nil: true
23

24 25
  before_validation :set_award!

26
  validates :note, :project, presence: true
Valery Sizov's avatar
Valery Sizov committed
27
  validates :note, uniqueness: { scope: [:author, :noteable_type, :noteable_id] }, if: ->(n) { n.is_award }
28
  validates :note, inclusion: { in: Emoji.emojis_names }, if: ->(n) { n.is_award }
29 30
  # Attachments are deprecated and are handled by Markdown uploader
  validates :attachment, file_size: { maximum: :max_attachment_size }
gitlabhq's avatar
gitlabhq committed
31

32 33
  validates :noteable_id, presence: true, unless: :for_commit?
  validates :commit_id, presence: true, if: :for_commit?
Valery Sizov's avatar
Valery Sizov committed
34
  validates :author, presence: true
35

36
  validate unless: :for_commit? do |note|
37
    unless note.noteable.try(:project) == note.project
38
      errors.add(:invalid_project, 'Note and noteable project mismatch')
39 40 41
    end
  end

42
  mount_uploader :attachment, AttachmentUploader
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
43 44

  # Scopes
Valery Sizov's avatar
Valery Sizov committed
45 46
  scope :awards, ->{ where(is_award: true) }
  scope :nonawards, ->{ where(is_award: false) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
47
  scope :for_commit_id, ->(commit_id) { where(noteable_type: "Commit", commit_id: commit_id) }
48
  scope :system, ->{ where(system: true) }
49
  scope :user, ->{ where(system: false) }
50
  scope :common, ->{ where(noteable_type: ["", nil]) }
51
  scope :fresh, ->{ order(created_at: :asc, id: :asc) }
52 53
  scope :inc_author_project, ->{ includes(:project, :author) }
  scope :inc_author, ->{ includes(:author) }
gitlabhq's avatar
gitlabhq committed
54

55 56 57
  scope :legacy_diff_notes, ->{ where(type: 'LegacyDiffNote') }
  scope :non_diff_notes, ->{ where(type: ['Note', nil]) }

58
  scope :with_associations, -> do
59
    includes(:author, :noteable, :updated_by,
60
             project: [:project_members, { group: [:group_members] }])
61
  end
gitlabhq's avatar
gitlabhq committed
62

63
  before_validation :clear_blank_line_code!
64

65
  class << self
66 67 68 69 70 71 72
    def model_name
      ActiveModel::Name.new(self, nil, 'note')
    end

    def build_discussion_id(noteable_type, noteable_id)
      [:discussion, noteable_type.try(:underscore), noteable_id].join("-")
    end
73

74 75
    def discussions
      all.group_by(&:discussion_id).values
76
    end
77

78 79
    def grouped_diff_notes
      legacy_diff_notes.select(&:active?).sort_by(&:created_at).group_by(&:line_code)
80 81
    end

82 83 84 85 86 87 88
    # Searches for notes matching the given query.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
    # query - The search query as a String.
    #
    # Returns an ActiveRecord::Relation.
89
    def search(query)
90
      table   = arel_table
91 92 93
      pattern = "%#{query}%"

      where(table[:note].matches(pattern))
94
    end
Valery Sizov's avatar
Valery Sizov committed
95 96

    def grouped_awards
97 98
      notes = {}

Valery Sizov's avatar
Valery Sizov committed
99
      awards.select(:note).distinct.map do |note|
100
        notes[note.note] = where(note: note.note)
Valery Sizov's avatar
Valery Sizov committed
101
      end
102 103 104 105 106

      notes["thumbsup"] ||= Note.none
      notes["thumbsdown"] ||= Note.none

      notes
Valery Sizov's avatar
Valery Sizov committed
107
    end
108
  end
109

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

114 115
  def diff_note?
    false
116 117
  end

118 119
  def legacy_diff_note?
    false
120 121
  end

122 123 124 125
  def active?
    true
  end

126 127 128 129
  def discussion_id
    @discussion_id ||=
      if for_merge_request?
        [:discussion, :note, id].join("-")
130
      else
131
        self.class.build_discussion_id(noteable_type, noteable_id || commit_id)
132
      end
133 134
  end

135 136
  def max_attachment_size
    current_application_settings.max_attachment_size.megabytes.to_i
137 138
  end

139 140
  def hook_attrs
    attributes
141 142 143 144 145 146
  end

  def for_commit?
    noteable_type == "Commit"
  end

Riyad Preukschas's avatar
Riyad Preukschas committed
147 148 149 150
  def for_issue?
    noteable_type == "Issue"
  end

151 152 153 154
  def for_merge_request?
    noteable_type == "MergeRequest"
  end

155
  def for_snippet?
156 157 158
    noteable_type == "Snippet"
  end

Riyad Preukschas's avatar
Riyad Preukschas committed
159 160 161
  # override to return commits, which are not active record
  def noteable
    if for_commit?
162
      project.commit(commit_id)
163
    else
Riyad Preukschas's avatar
Riyad Preukschas committed
164
      super
165
    end
166 167
  # Temp fix to prevent app crash
  # if note commit id doesn't exist
168
  rescue
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
169
    nil
170
  end
171

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

  # 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
188
    Event.reset_event_cache_for(self)
Drew Blessing's avatar
Drew Blessing committed
189
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
190

191
  def downvote?
192
    is_award && note == "thumbsdown"
193 194 195
  end

  def upvote?
196
    is_award && note == "thumbsup"
197 198
  end

199
  def editable?
200
    !system? && !is_award
201
  end
202

203 204 205 206
  def cross_reference_not_visible_for?(user)
    cross_reference? && referenced_mentionables(user).empty?
  end

207
  # Checks if note is an award added as a comment
208
  #
209 210
  # If note is an award, this method sets is_award to true
  #   and changes content of the note to award name.
211 212 213 214
  #
  # Method is executed as a before_validation callback.
  #
  def set_award!
215
    return unless awards_supported? && contains_emoji_only?
216

217 218 219 220
    self.is_award = true
    self.note = award_emoji_name
  end

221 222
  private

223 224 225 226
  def clear_blank_line_code!
    self.line_code = nil if self.line_code.blank?
  end

227
  def awards_supported?
228
    (for_issue? || for_merge_request?) && !diff_note?
229 230
  end

231
  def contains_emoji_only?
232
    note =~ /\A#{Banzai::Filter::EmojiFilter.emoji_pattern}\s?\Z/
233 234 235
  end

  def award_emoji_name
236
    original_name = note.match(Banzai::Filter::EmojiFilter.emoji_pattern)[1]
Valery Sizov's avatar
Valery Sizov committed
237
    AwardEmoji.normilize_emoji_name(original_name)
238
  end
gitlabhq's avatar
gitlabhq committed
239
end