debian-mirror-gitlab/app/models/notification_setting.rb

103 lines
2.6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class NotificationSetting < ApplicationRecord
2019-02-15 15:39:39 +05:30
enum level: { global: 3, watch: 2, participating: 1, mention: 4, disabled: 0, custom: 5 }
2016-06-02 11:05:42 +05:30
default_value_for :level, NotificationSetting.levels[:global]
belongs_to :user
2017-09-10 17:25:29 +05:30
belongs_to :source, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
2016-08-24 12:49:21 +05:30
belongs_to :project, foreign_key: 'source_id'
2016-06-02 11:05:42 +05:30
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 }
scope :for_groups, -> { where(source_type: 'Namespace') }
2016-08-24 12:49:21 +05:30
# Exclude projects not included by the Project model's default scope (those that are
# pending delete).
#
scope :for_projects, -> do
2020-03-09 13:42:32 +05:30
includes(:project).references(:projects)
.where(source_type: 'Project')
.where.not(projects: { id: nil })
.where.not(projects: { pending_delete: true })
2016-08-24 12:49:21 +05:30
end
2016-06-02 11:05:42 +05:30
2020-03-09 13:42:32 +05:30
scope :preload_source_route, -> { preload(source: [:route]) }
2016-06-22 15:30:34 +05:30
EMAIL_EVENTS = [
2019-12-21 20:55:43 +05:30
:new_release,
2016-06-22 15:30:34 +05:30
:new_note,
:new_issue,
:reopen_issue,
:close_issue,
:reassign_issue,
2018-11-18 11:00:15 +05:30
:issue_due,
2016-06-22 15:30:34 +05:30
:new_merge_request,
2018-05-09 12:01:36 +05:30
:push_to_merge_request,
2016-06-22 15:30:34 +05:30
:reopen_merge_request,
:close_merge_request,
:reassign_merge_request,
2017-08-17 22:00:37 +05:30
:merge_merge_request,
:failed_pipeline,
2020-04-08 14:13:33 +05:30
:fixed_pipeline,
2017-08-17 22:00:37 +05:30
:success_pipeline
].freeze
2018-11-20 20:47:30 +05:30
# Update unfound_translations.rb when events are changed
def self.email_events(source = nil)
EMAIL_EVENTS
end
2019-12-21 20:55:43 +05:30
def self.allowed_fields(source = nil)
NotificationSetting.email_events(source).dup + %i(level notification_email)
end
2018-11-20 20:47:30 +05:30
def email_events
self.class.email_events(source)
end
2018-05-09 12:01:36 +05:30
EXCLUDED_WATCHER_EVENTS = [
2018-10-15 14:42:47 +05:30
:push_to_merge_request,
2019-09-04 21:01:54 +05:30
:issue_due,
:success_pipeline
].freeze
2018-05-09 12:01:36 +05:30
2016-06-02 11:05:42 +05:30
def self.find_or_create_for(source)
setting = find_or_initialize_by(source: source)
unless setting.persisted?
setting.save
end
setting
end
2016-06-22 15:30:34 +05:30
2020-04-08 14:13:33 +05:30
# 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-08-17 22:00:37 +05:30
def failed_pipeline
bool = super
bool.nil? || bool
end
2017-09-10 17:25:29 +05:30
alias_method :failed_pipeline?, :failed_pipeline
2020-04-08 14:13:33 +05:30
def fixed_pipeline
bool = super
bool.nil? || bool
end
alias_method :fixed_pipeline?, :fixed_pipeline
2017-09-10 17:25:29 +05:30
def event_enabled?(event)
2018-03-17 18:26:18 +05:30
respond_to?(event) && !!public_send(event) # rubocop:disable GitlabSecurity/PublicSend
2017-09-10 17:25:29 +05:30
end
2016-06-02 11:05:42 +05:30
end
2019-12-04 20:38:33 +05:30
NotificationSetting.prepend_if_ee('EE::NotificationSetting')