2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class SentNotification < ApplicationRecord
|
2017-07-03 14:01:41 +00:00
|
|
|
serialize :position, Gitlab::Diff::Position # rubocop:disable Cop/ActiveRecordSerialize
|
2016-06-20 17:23:10 +00:00
|
|
|
|
2015-08-18 22:46:36 +00:00
|
|
|
belongs_to :project
|
2017-06-02 12:29:30 +00:00
|
|
|
belongs_to :noteable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
|
2015-08-18 22:46:36 +00:00
|
|
|
belongs_to :recipient, class_name: "User"
|
|
|
|
|
2018-05-03 21:32:20 +00:00
|
|
|
validates :recipient, presence: true
|
2017-03-10 01:29:11 +00:00
|
|
|
validates :reply_key, presence: true, uniqueness: true
|
2015-08-19 00:02:26 +00:00
|
|
|
validates :noteable_id, presence: true, unless: :for_commit?
|
|
|
|
validates :commit_id, presence: true, if: :for_commit?
|
2017-03-10 01:29:11 +00:00
|
|
|
validates :in_reply_to_discussion_id, format: { with: /\A\h{40}\z/, allow_nil: true }
|
2016-06-20 17:23:10 +00:00
|
|
|
validate :note_valid
|
2015-08-18 22:46:36 +00:00
|
|
|
|
2018-05-03 21:32:20 +00:00
|
|
|
after_save :keep_around_commit, if: :for_commit?
|
2016-07-03 23:58:58 +00:00
|
|
|
|
2015-08-19 19:35:23 +00:00
|
|
|
class << self
|
2015-09-21 07:46:47 +00:00
|
|
|
def reply_key
|
|
|
|
SecureRandom.hex(16)
|
|
|
|
end
|
|
|
|
|
2015-08-19 19:35:23 +00:00
|
|
|
def for(reply_key)
|
|
|
|
find_by(reply_key: reply_key)
|
|
|
|
end
|
|
|
|
|
2017-03-17 19:25:52 +00:00
|
|
|
def record(noteable, recipient_id, reply_key = self.reply_key, attrs = {})
|
2015-08-19 19:35:23 +00: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 17:15:44 +00:00
|
|
|
attrs.reverse_merge!(
|
2017-03-10 01:29:11 +00:00
|
|
|
project: noteable.project,
|
|
|
|
recipient_id: recipient_id,
|
|
|
|
reply_key: reply_key,
|
|
|
|
|
|
|
|
noteable_type: noteable.class.name,
|
|
|
|
noteable_id: noteable_id,
|
2017-05-03 11:27:17 +00:00
|
|
|
commit_id: commit_id
|
2015-08-19 19:35:23 +00:00
|
|
|
)
|
2015-09-20 16:18:39 +00:00
|
|
|
|
2016-06-20 17:15:44 +00:00
|
|
|
create(attrs)
|
2015-09-20 16:18:39 +00:00
|
|
|
end
|
|
|
|
|
2017-03-17 19:25:52 +00:00
|
|
|
def record_note(note, recipient_id, reply_key = self.reply_key, attrs = {})
|
2019-02-06 10:31:46 +00:00
|
|
|
attrs[:in_reply_to_discussion_id] = note.discussion_id if note.part_of_discussion?
|
2016-06-20 17:15:44 +00:00
|
|
|
|
|
|
|
record(note.noteable, recipient_id, reply_key, attrs)
|
2015-08-19 19:35:23 +00:00
|
|
|
end
|
2015-08-18 22:46:36 +00:00
|
|
|
end
|
|
|
|
|
2016-01-11 13:23:45 +00:00
|
|
|
def unsubscribable?
|
2017-10-09 12:59:10 +00:00
|
|
|
!(for_commit? || for_snippet?)
|
2016-01-09 11:47:31 +00:00
|
|
|
end
|
|
|
|
|
2015-08-18 22:46:36 +00:00
|
|
|
def for_commit?
|
|
|
|
noteable_type == "Commit"
|
|
|
|
end
|
|
|
|
|
2017-10-09 12:59:10 +00:00
|
|
|
def for_snippet?
|
|
|
|
noteable_type.end_with?('Snippet')
|
|
|
|
end
|
|
|
|
|
2015-08-18 22:46:36 +00:00
|
|
|
def noteable
|
|
|
|
if for_commit?
|
2015-08-19 00:02:26 +00:00
|
|
|
project.commit(commit_id) rescue nil
|
2015-08-18 22:46:36 +00:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2015-12-09 10:59:25 +00:00
|
|
|
|
2016-07-11 17:21:05 +00: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 10:59:25 +00:00
|
|
|
def to_param
|
|
|
|
self.reply_key
|
|
|
|
end
|
2016-07-03 23:58:58 +00:00
|
|
|
|
2017-03-17 19:25:52 +00: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
|
2019-02-06 10:31:46 +00:00
|
|
|
{
|
2017-03-10 01:29:11 +00:00
|
|
|
noteable_type: self.noteable_type,
|
|
|
|
noteable_id: self.noteable_id,
|
2019-02-06 10:31:46 +00:00
|
|
|
commit_id: self.commit_id,
|
|
|
|
in_reply_to_discussion_id: self.in_reply_to_discussion_id
|
2016-06-20 17:23:10 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def note_valid
|
2017-03-17 19:25:52 +00:00
|
|
|
note = create_reply('Test', dryrun: true)
|
|
|
|
|
|
|
|
unless note.valid?
|
2020-03-03 09:07:54 +00:00
|
|
|
self.errors.add(
|
|
|
|
:base, _("Note parameters are invalid: %{errors}") %
|
|
|
|
{ errors: note.errors.full_messages.to_sentence }
|
|
|
|
)
|
2017-03-17 19:25:52 +00:00
|
|
|
end
|
2016-06-20 17:23:10 +00:00
|
|
|
end
|
|
|
|
|
2016-07-03 23:58:58 +00:00
|
|
|
def keep_around_commit
|
|
|
|
project.repository.keep_around(self.commit_id)
|
|
|
|
end
|
2015-08-18 22:46:36 +00:00
|
|
|
end
|