BigW Consortium Gitlab

notification_recipient.rb 3.23 KB
Newer Older
1
class NotificationRecipient
2
  attr_reader :user, :type
http://jneen.net/ committed
3 4 5 6 7 8 9
  def initialize(
    user, type,
    custom_action: nil,
    target: nil,
    acting_user: nil,
    project: nil
  )
10 11 12
    @custom_action = custom_action
    @acting_user = acting_user
    @target = target
13
    @project = project || @target&.project
14 15 16 17 18 19 20 21 22 23 24 25 26 27
    @user = user
    @type = type
  end

  def notification_setting
    @notification_setting ||= find_notification_setting
  end

  def raw_notification_level
    notification_setting&.level&.to_sym
  end

  def notification_level
    # custom is treated the same as watch if it's enabled - otherwise it's
http://jneen.net/ committed
28 29
    # set to :custom, meaning to send exactly when our type is :participating
    # or :mention.
30 31 32
    @notification_level ||=
      case raw_notification_level
      when :custom
33
        if @custom_action && notification_setting&.event_enabled?(@custom_action)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
          :watch
        else
          :custom
        end
      else
        raw_notification_level
      end
  end

  def notifiable?
    return false unless has_access?
    return false if own_activity?

    return true if @type == :subscription

    return false if notification_level.nil? || notification_level == :disabled

    return %i[participating mention].include?(@type) if notification_level == :custom

    return false if %i[watch participating].include?(notification_level) && excluded_watcher_action?

55
    return false unless NotificationSetting.levels[notification_level] <= NotificationSetting.levels[@type]
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    return false if unsubscribed?

    true
  end

  def unsubscribed?
    return false unless @target
    return false unless @target.respond_to?(:subscriptions)

    subscription = @target.subscriptions.find_by_user_id(@user.id)
    subscription && !subscription.subscribed
  end

  def own_activity?
    return false unless @acting_user
    return false if @acting_user.notified_of_own_activity?

    user == @acting_user
  end

  def has_access?
    DeclarativePolicy.subject_scope do
79 80 81
      return false unless user.can?(:receive_notifications)
      return false if @project && !user.can?(:read_project, @project)

82
      return true unless read_ability
83 84
      return true unless DeclarativePolicy.has_policy?(@target)

85
      user.can?(read_ability, @target)
86 87 88 89 90 91 92 93 94 95 96 97
    end
  end

  def excluded_watcher_action?
    return false unless @custom_action
    return false if raw_notification_level == :custom

    NotificationSetting::EXCLUDED_WATCHER_EVENTS.include?(@custom_action)
  end

  private

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  def read_ability
    return @read_ability if instance_variable_defined?(:@read_ability)

    @read_ability =
      case @target
      when Issuable
        :"read_#{@target.to_ability_name}"
      when Ci::Pipeline
        :read_build # We have build trace in pipeline emails
      when ActiveRecord::Base
        :"read_#{@target.class.model_name.name.underscore}"
      else
        nil
      end
  end

114 115 116 117 118 119 120 121 122 123 124 125
  def find_notification_setting
    project_setting = @project && user.notification_settings_for(@project)

    return project_setting unless project_setting.nil? || project_setting.global?

    group_setting = @project&.group && user.notification_settings_for(@project.group)

    return group_setting unless group_setting.nil? || group_setting.global?

    user.global_notification_setting
  end
end