Class: SentNotification

Inherits:
ApplicationRecord show all
Defined in:
app/models/sent_notification.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.for(reply_key) ⇒ Object


24
25
26
# File 'app/models/sent_notification.rb', line 24

def for(reply_key)
  find_by(reply_key: reply_key)
end

.record(noteable, recipient_id, reply_key = self.reply_key, attrs = {}) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/sent_notification.rb', line 28

def record(noteable, recipient_id, reply_key = self.reply_key, attrs = {})
  noteable_id = nil
  commit_id = nil
  if noteable.is_a?(Commit)
    commit_id = noteable.id
  else
    noteable_id = noteable.id
  end

  attrs.reverse_merge!(
    project: noteable.project,
    recipient_id: recipient_id,
    reply_key: reply_key,

    noteable_type: noteable.class.name,
    noteable_id: noteable_id,
    commit_id: commit_id
  )

  create(attrs)
end

.record_note(note, recipient_id, reply_key = self.reply_key, attrs = {}) ⇒ Object


50
51
52
53
54
# File 'app/models/sent_notification.rb', line 50

def record_note(note, recipient_id, reply_key = self.reply_key, attrs = {})
  attrs[:in_reply_to_discussion_id] = note.discussion_id if note.part_of_discussion?

  record(note.noteable, recipient_id, reply_key, attrs)
end

.reply_keyObject


20
21
22
# File 'app/models/sent_notification.rb', line 20

def reply_key
  SecureRandom.hex(16)
end

Instance Method Details

#create_reply(message, dryrun: false) ⇒ Object


96
97
98
99
# File 'app/models/sent_notification.rb', line 96

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

#for_commit?Boolean

Returns:

  • (Boolean)

61
62
63
# File 'app/models/sent_notification.rb', line 61

def for_commit?
  noteable_type == "Commit"
end

#for_snippet?Boolean

Returns:

  • (Boolean)

65
66
67
# File 'app/models/sent_notification.rb', line 65

def for_snippet?
  noteable_type.end_with?('Snippet')
end

#noteableObject


69
70
71
72
73
74
75
# File 'app/models/sent_notification.rb', line 69

def noteable
  if for_commit?
    project.commit(commit_id) rescue nil
  else
    super
  end
end

#position=(new_position) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/sent_notification.rb', line 77

def position=(new_position)
  if new_position.is_a?(String)
    new_position = Gitlab::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)
  else
    new_position = nil
  end

  super(new_position)
end

#to_paramObject


92
93
94
# File 'app/models/sent_notification.rb', line 92

def to_param
  self.reply_key
end

#unsubscribable?Boolean

Returns:

  • (Boolean)

57
58
59
# File 'app/models/sent_notification.rb', line 57

def unsubscribable?
  !(for_commit? || for_snippet?)
end