2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-26 11:43:27 -04:00
|
|
|
class NotificationRecipient
|
2018-05-28 14:43:46 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
attr_reader :user, :type, :reason
|
|
|
|
def initialize(user, type, **opts)
|
2017-08-04 14:53:17 -04:00
|
|
|
unless NotificationSetting.levels.key?(type) || type == :subscription
|
|
|
|
raise ArgumentError, "invalid type: #{type.inspect}"
|
|
|
|
end
|
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
@custom_action = opts[:custom_action]
|
|
|
|
@acting_user = opts[:acting_user]
|
|
|
|
@target = opts[:target]
|
|
|
|
@project = opts[:project] || default_project
|
|
|
|
@group = opts[:group] || @project&.group
|
2017-07-26 11:43:27 -04:00
|
|
|
@user = user
|
|
|
|
@type = type
|
2017-12-28 12:25:02 -05:00
|
|
|
@reason = opts[:reason]
|
|
|
|
@skip_read_ability = opts[:skip_read_ability]
|
2017-07-26 11:43:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def notification_setting
|
|
|
|
@notification_setting ||= find_notification_setting
|
|
|
|
end
|
|
|
|
|
|
|
|
def notification_level
|
2017-08-18 16:40:09 -04:00
|
|
|
@notification_level ||= notification_setting&.level&.to_sym
|
2017-07-26 11:43:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def notifiable?
|
|
|
|
return false unless has_access?
|
|
|
|
return false if own_activity?
|
|
|
|
|
2017-08-18 16:40:09 -04:00
|
|
|
# even users with :disabled notifications receive manual subscriptions
|
|
|
|
return !unsubscribed? if @type == :subscription
|
2017-07-26 11:43:27 -04:00
|
|
|
|
2017-08-18 16:40:09 -04:00
|
|
|
return false unless suitable_notification_level?
|
2017-07-26 11:43:27 -04:00
|
|
|
|
2017-08-18 16:40:09 -04:00
|
|
|
# check this last because it's expensive
|
|
|
|
# nobody should receive notifications if they've specifically unsubscribed
|
2018-03-15 21:05:44 -04:00
|
|
|
# except if they were mentioned.
|
|
|
|
return false if @type != :mention && unsubscribed?
|
2017-07-26 11:43:27 -04:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-08-18 16:40:09 -04:00
|
|
|
def suitable_notification_level?
|
|
|
|
case notification_level
|
|
|
|
when :disabled, nil
|
|
|
|
false
|
|
|
|
when :custom
|
|
|
|
custom_enabled? || %i[participating mention].include?(@type)
|
|
|
|
when :watch, :participating
|
2017-07-25 07:56:09 -04:00
|
|
|
!action_excluded?
|
2017-08-18 16:40:09 -04:00
|
|
|
when :mention
|
|
|
|
@type == :mention
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_enabled?
|
|
|
|
@custom_action && notification_setting&.event_enabled?(@custom_action)
|
|
|
|
end
|
|
|
|
|
2017-07-26 11:43:27 -04:00
|
|
|
def unsubscribed?
|
|
|
|
return false unless @target
|
|
|
|
return false unless @target.respond_to?(:subscriptions)
|
|
|
|
|
2018-06-07 07:14:27 -04:00
|
|
|
subscription = @target.subscriptions.find { |subscription| subscription.user_id == @user.id }
|
2017-07-26 11:43:27 -04:00
|
|
|
subscription && !subscription.subscribed
|
|
|
|
end
|
|
|
|
|
|
|
|
def own_activity?
|
|
|
|
return false unless @acting_user
|
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
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
|
2017-07-26 11:43:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_access?
|
|
|
|
DeclarativePolicy.subject_scope do
|
2018-04-18 05:19:40 -04:00
|
|
|
break false unless user.can?(:receive_notifications)
|
|
|
|
break true if @skip_read_ability
|
2017-08-10 13:41:25 -04:00
|
|
|
|
2018-04-18 05:19:40 -04:00
|
|
|
break false if @target && !user.can?(:read_cross_project)
|
|
|
|
break false if @project && !user.can?(:read_project, @project)
|
2017-08-01 13:42:54 -04:00
|
|
|
|
2018-04-18 05:19:40 -04:00
|
|
|
break true unless read_ability
|
|
|
|
break true unless DeclarativePolicy.has_policy?(@target)
|
2017-08-01 13:42:54 -04:00
|
|
|
|
2017-08-02 17:14:53 -04:00
|
|
|
user.can?(read_ability, @target)
|
2017-07-26 11:43:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-25 07:56:09 -04:00
|
|
|
def action_excluded?
|
|
|
|
excluded_watcher_action? || excluded_participating_action?
|
|
|
|
end
|
|
|
|
|
2017-07-26 11:43:27 -04:00
|
|
|
def excluded_watcher_action?
|
2017-07-25 07:56:09 -04:00
|
|
|
return false unless @custom_action && notification_level == :watch
|
2017-07-26 11:43:27 -04:00
|
|
|
|
|
|
|
NotificationSetting::EXCLUDED_WATCHER_EVENTS.include?(@custom_action)
|
2017-07-25 07:56:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def excluded_participating_action?
|
|
|
|
return false unless @custom_action && notification_level == :participating
|
|
|
|
|
|
|
|
NotificationSetting::EXCLUDED_PARTICIPATING_EVENTS.include?(@custom_action)
|
2017-07-26 11:43:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-02 17:14:53 -04:00
|
|
|
def read_ability
|
2017-08-10 13:41:25 -04:00
|
|
|
return nil if @skip_read_ability
|
2017-08-02 17:14:53 -04:00
|
|
|
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
|
|
|
|
|
2017-08-04 14:53:17 -04:00
|
|
|
def default_project
|
|
|
|
return nil if @target.nil?
|
|
|
|
return @target if @target.is_a?(Project)
|
|
|
|
return @target.project if @target.respond_to?(:project)
|
|
|
|
end
|
|
|
|
|
2017-07-26 11:43:27 -04:00
|
|
|
def find_notification_setting
|
|
|
|
project_setting = @project && user.notification_settings_for(@project)
|
|
|
|
|
|
|
|
return project_setting unless project_setting.nil? || project_setting.global?
|
|
|
|
|
2018-05-28 14:43:46 -04:00
|
|
|
group_setting = closest_non_global_group_notification_settting
|
2017-07-26 11:43:27 -04:00
|
|
|
|
2018-05-28 14:43:46 -04:00
|
|
|
return group_setting unless group_setting.nil?
|
2017-07-26 11:43:27 -04:00
|
|
|
|
|
|
|
user.global_notification_setting
|
|
|
|
end
|
2018-05-28 14:43:46 -04:00
|
|
|
|
|
|
|
# Returns the notificaton_setting of the lowest group in hierarchy with non global level
|
|
|
|
def closest_non_global_group_notification_settting
|
|
|
|
return unless @group
|
|
|
|
return if indexed_group_notification_settings.empty?
|
|
|
|
|
|
|
|
notification_setting = nil
|
|
|
|
|
|
|
|
@group.self_and_ancestors_ids.each do |id|
|
|
|
|
notification_setting = indexed_group_notification_settings[id]
|
|
|
|
break if notification_setting
|
|
|
|
end
|
|
|
|
|
|
|
|
notification_setting
|
|
|
|
end
|
|
|
|
|
|
|
|
def indexed_group_notification_settings
|
|
|
|
strong_memoize(:indexed_group_notification_settings) do
|
|
|
|
@group.notification_settings.where(user_id: user.id)
|
|
|
|
.where.not(level: NotificationSetting.levels[:global])
|
|
|
|
.index_by(&:source_id)
|
|
|
|
end
|
|
|
|
end
|
2017-07-26 11:43:27 -04:00
|
|
|
end
|