BigW Consortium Gitlab

notification_setting.rb 2.54 KB
Newer Older
1
class NotificationSetting < ActiveRecord::Base
2
  enum level: { global: 3, watch: 2, mention: 4, participating: 1, disabled: 0, custom: 5 }
3 4 5

  default_value_for :level, NotificationSetting.levels[:global]

6
  belongs_to :user
7
  belongs_to :source, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
8
  belongs_to :project, foreign_key: 'source_id'
9 10 11 12 13 14

  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 }
15 16

  scope :for_groups, -> { where(source_type: 'Namespace') }
17 18 19 20 21

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

25 26 27 28 29 30 31 32 33 34
  EMAIL_EVENTS = [
    :new_note,
    :new_issue,
    :reopen_issue,
    :close_issue,
    :reassign_issue,
    :new_merge_request,
    :reopen_merge_request,
    :close_merge_request,
    :reassign_merge_request,
35 36 37
    :merge_merge_request,
    :failed_pipeline,
    :success_pipeline
38
  ].freeze
39

40 41
  EXCLUDED_WATCHER_EVENTS = [
    :success_pipeline
42
  ].freeze
43

44
  store :events, coder: JSON
45
  before_save :convert_events
46

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

    unless setting.persisted?
      setting.save
    end

    setting
  end
56

57 58 59 60 61
  # 1. Check if this event has a value stored in its database column.
  # 2. If it does, return that value.
  # 3. If it doesn't (the value is nil), return the value from the serialized
  #    JSON hash in `events`.
  (EMAIL_EVENTS - [:failed_pipeline]).each do |event|
62 63
    define_method(event) do
      bool = super()
Sean McGivern committed
64

65
      bool.nil? ? !!events[event] : bool
66
    end
67 68

    alias_method :"#{event}?", event
69
  end
Sean McGivern committed
70 71 72 73 74

  # 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
75
    bool = super
76
    bool = events[:failed_pipeline] if bool.nil?
Sean McGivern committed
77 78 79

    bool.nil? || bool
  end
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  alias_method :failed_pipeline?, :failed_pipeline

  def event_enabled?(event)
    respond_to?(event) && public_send(event)
  end

  def convert_events
    return if events_before_type_cast.nil?

    EMAIL_EVENTS.each do |event|
      write_attribute(event, public_send(event))
    end

    write_attribute(:events, nil)
  end
95
end