BigW Consortium Gitlab

notification_setting.rb 1.93 KB
Newer Older
1
class NotificationSetting < ActiveRecord::Base
2 3 4 5
  include IgnorableColumn

  ignore_column :events

6
  enum level: { global: 3, watch: 2, mention: 4, participating: 1, disabled: 0, custom: 5 }
7 8 9

  default_value_for :level, NotificationSetting.levels[:global]

10
  belongs_to :user
11
  belongs_to :source, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
12
  belongs_to :project, foreign_key: 'source_id'
13 14 15 16 17 18

  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 }
19 20

  scope :for_groups, -> { where(source_type: 'Namespace') }
21 22 23 24 25

  # Exclude projects not included by the Project model's default scope (those that are
  # pending delete).
  #
  scope :for_projects, -> do
26
    includes(:project).references(:projects).where(source_type: 'Project').where.not(projects: { id: nil, pending_delete: true })
27
  end
28

29 30 31 32 33 34 35 36 37 38
  EMAIL_EVENTS = [
    :new_note,
    :new_issue,
    :reopen_issue,
    :close_issue,
    :reassign_issue,
    :new_merge_request,
    :reopen_merge_request,
    :close_merge_request,
    :reassign_merge_request,
39 40 41
    :merge_merge_request,
    :failed_pipeline,
    :success_pipeline
42
  ].freeze
43

44 45
  EXCLUDED_WATCHER_EVENTS = [
    :success_pipeline
46
  ].freeze
47

48 49 50 51 52 53 54 55 56
  def self.find_or_create_for(source)
    setting = find_or_initialize_by(source: source)

    unless setting.persisted?
      setting.save
    end

    setting
  end
57

Sean McGivern committed
58 59 60 61
  # 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
62
    bool = super
Sean McGivern committed
63 64 65

    bool.nil? || bool
  end
66 67 68
  alias_method :failed_pipeline?, :failed_pipeline

  def event_enabled?(event)
69
    respond_to?(event) && !!public_send(event) # rubocop:disable GitlabSecurity/PublicSend
70
  end
71
end