debian-mirror-gitlab/app/models/sent_notification.rb

122 lines
2.9 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class SentNotification < ApplicationRecord
2017-09-10 17:25:29 +05:30
serialize :position, Gitlab::Diff::Position # rubocop:disable Cop/ActiveRecordSerialize
2016-08-24 12:49:21 +05:30
2015-09-25 12:07:36 +05:30
belongs_to :project
2017-09-10 17:25:29 +05:30
belongs_to :noteable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
2015-09-25 12:07:36 +05:30
belongs_to :recipient, class_name: "User"
2018-10-15 14:42:47 +05:30
validates :recipient, presence: true
2017-08-17 22:00:37 +05:30
validates :reply_key, presence: true, uniqueness: true
2015-09-25 12:07:36 +05:30
validates :noteable_id, presence: true, unless: :for_commit?
validates :commit_id, presence: true, if: :for_commit?
2017-08-17 22:00:37 +05:30
validates :in_reply_to_discussion_id, format: { with: /\A\h{40}\z/, allow_nil: true }
2016-08-24 12:49:21 +05:30
validate :note_valid
2018-10-15 14:42:47 +05:30
after_save :keep_around_commit, if: :for_commit?
2015-09-25 12:07:36 +05:30
class << self
def reply_key
SecureRandom.hex(16)
end
def for(reply_key)
find_by(reply_key: reply_key)
end
2017-08-17 22:00:37 +05:30
def record(noteable, recipient_id, reply_key = self.reply_key, attrs = {})
2015-09-25 12:07:36 +05:30
noteable_id = nil
commit_id = nil
if noteable.is_a?(Commit)
commit_id = noteable.id
else
noteable_id = noteable.id
end
2016-08-24 12:49:21 +05:30
attrs.reverse_merge!(
2017-08-17 22:00:37 +05:30
project: noteable.project,
recipient_id: recipient_id,
reply_key: reply_key,
noteable_type: noteable.class.name,
noteable_id: noteable_id,
2017-09-10 17:25:29 +05:30
commit_id: commit_id
2015-09-25 12:07:36 +05:30
)
2016-08-24 12:49:21 +05:30
create(attrs)
2015-09-25 12:07:36 +05:30
end
2017-08-17 22:00:37 +05:30
def record_note(note, recipient_id, reply_key = self.reply_key, attrs = {})
2019-03-02 22:35:43 +05:30
attrs[:in_reply_to_discussion_id] = note.discussion_id if note.part_of_discussion?
2016-08-24 12:49:21 +05:30
record(note.noteable, recipient_id, reply_key, attrs)
2015-09-25 12:07:36 +05:30
end
end
def unsubscribable?
2018-03-17 18:26:18 +05:30
!(for_commit? || for_snippet?)
end
2015-09-25 12:07:36 +05:30
def for_commit?
noteable_type == "Commit"
end
2018-03-17 18:26:18 +05:30
def for_snippet?
noteable_type.end_with?('Snippet')
end
2015-09-25 12:07:36 +05:30
def noteable
if for_commit?
project.commit(commit_id) rescue nil
else
super
end
end
2016-08-24 12:49:21 +05:30
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
def to_param
self.reply_key
end
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
def create_reply(message, dryrun: false)
klass = dryrun ? Notes::BuildService : Notes::CreateService
klass.new(self.project, self.recipient, reply_params.merge(note: message)).execute
2016-08-24 12:49:21 +05:30
end
private
2017-08-17 22:00:37 +05:30
def reply_params
2019-03-02 22:35:43 +05:30
{
2017-08-17 22:00:37 +05:30
noteable_type: self.noteable_type,
noteable_id: self.noteable_id,
2019-03-02 22:35:43 +05:30
commit_id: self.commit_id,
in_reply_to_discussion_id: self.in_reply_to_discussion_id
2017-08-17 22:00:37 +05:30
}
end
2016-08-24 12:49:21 +05:30
def note_valid
2017-08-17 22:00:37 +05:30
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-08-24 12:49:21 +05:30
end
def keep_around_commit
project.repository.keep_around(self.commit_id)
end
2015-09-25 12:07:36 +05:30
end