BigW Consortium Gitlab

note.rb 5.78 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

8 9
  default_value_for :system, false

Yorick Peterse committed
10
  attr_mentionable :note, pipeline: :note
11
  participant :author
12

gitlabhq committed
13
  belongs_to :project
14
  belongs_to :noteable, polymorphic: true, touch: true
15
  belongs_to :author, class_name: "User"
16
  belongs_to :updated_by, class_name: "User"
gitlabhq committed
17

18
  has_many :todos, dependent: :destroy
19

20
  delegate :gfm_reference, :local_reference, to: :noteable
21 22
  delegate :name, to: :project, prefix: true
  delegate :name, :email, to: :author, prefix: true
23
  delegate :title, to: :noteable, allow_nil: true
24

25
  validates :note, :project, presence: true
26

27 28
  # Attachments are deprecated and are handled by Markdown uploader
  validates :attachment, file_size: { maximum: :max_attachment_size }
gitlabhq committed
29

30
  validates :noteable_type, presence: true
31 32
  validates :noteable_id, presence: true, unless: :for_commit?
  validates :commit_id, presence: true, if: :for_commit?
Valery Sizov committed
33
  validates :author, presence: true
34

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

41
  mount_uploader :attachment, AttachmentUploader
Andrey Kumanyaev committed
42 43

  # Scopes
44
  scope :for_commit_id, ->(commit_id) { where(noteable_type: "Commit", commit_id: commit_id) }
45
  scope :system, ->{ where(system: true) }
46
  scope :user, ->{ where(system: false) }
47
  scope :common, ->{ where(noteable_type: ["", nil]) }
48
  scope :fresh, ->{ order(created_at: :asc, id: :asc) }
49 50
  scope :inc_author_project, ->{ includes(:project, :author) }
  scope :inc_author, ->{ includes(:author) }
gitlabhq committed
51

52 53 54
  scope :legacy_diff_notes, ->{ where(type: 'LegacyDiffNote') }
  scope :non_diff_notes, ->{ where(type: ['Note', nil]) }

55
  scope :with_associations, -> do
56
    includes(:author, :noteable, :updated_by,
57
             project: [:project_members, { group: [:group_members] }])
58
  end
gitlabhq committed
59

60
  before_validation :clear_blank_line_code!
61

62
  class << self
63 64 65
    def model_name
      ActiveModel::Name.new(self, nil, 'note')
    end
66

67 68
    def build_discussion_id(noteable_type, noteable_id)
      [:discussion, noteable_type.try(:underscore), noteable_id].join("-")
69
    end
70

71 72
    def discussions
      all.group_by(&:discussion_id).values
73
    end
74

75 76
    def grouped_diff_notes
      legacy_diff_notes.select(&:active?).sort_by(&:created_at).group_by(&:line_code)
77 78
    end

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

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
      found_notes = joins('LEFT JOIN issues ON issues.id = noteable_id').
        where(table[:note].matches(pattern))

      if as_user
        found_notes.where('
          issues.confidential IS NULL
          OR issues.confidential IS FALSE
          OR (issues.confidential IS TRUE
            AND (issues.author_id = :user_id
            OR issues.assignee_id = :user_id
            OR issues.project_id IN(:project_ids)))',
          user_id: as_user.id,
          project_ids: as_user.authorized_projects.select(:id))
      else
        found_notes.where('issues.confidential IS NULL OR issues.confidential IS FALSE')
      end
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
  def active?
123
    true
124 125
  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 133 134
      end
  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 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

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

Andrew8xx8 committed
172
  # FIXME: Hack for polymorphic associations with STI
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 committed
176
  end
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)
189
  end
190

191
  def editable?
192
    !system?
193
  end
194

195 196 197 198
  def cross_reference_not_visible_for?(user)
    cross_reference? && referenced_mentionables(user).empty?
  end

199 200
  def award_emoji?
    award_emoji_supported? && contains_emoji_only?
201 202
  end

203 204 205 206
  def clear_blank_line_code!
    self.line_code = nil if self.line_code.blank?
  end

207
  def award_emoji_supported?
208
    noteable.is_a?(Awardable)
209 210
  end

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

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