2016-03-28 07:41:00 -04:00
|
|
|
class NotificationSetting < ActiveRecord::Base
|
2017-07-28 05:44:33 -04:00
|
|
|
include IgnorableColumn
|
|
|
|
|
|
|
|
ignore_column :events
|
|
|
|
|
2016-05-31 16:51:46 -04:00
|
|
|
enum level: { global: 3, watch: 2, mention: 4, participating: 1, 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 }
|
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
|
2017-06-15 08:06:49 -04:00
|
|
|
includes(:project).references(:projects).where(source_type: 'Project').where.not(projects: { id: nil, pending_delete: true })
|
2016-07-07 07:41:48 -04:00
|
|
|
end
|
2016-03-28 12:17:42 -04:00
|
|
|
|
2016-05-31 16:51:46 -04:00
|
|
|
EMAIL_EVENTS = [
|
|
|
|
:new_note,
|
|
|
|
:new_issue,
|
|
|
|
:reopen_issue,
|
|
|
|
:close_issue,
|
|
|
|
:reassign_issue,
|
|
|
|
: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,
|
2016-09-14 07:42:16 -04:00
|
|
|
:merge_merge_request,
|
|
|
|
:failed_pipeline,
|
|
|
|
:success_pipeline
|
2017-02-21 18:32:18 -05:00
|
|
|
].freeze
|
2016-05-31 16:51:46 -04:00
|
|
|
|
2017-07-25 07:56:09 -04:00
|
|
|
EXCLUDED_PARTICIPATING_EVENTS = [
|
2017-01-05 06:31:11 -05:00
|
|
|
:success_pipeline
|
2017-02-21 18:32:18 -05:00
|
|
|
].freeze
|
2017-01-05 06:31:11 -05:00
|
|
|
|
2017-07-25 07:56:09 -04:00
|
|
|
EXCLUDED_WATCHER_EVENTS = [
|
|
|
|
:push_to_merge_request
|
|
|
|
].push(*EXCLUDED_PARTICIPATING_EVENTS).freeze
|
|
|
|
|
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
|
|
|
|
2017-03-30 07:29:52 -04: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 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
|
|
|
|
|
|
|
|
def event_enabled?(event)
|
2017-08-03 22:20:34 -04:00
|
|
|
respond_to?(event) && !!public_send(event) # rubocop:disable GitlabSecurity/PublicSend
|
2017-06-08 12:10:35 -04:00
|
|
|
end
|
2016-03-28 07:41:00 -04:00
|
|
|
end
|