BigW Consortium Gitlab

note.rb 5.68 KB
Newer Older
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
  default_value_for :system, false

Yorick Peterse committed
15
  attr_mentionable :note, pipeline: :note
16
  participant :author
17

gitlabhq committed
18
  belongs_to :project
19
  belongs_to :noteable, polymorphic: true, touch: true
20
  belongs_to :author, class_name: "User"
21
  belongs_to :updated_by, class_name: "User"
gitlabhq committed
22

23
  has_many :todos, dependent: :destroy
24

25
  delegate :gfm_reference, :local_reference, to: :noteable
26 27
  delegate :name, to: :project, prefix: true
  delegate :name, :email, to: :author, prefix: true
28
  delegate :title, to: :noteable, allow_nil: true
29

30
  validates :note, :project, presence: true
31

32 33
  # Attachments are deprecated and are handled by Markdown uploader
  validates :attachment, file_size: { maximum: :max_attachment_size }
gitlabhq committed
34

35
  validates :noteable_type, presence: true
36
  validates :noteable_id, presence: true, unless: [:for_commit?, :importing?]
37
  validates :commit_id, presence: true, if: :for_commit?
Valery Sizov committed
38
  validates :author, presence: true
39

40
  validate unless: [:for_commit?, :importing?] do |note|
41
    unless note.noteable.try(:project) == note.project
42
      errors.add(:invalid_project, 'Note and noteable project mismatch')
43 44 45
    end
  end

46
  mount_uploader :attachment, AttachmentUploader
Andrey Kumanyaev committed
47 48

  # Scopes
49
  scope :for_commit_id, ->(commit_id) { where(noteable_type: "Commit", commit_id: commit_id) }
50
  scope :system, ->{ where(system: true) }
51
  scope :user, ->{ where(system: false) }
52
  scope :common, ->{ where(noteable_type: ["", nil]) }
53
  scope :fresh, ->{ order(created_at: :asc, id: :asc) }
54 55
  scope :inc_author_project, ->{ includes(:project, :author) }
  scope :inc_author, ->{ includes(:author) }
56
  scope :inc_author_project_award_emoji, ->{ includes(:project, :author, :award_emoji) }
gitlabhq committed
57

58 59 60
  scope :legacy_diff_notes, ->{ where(type: 'LegacyDiffNote') }
  scope :non_diff_notes, ->{ where(type: ['Note', nil]) }

61
  scope :with_associations, -> do
62 63
    # FYI noteable cannot be loaded for LegacyDiffNote for commits
    includes(:author, :noteable, :updated_by,
64
             project: [:project_members, { group: [:group_members] }])
65
  end
gitlabhq committed
66

67
  before_validation :clear_blank_line_code!
68

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

74 75
    def build_discussion_id(noteable_type, noteable_id)
      [:discussion, noteable_type.try(:underscore), noteable_id].join("-")
76
    end
77

78 79
    def discussions
      all.group_by(&:discussion_id).values
80
    end
81

82 83
    def grouped_diff_notes
      legacy_diff_notes.select(&:active?).sort_by(&:created_at).group_by(&:line_code)
84 85
    end

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

98 99 100
      Note.joins('LEFT JOIN issues ON issues.id = noteable_id').
        where(table[:note].matches(pattern)).
        merge(Issue.visible_to_user(as_user))
101
    end
102
  end
103

104
  def cross_reference?
105
    system && SystemNoteService.cross_reference?(note)
106 107
  end

108 109
  def diff_note?
    false
110 111
  end

112 113
  def legacy_diff_note?
    false
114 115
  end

116
  def active?
117
    true
118 119
  end

120 121 122 123
  def discussion_id
    @discussion_id ||=
      if for_merge_request?
        [:discussion, :note, id].join("-")
124
      else
125
        self.class.build_discussion_id(noteable_type, noteable_id || commit_id)
126 127 128
      end
  end

129 130
  def max_attachment_size
    current_application_settings.max_attachment_size.megabytes.to_i
131 132
  end

133 134
  def hook_attrs
    attributes
135 136 137 138 139 140
  end

  def for_commit?
    noteable_type == "Commit"
  end

Riyad Preukschas committed
141 142 143 144
  def for_issue?
    noteable_type == "Issue"
  end

145 146 147 148
  def for_merge_request?
    noteable_type == "MergeRequest"
  end

149
  def for_snippet?
150 151 152
    noteable_type == "Snippet"
  end

153 154 155
  # override to return commits, which are not active record
  def noteable
    if for_commit?
156
      project.commit(commit_id)
157
    else
158
      super
159
    end
160 161
  # Temp fix to prevent app crash
  # if note commit id doesn't exist
162
  rescue
163
    nil
164
  end
165

Andrew8xx8 committed
166
  # FIXME: Hack for polymorphic associations with STI
Steven Burgart committed
167
  #        For more information visit http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Polymorphic+Associations
168 169
  def noteable_type=(noteable_type)
    super(noteable_type.to_s.classify.constantize.base_class.to_s)
Andrew8xx8 committed
170
  end
171 172 173 174 175 176 177 178 179 180 181

  # 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
182
    Event.reset_event_cache_for(self)
183
  end
184

185
  def editable?
186
    !system?
187
  end
188

189 190 191 192
  def cross_reference_not_visible_for?(user)
    cross_reference? && referenced_mentionables(user).empty?
  end

193 194
  def award_emoji?
    award_emoji_supported? && contains_emoji_only?
195 196
  end

197 198 199 200
  def emoji_awardable?
    !system?
  end

201 202 203 204
  def clear_blank_line_code!
    self.line_code = nil if self.line_code.blank?
  end

205
  def award_emoji_supported?
206
    noteable.is_a?(Awardable)
207 208
  end

209
  def contains_emoji_only?
210
    note =~ /\A#{Banzai::Filter::EmojiFilter.emoji_pattern}\s?\Z/
211 212 213
  end

  def award_emoji_name
214
    original_name = note.match(Banzai::Filter::EmojiFilter.emoji_pattern)[1]
215
    Gitlab::AwardEmoji.normalize_emoji_name(original_name)
216
  end
gitlabhq committed
217
end