From eaa503d679e3c2a1396091efebda49a91637cb02 Mon Sep 17 00:00:00 2001 From: "http://jneen.net/" <jneen@jneen.net> Date: Wed, 2 Aug 2017 14:14:53 -0700 Subject: [PATCH] move the read_ability logic into NotificationRecipient --- app/models/notification_recipient.rb | 22 +++++++++++++++---- .../notification_recipient_service.rb | 16 -------------- app/services/notification_service.rb | 1 - 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/app/models/notification_recipient.rb b/app/models/notification_recipient.rb index c307f0ad5b6..418b42d8f1d 100644 --- a/app/models/notification_recipient.rb +++ b/app/models/notification_recipient.rb @@ -5,12 +5,10 @@ class NotificationRecipient custom_action: nil, target: nil, acting_user: nil, - read_ability: nil, project: nil ) @custom_action = custom_action @acting_user = acting_user - @read_ability = read_ability @target = target @project = project || @target&.project @user = user @@ -81,10 +79,10 @@ class NotificationRecipient return false unless user.can?(:receive_notifications) return false if @project && !user.can?(:read_project, @project) - return true unless @read_ability + return true unless read_ability return true unless DeclarativePolicy.has_policy?(@target) - user.can?(@read_ability, @target) + user.can?(read_ability, @target) end end @@ -97,6 +95,22 @@ class NotificationRecipient 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) diff --git a/app/services/notification_recipient_service.rb b/app/services/notification_recipient_service.rb index 5d394f67631..7d4f8110f84 100644 --- a/app/services/notification_recipient_service.rb +++ b/app/services/notification_recipient_service.rb @@ -76,7 +76,6 @@ module NotificationRecipientService custom_action: custom_action, target: target, acting_user: acting_user, - read_ability: read_ability ) end @@ -91,15 +90,6 @@ module NotificationRecipientService end end - def read_ability - case target - when Issuable - :"read_#{target.to_ability_name}" - when Ci::Pipeline - :read_build # We have build trace in pipeline emails - end - end - def custom_action nil end @@ -285,12 +275,6 @@ module NotificationRecipientService note.project end - def read_ability - return nil if target.nil? - - :"read_#{target.class.model_name.name.underscore}" - end - def build! # Add all users participating in the thread (author, assignee, comment authors) add_participants(note.author) diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index c93f82999dc..2e52296f048 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -303,7 +303,6 @@ class NotificationService recipients ||= NotificationRecipientService.notifiable_users( [pipeline.user], :watch, custom_action: :"#{pipeline.status}_pipeline", - read_ability: :read_build, target: pipeline ).map(&:notification_email) -- GitLab