2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class NotificationSetting < ApplicationRecord
|
2021-04-20 02:09:33 -04:00
|
|
|
include FromUnion
|
|
|
|
|
2018-11-30 21:16:03 -05:00
|
|
|
enum level: { global: 3, watch: 2, participating: 1, mention: 4, disabled: 0, custom: 5 }
|
2016-04-08 16:24:27 -04:00
|
|
|
|
|
|
|
default_value_for :level, NotificationSetting.levels[:global]
|
|
|
|
|
2016-03-28 07:41:00 -04:00
|
|
|
belongs_to :user
|
2017-06-02 08:29:30 -04:00
|
|
|
belongs_to :source, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
|
2016-07-07 07:41:48 -04:00
|
|
|
belongs_to :project, foreign_key: 'source_id'
|
2016-03-28 07:41:00 -04:00
|
|
|
|
|
|
|
validates :user, presence: true
|
|
|
|
validates :level, presence: true
|
|
|
|
validates :user_id, uniqueness: { scope: [:source_type, :source_id],
|
|
|
|
message: "already exists in source",
|
|
|
|
allow_nil: true }
|
2021-08-17 08:08:42 -04:00
|
|
|
validate :notification_email_verified, if: :notification_email_changed?
|
2016-03-28 12:17:42 -04:00
|
|
|
|
|
|
|
scope :for_groups, -> { where(source_type: 'Namespace') }
|
2016-07-07 07:41:48 -04:00
|
|
|
|
|
|
|
# Exclude projects not included by the Project model's default scope (those that are
|
|
|
|
# pending delete).
|
|
|
|
#
|
|
|
|
scope :for_projects, -> do
|
2020-01-29 13:08:47 -05:00
|
|
|
includes(:project).references(:projects)
|
|
|
|
.where(source_type: 'Project')
|
|
|
|
.where.not(projects: { id: nil })
|
|
|
|
.where.not(projects: { pending_delete: true })
|
2016-07-07 07:41:48 -04:00
|
|
|
end
|
2016-03-28 12:17:42 -04:00
|
|
|
|
2020-02-05 22:08:47 -05:00
|
|
|
scope :preload_source_route, -> { preload(source: [:route]) }
|
|
|
|
|
2021-04-13 14:11:28 -04:00
|
|
|
scope :order_by_id_asc, -> { order(id: :asc) }
|
|
|
|
|
2020-12-02 07:09:46 -05:00
|
|
|
# NOTE: Applicable unfound_translations.rb also needs to be updated when below events are changed.
|
2016-05-31 16:51:46 -04:00
|
|
|
EMAIL_EVENTS = [
|
2019-10-17 14:08:05 -04:00
|
|
|
:new_release,
|
2016-05-31 16:51:46 -04:00
|
|
|
:new_note,
|
|
|
|
:new_issue,
|
|
|
|
:reopen_issue,
|
|
|
|
:close_issue,
|
|
|
|
:reassign_issue,
|
2018-07-10 10:29:27 -04:00
|
|
|
:issue_due,
|
2016-05-31 16:51:46 -04:00
|
|
|
:new_merge_request,
|
2017-07-25 07:56:09 -04:00
|
|
|
:push_to_merge_request,
|
2016-05-31 16:51:46 -04:00
|
|
|
:reopen_merge_request,
|
|
|
|
:close_merge_request,
|
|
|
|
:reassign_merge_request,
|
2020-09-21 08:09:34 -04:00
|
|
|
:change_reviewer_merge_request,
|
2016-09-14 07:42:16 -04:00
|
|
|
:merge_merge_request,
|
|
|
|
:failed_pipeline,
|
2020-02-27 13:09:21 -05:00
|
|
|
:fixed_pipeline,
|
2020-07-27 23:09:35 -04:00
|
|
|
:success_pipeline,
|
2021-02-19 07:11:06 -05:00
|
|
|
:moved_project,
|
|
|
|
:merge_when_pipeline_succeeds
|
2017-02-21 18:32:18 -05:00
|
|
|
].freeze
|
2016-05-31 16:51:46 -04:00
|
|
|
|
2018-07-22 22:42:19 -04:00
|
|
|
def self.email_events(source = nil)
|
|
|
|
EMAIL_EVENTS
|
|
|
|
end
|
|
|
|
|
2019-10-18 14:06:21 -04:00
|
|
|
def self.allowed_fields(source = nil)
|
|
|
|
NotificationSetting.email_events(source).dup + %i(level notification_email)
|
|
|
|
end
|
|
|
|
|
2018-07-22 22:42:19 -04:00
|
|
|
def email_events
|
|
|
|
self.class.email_events(source)
|
|
|
|
end
|
|
|
|
|
2017-07-25 07:56:09 -04:00
|
|
|
EXCLUDED_WATCHER_EVENTS = [
|
2018-03-30 08:25:46 -04:00
|
|
|
:push_to_merge_request,
|
2019-06-05 03:37:08 -04:00
|
|
|
:issue_due,
|
|
|
|
:success_pipeline
|
|
|
|
].freeze
|
2017-07-25 07:56:09 -04:00
|
|
|
|
2016-03-29 07:52:42 -04:00
|
|
|
def self.find_or_create_for(source)
|
|
|
|
setting = find_or_initialize_by(source: source)
|
|
|
|
|
|
|
|
unless setting.persisted?
|
|
|
|
setting.save
|
|
|
|
end
|
|
|
|
|
|
|
|
setting
|
|
|
|
end
|
2016-05-31 16:51:46 -04:00
|
|
|
|
2020-02-27 13:09:21 -05:00
|
|
|
# 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.
|
2017-03-30 07:29:52 -04:00
|
|
|
def failed_pipeline
|
2017-06-08 12:10:35 -04:00
|
|
|
bool = super
|
2017-03-30 07:29:52 -04:00
|
|
|
|
|
|
|
bool.nil? || bool
|
|
|
|
end
|
2017-06-08 12:10:35 -04:00
|
|
|
alias_method :failed_pipeline?, :failed_pipeline
|
|
|
|
|
2020-02-27 13:09:21 -05:00
|
|
|
def fixed_pipeline
|
|
|
|
bool = super
|
|
|
|
|
|
|
|
bool.nil? || bool
|
|
|
|
end
|
|
|
|
alias_method :fixed_pipeline?, :fixed_pipeline
|
|
|
|
|
2017-06-08 12:10:35 -04:00
|
|
|
def event_enabled?(event)
|
2020-08-17 08:10:12 -04:00
|
|
|
# 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)
|
2017-06-08 12:10:35 -04:00
|
|
|
end
|
2020-05-27 17:08:05 -04:00
|
|
|
|
2021-08-17 08:08:42 -04:00
|
|
|
def notification_email_verified
|
2020-05-27 17:08:05 -04:00
|
|
|
return if user.temp_oauth_email?
|
|
|
|
return if notification_email.empty?
|
|
|
|
|
2021-08-17 08:08:42 -04:00
|
|
|
errors.add(:notification_email, _("must be an email you have verified")) unless user.verified_emails.include?(notification_email)
|
2020-05-27 17:08:05 -04:00
|
|
|
end
|
2016-03-28 07:41:00 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
NotificationSetting.prepend_mod_with('NotificationSetting')
|