Class: NotificationSetting

Inherits:
ApplicationRecord show all
Includes:
EachBatch, FromUnion
Defined in:
app/models/notification_setting.rb

Constant Summary collapse

EMAIL_EVENTS =

NOTE: Applicable unfound_translations.rb also needs to be updated when below events are changed.

[
  :new_release,
  :new_note,
  :new_issue,
  :reopen_issue,
  :close_issue,
  :reassign_issue,
  :issue_due,
  :new_merge_request,
  :push_to_merge_request,
  :reopen_merge_request,
  :close_merge_request,
  :reassign_merge_request,
  :change_reviewer_merge_request,
  :merge_merge_request,
  :failed_pipeline,
  :fixed_pipeline,
  :success_pipeline,
  :moved_project,
  :merge_when_pipeline_succeeds
].freeze
EXCLUDED_WATCHER_EVENTS =
[
  :push_to_merge_request,
  :issue_due,
  :success_pipeline,
  :approver
].freeze

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, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, nullable_column?, 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 ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.allowed_fields(source = nil) ⇒ Object



64
65
66
# File 'app/models/notification_setting.rb', line 64

def self.allowed_fields(source = nil)
  NotificationSetting.email_events(source).dup + %i[level notification_email]
end

.email_events(source = nil) ⇒ Object



60
61
62
# File 'app/models/notification_setting.rb', line 60

def self.email_events(source = nil)
  EMAIL_EVENTS
end

.find_or_create_for(source) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'app/models/notification_setting.rb', line 79

def self.find_or_create_for(source)
  setting = find_or_initialize_by(source: source)

  unless setting.persisted?
    setting.save
  end

  setting
end

.reset_email_for_user!(email) ⇒ Object



89
90
91
92
93
94
95
96
# File 'app/models/notification_setting.rb', line 89

def self.reset_email_for_user!(email)
  where(
    user_id: email.user_id,
    notification_email: email.email
  ).each_batch(of: 500) do |relation|
    relation.update_all(notification_email: nil)
  end
end

Instance Method Details

#email_eventsObject



68
69
70
# File 'app/models/notification_setting.rb', line 68

def email_events
  self.class.email_events(source)
end

#event_enabled?(event) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
# File 'app/models/notification_setting.rb', line 115

def event_enabled?(event)
  # We override these two attributes, so we can't use read_attribute
  return failed_pipeline if event.to_sym == :failed_pipeline
  return fixed_pipeline if event.to_sym == :fixed_pipeline

  has_attribute?(event) && !!read_attribute(event)
end

#failed_pipelineObject Also known as: failed_pipeline?

Allow people to receive both failed pipeline/fixed pipeline notifications if they already have custom notifications enabled, as these are more like mentions than the other custom settings.



101
102
103
104
105
# File 'app/models/notification_setting.rb', line 101

def failed_pipeline
  bool = super

  bool.nil? || bool
end

#fixed_pipelineObject Also known as: fixed_pipeline?



108
109
110
111
112
# File 'app/models/notification_setting.rb', line 108

def fixed_pipeline
  bool = super

  bool.nil? || bool
end

#notification_email_verifiedObject



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

def notification_email_verified
  return if user.temp_oauth_email?
  return if notification_email.blank?

  errors.add(:notification_email, _("must be an email you have verified")) unless user.verified_emails.include?(notification_email)
end