Read about our upcoming Code of Conduct on this issue

note.rb 2.89 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
2
3
4
require 'carrierwave/orm/activerecord'
require 'file_size_validator'

class Note < ActiveRecord::Base
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
5

6
7
8
  attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id,
                  :attachment, :line_code

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
9
10
11
  attr_accessor :notify
  attr_accessor :notify_author

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

Nihad Abbasov's avatar
Nihad Abbasov committed
16
17
  delegate :name, to: :project, prefix: true
  delegate :name, :email, to: :author, prefix: true
18

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
19
  validates :project, presence: true
Nihad Abbasov's avatar
Nihad Abbasov committed
20
21
  validates :note, presence: true, length: { within: 0..5000 }
  validates :attachment, file_size: { maximum: 10.megabytes.to_i }
gitlabhq's avatar
gitlabhq committed
22

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
23
24
25
  mount_uploader  :attachment, AttachmentUploader

  # Scopes
26
27
28
  scope :common, where(noteable_id: nil)
  scope :today, where("created_at >= :date", date: Date.today)
  scope :last_week, where("created_at  >= :date", date: (Date.today - 7.days))
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
29
  scope :since, ->(day) { where("created_at  >= :date", date: (day)) }
30
  scope :fresh, order("created_at ASC, id ASC")
gitlabhq's avatar
gitlabhq committed
31
32
  scope :inc_author_project, includes(:project, :author)
  scope :inc_author, includes(:author)
gitlabhq's avatar
gitlabhq committed
33

34
  def self.create_status_change_note(noteable, author, status)
Nihad Abbasov's avatar
Nihad Abbasov committed
35
36
37
38
39
40
    create({
      noteable: noteable,
      project: noteable.project,
      author: author,
      note: "_Status changed to #{status}_"
    }, without_protection: true)
41
42
  end

Valery Sizov's avatar
Valery Sizov committed
43
44
45
  def notify
    @notify ||= false
  end
Cedric Gatay's avatar
Cedric Gatay committed
46
47
48
49

  def notify_author
    @notify_author ||= false
  end
50
51

  def target
52
    if noteable_type == "Commit"
53
      project.commit(noteable_id)
54
    else
55
56
      noteable
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
57
58
  # Temp fix to prevent app crash
  # if note commit id doesnt exist
59
  rescue
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
60
    nil
61
  end
62
63
64
65

  # Check if we can notify commit author
  # with email about our comment
  #
66
67
  # If commit author email exist in project
  # and commit author is not passed user we can
68
69
70
71
  # send email to him
  #
  # params:
  #   user - current user
72
  #
73
74
75
76
77
78
79
80
81
82
83
84
85
  # return:
  #   Boolean
  #
  def notify_only_author?(user)
    commit? && commit_author &&
      commit_author.email != user.email
  end

  def commit?
    noteable_type == "Commit"
  end

  def commit_author
86
87
    @commit_author ||=
      project.users.find_by_email(target.author_email) ||
88
      project.users.find_by_name(target.author_name)
89
  rescue
90
91
    nil
  end
92
93
94
95

  # Returns true if this is an upvote note,
  # otherwise false is returned
  def upvote?
96
    note.start_with?('+1') || note.start_with?(':+1:')
97
  end
98
99
100
101
102
103

  # Returns true if this is a downvote note,
  # otherwise false is returned
  def downvote?
    note.start_with?('-1') || note.start_with?(':-1:')
  end
gitlabhq's avatar
gitlabhq committed
104
end
Nihad Abbasov's avatar
Nihad Abbasov committed
105

gitlabhq's avatar
gitlabhq committed
106
107
108
109
# == Schema Information
#
# Table name: notes
#
Nihad Abbasov's avatar
Nihad Abbasov committed
110
#  id            :integer         not null, primary key
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
111
#  note          :text
gitlabhq's avatar
gitlabhq committed
112
113
#  noteable_id   :string(255)
#  noteable_type :string(255)
Nihad Abbasov's avatar
Nihad Abbasov committed
114
#  author_id     :integer
randx's avatar
randx committed
115
116
#  created_at    :datetime        not null
#  updated_at    :datetime        not null
Nihad Abbasov's avatar
Nihad Abbasov committed
117
#  project_id    :integer
gitlabhq's avatar
gitlabhq committed
118
#  attachment    :string(255)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
119
#  line_code     :string(255)
gitlabhq's avatar
gitlabhq committed
120
#