Class: SentNotification

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

Constant Summary

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.for(reply_key) ⇒ Object



26
27
28
# File 'app/models/sent_notification.rb', line 26

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

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/sent_notification.rb', line 30

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
  )

  # Non-sticky write is used as `.record` is only used in ActionMailer
  # where there are no queries to SentNotification.
  ::Gitlab::Database::LoadBalancing::Session.without_sticky_writes do
    create(attrs)
  end
end

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



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

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? || note.can_be_discussion_note?

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

.reply_keyObject



22
23
24
# File 'app/models/sent_notification.rb', line 22

def reply_key
  SecureRandom.hex(16)
end

Instance Method Details

#create_reply(message, external_author = nil, dryrun: false) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/sent_notification.rb', line 91

def create_reply(message, external_author = nil, dryrun: false)
  klass = dryrun ? Notes::BuildService : Notes::CreateService
  params = reply_params.merge(
    note: message
  )

  params[:external_author] = external_author if external_author.present?

  klass.new(self.project,
    self.recipient,
    params
  ).execute
end

#for_commit?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/sent_notification.rb', line 67

def for_commit?
  noteable_type == "Commit"
end

#for_snippet?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/sent_notification.rb', line 71

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

#noteableObject



75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/sent_notification.rb', line 75

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

#to_paramObject



87
88
89
# File 'app/models/sent_notification.rb', line 87

def to_param
  self.reply_key
end

#unsubscribable?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/sent_notification.rb', line 63

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