Class: SentNotification

Inherits:
ApplicationRecord show all
Extended by:
SuppressCompositePrimaryKeyWarning
Includes:
EachBatch, PartitionedTable
Defined in:
app/models/sent_notification.rb

Constant Summary collapse

INVALID_NOTEABLE =
Class.new(StandardError)
REPLY_KEY_BYTE_SIZE =
16
INTEGER_CONVERT_BASE =
36
BASE36_REGEX =
/[0-9a-z]/
PARTITIONED_REPLY_KEY_REGEX =

Email reply key is in the form: <base36-partition-id>-<base36-reply-key>

/(?<partition>#{BASE36_REGEX}{1,4})-(?<reply_key>#{BASE36_REGEX}{25})/
LEGACY_REPLY_KEY_REGEX =
/(?<legacy_key>[a-f\d]{32})/
FULL_REPLY_KEY_REGEX =
/(?:(#{LEGACY_REPLY_KEY_REGEX})|(#{PARTITIONED_REPLY_KEY_REGEX}))/
PARTITION_DURATION =
2.months
RETENTION_PERIOD =
2.years

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, 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 Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.for(reply_key) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/sent_notification.rb', line 74

def for(reply_key)
  matches = FULL_REPLY_KEY_REGEX.match(reply_key)
  return unless matches

  result = if matches[:reply_key]
             partition_result = where(
               partition: matches[:partition], reply_key: matches[:reply_key]
             ).to_a

             if partition_result.any?
               partition_result
             else
               where(reply_key: matches[:reply_key]).to_a
             end
           else
             where(reply_key: matches[:legacy_key]).to_a
           end

  # We don't expect collisions, but in the unlikely case of one, behave like the record has been deleted
  # Discussed in https://gitlab.com/gitlab-org/gitlab/-/issues/577844#note_2838135886
  result.one? ? result.first : nil
end

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/sent_notification.rb', line 97

def record(noteable, recipient_id, 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::SessionMap.current(load_balancer).without_sticky_writes do
    create(attrs)
  end
end

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



123
124
125
126
127
# File 'app/models/sent_notification.rb', line 123

def record_note(note, recipient_id, 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, attrs)
end

.reply_keyObject



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

def reply_key
  # Adding leading 0 to make the key size stable. 25 is the max we can get with 16 bytes
  SecureRandom.random_number(2**(REPLY_KEY_BYTE_SIZE * 8)).to_s(INTEGER_CONVERT_BASE).rjust(25, '0')
end

Instance Method Details

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



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/models/sent_notification.rb', line 158

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(project,
    recipient,
    params
  ).execute
end

#for_commit?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'app/models/sent_notification.rb', line 134

def for_commit?
  noteable_type == "Commit"
end

#for_snippet?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'app/models/sent_notification.rb', line 138

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

#noteableObject



142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/sent_notification.rb', line 142

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

#partitioned_reply_keyObject



172
173
174
175
176
177
178
# File 'app/models/sent_notification.rb', line 172

def partitioned_reply_key
  return reply_key unless persisted?

  encoded_partition = partition.to_s(INTEGER_CONVERT_BASE)

  "#{encoded_partition}-#{reply_key}"
end

#to_paramObject



154
155
156
# File 'app/models/sent_notification.rb', line 154

def to_param
  partitioned_reply_key
end

#unsubscribable?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'app/models/sent_notification.rb', line 130

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