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

85 lines
2.2 KiB
Ruby
Raw Normal View History

2016-06-02 11:05:42 +05:30
class NotificationSetting < ActiveRecord::Base
2016-06-22 15:30:34 +05:30
enum level: { global: 3, watch: 2, mention: 4, participating: 1, disabled: 0, custom: 5 }
2016-06-02 11:05:42 +05:30
default_value_for :level, NotificationSetting.levels[:global]
belongs_to :user
belongs_to :source, polymorphic: true
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
includes(:project).references(:projects).where(source_type: 'Project').where.not(projects: { id: nil })
end
2016-06-02 11:05:42 +05:30
2016-06-22 15:30:34 +05:30
EMAIL_EVENTS = [
:new_note,
:new_issue,
:reopen_issue,
:close_issue,
:reassign_issue,
:new_merge_request,
:reopen_merge_request,
:close_merge_request,
:reassign_merge_request,
2017-08-17 22:00:37 +05:30
:merge_merge_request,
:failed_pipeline,
:success_pipeline
].freeze
EXCLUDED_WATCHER_EVENTS = [
:success_pipeline
].freeze
2016-06-22 15:30:34 +05:30
store :events, accessors: EMAIL_EVENTS, coder: JSON
before_create :set_events
before_save :events_to_boolean
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
# Set all event attributes to false when level is not custom or being initialized for UX reasons
def set_events
return if custom?
2017-08-17 22:00:37 +05:30
self.events = {}
2016-06-22 15:30:34 +05:30
end
# Validates store accessors values as boolean
# It is a text field so it does not cast correct boolean values in JSON
def events_to_boolean
EMAIL_EVENTS.each do |event|
2017-08-17 22:00:37 +05:30
bool = ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(public_send(event))
events[event] = bool
2016-06-22 15:30:34 +05:30
end
end
2017-08-17 22:00:37 +05:30
# 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
bool = super
bool.nil? || bool
end
2016-06-02 11:05:42 +05:30
end