BigW Consortium Gitlab

sent_notification.rb 3.17 KB
Newer Older
Douwe Maan committed
1
class SentNotification < ActiveRecord::Base
2
  serialize :position, Gitlab::Diff::Position # rubocop:disable Cop/ActiveRecordSerialize
3

Douwe Maan committed
4
  belongs_to :project
5
  belongs_to :noteable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
Douwe Maan committed
6 7
  belongs_to :recipient, class_name: "User"

8 9
  validates :project, :recipient, presence: true
  validates :reply_key, presence: true, uniqueness: true
Douwe Maan committed
10 11
  validates :noteable_id, presence: true, unless: :for_commit?
  validates :commit_id, presence: true, if: :for_commit?
12
  validates :in_reply_to_discussion_id, format: { with: /\A\h{40}\z/, allow_nil: true }
13
  validate :note_valid
Douwe Maan committed
14

15 16
  after_save :keep_around_commit

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

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

Douwe Maan committed
26
    def record(noteable, recipient_id, reply_key = self.reply_key, attrs = {})
27 28 29 30 31 32 33 34
      noteable_id = nil
      commit_id = nil
      if noteable.is_a?(Commit)
        commit_id = noteable.id
      else
        noteable_id = noteable.id
      end

35
      attrs.reverse_merge!(
36 37 38 39 40 41
        project: noteable.project,
        recipient_id: recipient_id,
        reply_key: reply_key,

        noteable_type: noteable.class.name,
        noteable_id: noteable_id,
42
        commit_id: commit_id
43
      )
44

45
      create(attrs)
46 47
    end

Douwe Maan committed
48
    def record_note(note, recipient_id, reply_key = self.reply_key, attrs = {})
49
      attrs[:in_reply_to_discussion_id] = note.discussion_id
50 51

      record(note.noteable, recipient_id, reply_key, attrs)
52
    end
Douwe Maan committed
53 54
  end

55
  def unsubscribable?
56 57 58
    !for_commit?
  end

Douwe Maan committed
59 60 61 62 63 64
  def for_commit?
    noteable_type == "Commit"
  end

  def noteable
    if for_commit?
Douwe Maan committed
65
      project.commit(commit_id) rescue nil
Douwe Maan committed
66 67 68 69
    else
      super
    end
  end
70

71 72 73 74 75 76 77 78 79 80 81 82 83
  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

84 85 86
  def to_param
    self.reply_key
  end
87

Douwe Maan committed
88 89 90 91 92 93 94 95
  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
96 97 98 99
    attrs = {
      noteable_type: self.noteable_type,
      noteable_id: self.noteable_id,
      commit_id: self.commit_id
100 101
    }

102 103 104
    if self.in_reply_to_discussion_id.present?
      attrs[:in_reply_to_discussion_id] = self.in_reply_to_discussion_id
    else
Douwe Maan committed
105 106
      # Remove in GitLab 10.0, when we will not support replying to SentNotifications
      # that don't have `in_reply_to_discussion_id` anymore.
107 108 109 110 111 112 113 114 115 116 117 118
      attrs.merge!(
        type: self.note_type,

        # LegacyDiffNote
        line_code: self.line_code,

        # DiffNote
        position: self.position.to_json
      )
    end

    attrs
119 120 121
  end

  def note_valid
Douwe Maan committed
122 123 124 125 126
    note = create_reply('Test', dryrun: true)

    unless note.valid?
      self.errors.add(:base, "Note parameters are invalid: #{note.errors.full_messages.to_sentence}")
    end
127 128
  end

129 130 131
  def keep_around_commit
    project.repository.keep_around(self.commit_id)
  end
Douwe Maan committed
132
end