2015-08-18 18:46:36 -04:00
|
|
|
class SentNotification < ActiveRecord::Base
|
2016-06-20 13:23:10 -04:00
|
|
|
serialize :position, Gitlab::Diff::Position
|
|
|
|
|
2015-08-18 18:46:36 -04:00
|
|
|
belongs_to :project
|
|
|
|
belongs_to :noteable, polymorphic: true
|
|
|
|
belongs_to :recipient, class_name: "User"
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
validates :project, :recipient, presence: true
|
|
|
|
validates :reply_key, presence: true, uniqueness: true
|
2015-08-18 20:02:26 -04:00
|
|
|
validates :noteable_id, presence: true, unless: :for_commit?
|
|
|
|
validates :commit_id, presence: true, if: :for_commit?
|
2017-03-09 20:29:11 -05:00
|
|
|
validates :in_reply_to_discussion_id, format: { with: /\A\h{40}\z/, allow_nil: true }
|
2016-06-20 13:23:10 -04:00
|
|
|
validate :note_valid
|
2015-08-18 18:46:36 -04:00
|
|
|
|
2016-07-03 19:58:58 -04:00
|
|
|
after_save :keep_around_commit
|
|
|
|
|
2015-08-19 15:35:23 -04:00
|
|
|
class << self
|
2015-09-21 03:46:47 -04:00
|
|
|
def reply_key
|
|
|
|
SecureRandom.hex(16)
|
|
|
|
end
|
|
|
|
|
2015-08-19 15:35:23 -04:00
|
|
|
def for(reply_key)
|
|
|
|
find_by(reply_key: reply_key)
|
|
|
|
end
|
|
|
|
|
2017-03-17 15:25:52 -04:00
|
|
|
def record(noteable, recipient_id, reply_key = self.reply_key, attrs = {})
|
2015-08-19 15:35:23 -04:00
|
|
|
noteable_id = nil
|
|
|
|
commit_id = nil
|
|
|
|
if noteable.is_a?(Commit)
|
|
|
|
commit_id = noteable.id
|
|
|
|
else
|
|
|
|
noteable_id = noteable.id
|
|
|
|
end
|
|
|
|
|
2016-06-20 13:15:44 -04:00
|
|
|
attrs.reverse_merge!(
|
2017-03-09 20:29:11 -05:00
|
|
|
project: noteable.project,
|
|
|
|
recipient_id: recipient_id,
|
|
|
|
reply_key: reply_key,
|
|
|
|
|
|
|
|
noteable_type: noteable.class.name,
|
|
|
|
noteable_id: noteable_id,
|
|
|
|
commit_id: commit_id,
|
2015-08-19 15:35:23 -04:00
|
|
|
)
|
2015-09-20 12:18:39 -04:00
|
|
|
|
2016-06-20 13:15:44 -04:00
|
|
|
create(attrs)
|
2015-09-20 12:18:39 -04:00
|
|
|
end
|
|
|
|
|
2017-03-17 15:25:52 -04:00
|
|
|
def record_note(note, recipient_id, reply_key = self.reply_key, attrs = {})
|
2017-03-30 21:33:45 -04:00
|
|
|
attrs[:in_reply_to_discussion_id] = note.discussion_id
|
2016-06-20 13:15:44 -04:00
|
|
|
|
|
|
|
record(note.noteable, recipient_id, reply_key, attrs)
|
2015-08-19 15:35:23 -04:00
|
|
|
end
|
2015-08-18 18:46:36 -04:00
|
|
|
end
|
|
|
|
|
2016-01-11 08:23:45 -05:00
|
|
|
def unsubscribable?
|
2016-01-09 06:47:31 -05:00
|
|
|
!for_commit?
|
|
|
|
end
|
|
|
|
|
2015-08-18 18:46:36 -04:00
|
|
|
def for_commit?
|
|
|
|
noteable_type == "Commit"
|
|
|
|
end
|
|
|
|
|
|
|
|
def noteable
|
|
|
|
if for_commit?
|
2015-08-18 20:02:26 -04:00
|
|
|
project.commit(commit_id) rescue nil
|
2015-08-18 18:46:36 -04:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2015-12-09 05:59:25 -05:00
|
|
|
|
2016-07-11 13:21:05 -04:00
|
|
|
def position=(new_position)
|
|
|
|
if new_position.is_a?(String)
|
|
|
|
new_position = JSON.parse(new_position) rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_position.is_a?(Hash)
|
|
|
|
new_position = new_position.with_indifferent_access
|
|
|
|
new_position = Gitlab::Diff::Position.new(new_position)
|
|
|
|
end
|
|
|
|
|
|
|
|
super(new_position)
|
|
|
|
end
|
|
|
|
|
2015-12-09 05:59:25 -05:00
|
|
|
def to_param
|
|
|
|
self.reply_key
|
|
|
|
end
|
2016-07-03 19:58:58 -04:00
|
|
|
|
2017-03-17 15:25:52 -04:00
|
|
|
def create_reply(message, dryrun: false)
|
|
|
|
klass = dryrun ? Notes::BuildService : Notes::CreateService
|
|
|
|
klass.new(self.project, self.recipient, reply_params.merge(note: message)).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def reply_params
|
2017-03-09 20:29:11 -05:00
|
|
|
attrs = {
|
|
|
|
noteable_type: self.noteable_type,
|
|
|
|
noteable_id: self.noteable_id,
|
|
|
|
commit_id: self.commit_id
|
2016-06-20 13:23:10 -04:00
|
|
|
}
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
if self.in_reply_to_discussion_id.present?
|
|
|
|
attrs[:in_reply_to_discussion_id] = self.in_reply_to_discussion_id
|
|
|
|
else
|
2017-04-04 18:27:23 -04:00
|
|
|
# Remove in GitLab 10.0, when we will not support replying to SentNotifications
|
|
|
|
# that don't have `in_reply_to_discussion_id` anymore.
|
2017-03-09 20:29:11 -05:00
|
|
|
attrs.merge!(
|
|
|
|
type: self.note_type,
|
|
|
|
|
|
|
|
# LegacyDiffNote
|
|
|
|
line_code: self.line_code,
|
|
|
|
|
|
|
|
# DiffNote
|
|
|
|
position: self.position.to_json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
attrs
|
2016-06-20 13:23:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def note_valid
|
2017-03-17 15:25:52 -04:00
|
|
|
note = create_reply('Test', dryrun: true)
|
|
|
|
|
|
|
|
unless note.valid?
|
|
|
|
self.errors.add(:base, "Note parameters are invalid: #{note.errors.full_messages.to_sentence}")
|
|
|
|
end
|
2016-06-20 13:23:10 -04:00
|
|
|
end
|
|
|
|
|
2016-07-03 19:58:58 -04:00
|
|
|
def keep_around_commit
|
|
|
|
project.repository.keep_around(self.commit_id)
|
|
|
|
end
|
2015-08-18 18:46:36 -04:00
|
|
|
end
|