Class: NotificationRecipient

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/models/notification_recipient.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, type, opts = {}) ⇒ NotificationRecipient

Returns a new instance of NotificationRecipient.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/notification_recipient.rb', line 8

def initialize(user, type, opts = {})
  unless NotificationSetting.levels.key?(type) || type == :subscription
    raise ArgumentError, "invalid type: #{type.inspect}"
  end

  @custom_action = opts[:custom_action]
  @acting_user = opts[:acting_user]
  @target = opts[:target]
  @project = opts[:project] || default_project
  @group = opts[:group] || @project&.group
  @user = user
  @type = type
  @reason = opts[:reason]
  @skip_read_ability = opts[:skip_read_ability]
end

Instance Attribute Details

#reasonObject (readonly)

Returns the value of attribute reason.



6
7
8
# File 'app/models/notification_recipient.rb', line 6

def reason
  @reason
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'app/models/notification_recipient.rb', line 6

def type
  @type
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'app/models/notification_recipient.rb', line 6

def user
  @user
end

Instance Method Details

#custom_enabled?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
# File 'app/models/notification_recipient.rb', line 66

def custom_enabled?
  return false unless @custom_action
  return false unless notification_setting

  notification_setting.event_enabled?(@custom_action) ||
    # fixed_pipeline is a subset of success_pipeline event
    (@custom_action == :fixed_pipeline &&
     notification_setting.event_enabled?(:success_pipeline))
end

#email_blocked?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
# File 'app/models/notification_recipient.rb', line 99

def email_blocked?
  recipient_email = user.notification_email_for(@group)

  Gitlab::ApplicationRateLimiter.peek(:permanent_email_failure, scope: recipient_email) ||
    Gitlab::ApplicationRateLimiter.peek(:temporary_email_failure, scope: recipient_email)
end

#excluded_watcher_action?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
# File 'app/models/notification_recipient.rb', line 121

def excluded_watcher_action?
  return false unless @type == :watch
  return false unless @custom_action

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

#has_access?Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/notification_recipient.rb', line 106

def has_access?
  DeclarativePolicy.subject_scope do
    break false unless user.can?(:receive_notifications)
    break true if @skip_read_ability

    break false if @target && !user.can?(:read_cross_project)
    break false if @project && !user.can?(:read_project, @project)

    break true unless read_ability
    break true unless DeclarativePolicy.has_policy?(@target)

    user.can?(read_ability, @target)
  end
end

#notifiable?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/notification_recipient.rb', line 32

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

  # even users with :disabled notifications receive manual subscriptions
  return !unsubscribed? if @type == :subscription

  return false unless suitable_notification_level?
  return false if email_blocked?

  # check this last because it's expensive
  # nobody should receive notifications if they've specifically unsubscribed
  # except if they were mentioned.
  return false if @type != :mention && unsubscribed?

  true
end

#notification_levelObject



28
29
30
# File 'app/models/notification_recipient.rb', line 28

def notification_level
  @notification_level ||= notification_setting&.level&.to_sym
end

#notification_settingObject



24
25
26
# File 'app/models/notification_recipient.rb', line 24

def notification_setting
  @notification_setting ||= find_notification_setting
end

#own_activity?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/notification_recipient.rb', line 86

def own_activity?
  return false unless @acting_user

  if user == @acting_user
    # if activity was generated by the same user, change reason to :own_activity
    @reason = NotificationReason::OWN_ACTIVITY
    # If the user wants to be notified, we must return `false`
    !@acting_user.notified_of_own_activity?
  else
    false
  end
end

#suitable_notification_level?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/notification_recipient.rb', line 51

def suitable_notification_level?
  case notification_level
  when :mention
    @type == :mention
  when :participating
    participating_custom_action? || participating_or_mention?
  when :custom
    custom_enabled? || participating_or_mention?
  when :watch
    !excluded_watcher_action?
  else
    false
  end
end

#unsubscribed?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
# File 'app/models/notification_recipient.rb', line 76

def unsubscribed?
  subscribable_target = @target.is_a?(Note) ? @target.noteable : @target

  return false unless subscribable_target
  return false unless subscribable_target.respond_to?(:subscriptions)

  subscription = subscribable_target.subscriptions.find { |subscription| subscription.user_id == @user.id }
  subscription && !subscription.subscribed
end