2017-07-26 11:43:27 -04:00
|
|
|
class NotificationRecipient
|
2017-07-31 14:32:17 -04:00
|
|
|
attr_reader :user, :type
|
2017-07-31 18:04:42 -04:00
|
|
|
def initialize(
|
|
|
|
user, type,
|
|
|
|
custom_action: nil,
|
|
|
|
target: nil,
|
|
|
|
acting_user: nil,
|
2017-08-04 14:53:17 -04:00
|
|
|
project: nil,
|
2017-08-10 13:41:25 -04:00
|
|
|
group: nil,
|
|
|
|
skip_read_ability: false
|
2017-07-31 18:04:42 -04:00
|
|
|
)
|
2017-08-04 14:53:17 -04:00
|
|
|
unless NotificationSetting.levels.key?(type) || type == :subscription
|
|
|
|
raise ArgumentError, "invalid type: #{type.inspect}"
|
|
|
|
end
|
|
|
|
|
2017-07-26 11:43:27 -04:00
|
|
|
@custom_action = custom_action
|
|
|
|
@acting_user = acting_user
|
|
|
|
@target = target
|
2017-08-04 14:53:17 -04:00
|
|
|
@project = project || default_project
|
|
|
|
@group = group || @project&.group
|
2017-07-26 11:43:27 -04:00
|
|
|
@user = user
|
|
|
|
@type = type
|
2017-08-10 13:41:25 -04:00
|
|
|
@skip_read_ability = skip_read_ability
|
2017-07-26 11:43:27 -04:00
|
|
|
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
|
2017-08-01 14:42:46 -04:00
|
|
|
# set to :custom, meaning to send exactly when our type is :participating
|
|
|
|
# or :mention.
|
2017-07-26 11:43:27 -04:00
|
|
|
@notification_level ||=
|
|
|
|
case raw_notification_level
|
|
|
|
when :custom
|
2017-08-01 14:42:24 -04:00
|
|
|
if @custom_action && notification_setting&.event_enabled?(@custom_action)
|
2017-07-26 11:43:27 -04:00
|
|
|
: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?
|
|
|
|
|
2017-08-01 14:42:59 -04:00
|
|
|
return false unless NotificationSetting.levels[notification_level] <= NotificationSetting.levels[@type]
|
2017-07-26 11:43:27 -04:00
|
|
|
|
|
|
|
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
|
2017-08-01 13:42:54 -04:00
|
|
|
return false unless user.can?(:receive_notifications)
|
2017-08-10 13:41:25 -04:00
|
|
|
return true if @skip_read_ability
|
|
|
|
|
2017-08-01 13:42:54 -04:00
|
|
|
return false if @project && !user.can?(:read_project, @project)
|
|
|
|
|
2017-08-02 17:14:53 -04:00
|
|
|
return true unless read_ability
|
2017-08-01 13:42:54 -04:00
|
|
|
return true unless DeclarativePolicy.has_policy?(@target)
|
|
|
|
|
2017-08-02 17:14:53 -04:00
|
|
|
user.can?(read_ability, @target)
|
2017-07-26 11:43:27 -04:00
|
|
|
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
|
|
|
|
|
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?
|
|
|
|
|
2017-08-04 14:53:17 -04:00
|
|
|
group_setting = @group && user.notification_settings_for(@group)
|
2017-07-26 11:43:27 -04:00
|
|
|
|
|
|
|
return group_setting unless group_setting.nil? || group_setting.global?
|
|
|
|
|
|
|
|
user.global_notification_setting
|
|
|
|
end
|
|
|
|
end
|