gitlab-org--gitlab-foss/app/models/sent_notification.rb

71 lines
1.5 KiB
Ruby
Raw Normal View History

2015-08-18 22:46:36 +00:00
class SentNotification < ActiveRecord::Base
belongs_to :project
belongs_to :noteable, polymorphic: true
belongs_to :recipient, class_name: "User"
2015-11-25 16:18:44 +00:00
validates :project, :recipient, :reply_key, presence: true
validates :reply_key, 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?
2015-12-02 00:30:01 +00:00
validates :line_code, line_code: true, allow_blank: true
2015-08-18 22:46:36 +00:00
class << self
def reply_key
SecureRandom.hex(16)
end
def for(reply_key)
find_by(reply_key: reply_key)
end
def record(noteable, recipient_id, reply_key, params = {})
return unless reply_key
noteable_id = nil
commit_id = nil
if noteable.is_a?(Commit)
commit_id = noteable.id
else
noteable_id = noteable.id
end
params.reverse_merge!(
project: noteable.project,
noteable_type: noteable.class.name,
noteable_id: noteable_id,
commit_id: commit_id,
recipient_id: recipient_id,
reply_key: reply_key
)
create(params)
end
def record_note(note, recipient_id, reply_key, params = {})
params[:line_code] = note.line_code
record(note.noteable, recipient_id, reply_key, params)
end
2015-08-18 22:46:36 +00:00
end
2016-01-11 13:23:45 +00:00
def unsubscribable?
!for_commit?
end
2015-08-18 22:46:36 +00:00
def for_commit?
noteable_type == "Commit"
end
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
def to_param
self.reply_key
end
2015-08-18 22:46:36 +00:00
end