BigW Consortium Gitlab

sent_notification.rb 2.64 KB
Newer Older
Douwe Maan committed
1
class SentNotification < ActiveRecord::Base
2 3
  serialize :position, Gitlab::Diff::Position

Douwe Maan committed
4 5 6 7
  belongs_to :project
  belongs_to :noteable, polymorphic: true
  belongs_to :recipient, class_name: "User"

Valery Sizov committed
8 9
  validates :project, :recipient, :reply_key, presence: true
  validates :reply_key, uniqueness: true
Douwe Maan committed
10 11
  validates :noteable_id, presence: true, unless: :for_commit?
  validates :commit_id, presence: true, if: :for_commit?
12
  validate :note_valid
Douwe Maan committed
13

14 15
  after_save :keep_around_commit

16
  class << self
17 18 19 20
    def reply_key
      SecureRandom.hex(16)
    end

21 22 23 24
    def for(reply_key)
      find_by(reply_key: reply_key)
    end

25
    def record(noteable, recipient_id, reply_key, attrs = {})
26 27 28 29 30 31 32 33 34 35
      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

36
      attrs.reverse_merge!(
37 38 39 40 41 42 43
        project:        noteable.project,
        noteable_type:  noteable.class.name,
        noteable_id:    noteable_id,
        commit_id:      commit_id,
        recipient_id:   recipient_id,
        reply_key:      reply_key
      )
44

45
      create(attrs)
46 47
    end

48 49 50
    def record_note(note, recipient_id, reply_key, attrs = {})
      if note.diff_note?
        attrs[:note_type] = note.type
51

52 53 54 55
        attrs.merge!(note.diff_attributes)
      end

      record(note.noteable, recipient_id, reply_key, attrs)
56
    end
Douwe Maan committed
57 58
  end

59
  def unsubscribable?
60 61 62
    !for_commit?
  end

Douwe Maan committed
63 64 65 66 67 68
  def for_commit?
    noteable_type == "Commit"
  end

  def noteable
    if for_commit?
Douwe Maan committed
69
      project.commit(commit_id) rescue nil
Douwe Maan committed
70 71 72 73
    else
      super
    end
  end
74

75 76 77 78 79 80 81 82 83 84 85 86 87
  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

88 89 90
  def to_param
    self.reply_key
  end
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  def note_attributes
    {
      project:        self.project,
      author:         self.recipient,
      type:           self.note_type,
      noteable_type:  self.noteable_type,
      noteable_id:    self.noteable_id,
      commit_id:      self.commit_id,
      line_code:      self.line_code,
      position:       self.position.to_json
    }
  end

  def create_note(note)
    Notes::CreateService.new(
      self.project,
      self.recipient,
      self.note_attributes.merge(note: note)
    ).execute
  end

113 114
  private

115 116 117 118
  def note_valid
    Note.new(note_attributes.merge(note: "Test")).valid?
  end

119 120 121
  def keep_around_commit
    project.repository.keep_around(self.commit_id)
  end
Douwe Maan committed
122
end