Commit b38cc0c2 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'refactor.notification-recipient-builders-ee' into 'master'

Refactor.notification recipient builders (ee version)

See merge request !2584
parents e012d8ca 046fceff
class NotificationRecipient
attr_reader :user, :type
def initialize(
user, type,
custom_action: nil,
target: nil,
acting_user: nil,
project: nil
)
@custom_action = custom_action
@acting_user = acting_user
@target = target
@project = project || @target&.project
@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
# set to :custom, meaning to send exactly when our type is :participating
# or :mention.
@notification_level ||=
case raw_notification_level
when :custom
if @custom_action && notification_setting&.event_enabled?(@custom_action)
: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?
return false unless NotificationSetting.levels[notification_level] <= NotificationSetting.levels[@type]
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
return false unless user.can?(:receive_notifications)
return false if @project && !user.can?(:read_project, @project)
return true unless read_ability
return true unless DeclarativePolicy.has_policy?(@target)
user.can?(read_ability, @target)
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
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
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
This diff is collapsed.
......@@ -28,7 +28,13 @@ module DeclarativePolicy
subject = find_delegate(subject)
class_for_class(subject.class)
policy_class = class_for_class(subject.class)
raise "no policy for #{subject.class.name}" if policy_class.nil?
policy_class
end
def has_policy?(subject)
!class_for_class(subject.class).nil?
end
private
......@@ -51,9 +57,7 @@ module DeclarativePolicy
end
end
policy_class = subject_class.instance_variable_get(CLASS_CACHE_IVAR)
raise "no policy for #{subject.class.name}" if policy_class.nil?
policy_class
subject_class.instance_variable_get(CLASS_CACHE_IVAR)
end
def compute_class_for_class(subject_class)
......@@ -71,6 +75,8 @@ module DeclarativePolicy
nil
end
end
nil
end
def find_delegate(subject)
......
require 'spec_helper'
describe NotificationRecipientService do
set(:user) { create(:user) }
set(:project) { create(:project, :public) }
set(:issue) { create(:issue, project: project) }
set(:watcher) do
watcher = create(:user)
setting = watcher.notification_settings_for(project)
setting.level = :watch
setting.save
watcher
end
subject { described_class.new(project) }
describe '#build_recipients' do
it 'does not modify the participants of the target' do
expect { subject.build_recipients(issue, user, action: :new_issue) }
.not_to change { issue.participants(user) }
end
end
describe '#build_new_note_recipients' do
set(:note) { create(:note_on_issue, noteable: issue, project: project) }
it 'does not modify the participants of the target' do
expect { subject.build_new_note_recipients(note) }
.not_to change { note.noteable.participants(note.author) }
end
end
end
......@@ -306,6 +306,11 @@ describe NotificationService, :mailer do
before do
build_team(note.project)
# make sure these users can read the project snippet!
project.add_guest(@u_guest_watcher)
project.add_guest(@u_guest_custom)
note.project.add_master(note.author)
reset_delivered_emails!
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment