2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class NotificationSetting < ApplicationRecord
|
2017-07-28 09:44:33 +00:00
|
|
|
include IgnorableColumn
|
|
|
|
|
|
|
|
ignore_column :events
|
|
|
|
|
2018-12-01 02:16:03 +00:00
|
|
|
enum level: { global: 3, watch: 2, participating: 1, mention: 4, disabled: 0, custom: 5 }
|
2016-04-08 20:24:27 +00:00
|
|
|
|
|
|
|
default_value_for :level, NotificationSetting.levels[:global]
|
|
|
|
|
2016-03-28 11:41:00 +00:00
|
|
|
belongs_to :user
|
2017-06-02 12:29:30 +00:00
|
|
|
belongs_to :source, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
|
2016-07-07 11:41:48 +00:00
|
|
|
belongs_to :project, foreign_key: 'source_id'
|
2016-03-28 11:41:00 +00: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 }
|
2016-03-28 16:17:42 +00:00
|
|
|
|
|
|
|
scope :for_groups, -> { where(source_type: 'Namespace') }
|
2016-07-07 11:41:48 +00:00
|
|
|
|
|
|
|
# Exclude projects not included by the Project model's default scope (those that are
|
|
|
|
# pending delete).
|
|
|
|
#
|
|
|
|
scope :for_projects, -> do
|
2017-06-15 12:06:49 +00:00
|
|
|
includes(:project).references(:projects).where(source_type: 'Project').where.not(projects: { id: nil, pending_delete: true })
|
2016-07-07 11:41:48 +00:00
|
|
|
end
|
2016-03-28 16:17:42 +00:00
|
|
|
|
2016-05-31 20:51:46 +00:00
|
|
|
EMAIL_EVENTS = [
|
|
|
|
:new_note,
|
|
|
|
:new_issue,
|
|
|
|
:reopen_issue,
|
|
|
|
:close_issue,
|
|
|
|
:reassign_issue,
|
2018-07-10 14:29:27 +00:00
|
|
|
:issue_due,
|
2016-05-31 20:51:46 +00:00
|
|
|
:new_merge_request,
|
2017-07-25 11:56:09 +00:00
|
|
|
:push_to_merge_request,
|
2016-05-31 20:51:46 +00:00
|
|
|
:reopen_merge_request,
|
|
|
|
:close_merge_request,
|
|
|
|
:reassign_merge_request,
|
2016-09-14 11:42:16 +00:00
|
|
|
:merge_merge_request,
|
|
|
|
:failed_pipeline,
|
|
|
|
:success_pipeline
|
2017-02-21 23:32:18 +00:00
|
|
|
].freeze
|
2016-05-31 20:51:46 +00:00
|
|
|
|
2018-08-08 06:38:39 +00:00
|
|
|
# Update unfound_translations.rb when events are changed
|
2018-07-23 02:42:19 +00:00
|
|
|
def self.email_events(source = nil)
|
|
|
|
EMAIL_EVENTS
|
|
|
|
end
|
|
|
|
|
|
|
|
def email_events
|
|
|
|
self.class.email_events(source)
|
|
|
|
end
|
|
|
|
|
2017-07-25 11:56:09 +00:00
|
|
|
EXCLUDED_WATCHER_EVENTS = [
|
2018-03-30 12:25:46 +00:00
|
|
|
:push_to_merge_request,
|
2019-06-05 07:37:08 +00:00
|
|
|
:issue_due,
|
|
|
|
:success_pipeline
|
|
|
|
].freeze
|
2017-07-25 11:56:09 +00:00
|
|
|
|
2016-03-29 11:52:42 +00: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 20:51:46 +00:00
|
|
|
|
2017-03-30 11:29:52 +00:00
|
|
|
# Allow people to receive failed pipeline notifications if they already have
|
|
|
|
# custom notifications enabled, as these are more like mentions than the other
|
|
|
|
# custom settings.
|
|
|
|
def failed_pipeline
|
2017-06-08 16:10:35 +00:00
|
|
|
bool = super
|
2017-03-30 11:29:52 +00:00
|
|
|
|
|
|
|
bool.nil? || bool
|
|
|
|
end
|
2017-06-08 16:10:35 +00:00
|
|
|
alias_method :failed_pipeline?, :failed_pipeline
|
|
|
|
|
|
|
|
def event_enabled?(event)
|
2017-08-04 02:20:34 +00:00
|
|
|
respond_to?(event) && !!public_send(event) # rubocop:disable GitlabSecurity/PublicSend
|
2017-06-08 16:10:35 +00:00
|
|
|
end
|
2016-03-28 11:41:00 +00:00
|
|
|
end
|