diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2019-08-15 17:37:36 +0000 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2019-08-15 17:37:36 +0000 |
commit | 1985bafe2be33f03bccefaadfa9173fca49c33b9 (patch) | |
tree | 896dc6ef08d8347e992365594ce7c4b0d49e6ee4 /app/models | |
parent | 23754943a7ec119f123694a93c79fc07c32b7ba5 (diff) | |
parent | 3489dc3d7277bf478f68e1b3e0353b702f652acc (diff) | |
download | gitlab-ce-1985bafe2be33f03bccefaadfa9173fca49c33b9.tar.gz |
Merge branch '50020-allow-email-notifications-to-be-disabled-for-all-users-of-a-group' into 'master'
Allow email notifications to be disabled for all users of a group
See merge request gitlab-org/gitlab-ce!30755
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/namespace.rb | 7 | ||||
-rw-r--r-- | app/models/notification_recipient.rb | 8 | ||||
-rw-r--r-- | app/models/project.rb | 7 | ||||
-rw-r--r-- | app/models/project_services/emails_on_push_service.rb | 1 |
4 files changed, 23 insertions, 0 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 058350b16ce..9f9c4288667 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -172,6 +172,13 @@ class Namespace < ApplicationRecord end end + # any ancestor can disable emails for all descendants + def emails_disabled? + strong_memoize(:emails_disabled) do + Feature.enabled?(:emails_disabled, self, default_enabled: true) && self_and_ancestors.where(emails_disabled: true).exists? + end + end + def lfs_enabled? # User namespace will always default to the global setting Gitlab.config.lfs.enabled diff --git a/app/models/notification_recipient.rb b/app/models/notification_recipient.rb index a7f73c0f29c..8e44e3d8e17 100644 --- a/app/models/notification_recipient.rb +++ b/app/models/notification_recipient.rb @@ -4,6 +4,7 @@ class NotificationRecipient include Gitlab::Utils::StrongMemoize attr_reader :user, :type, :reason + def initialize(user, type, **opts) unless NotificationSetting.levels.key?(type) || type == :subscription raise ArgumentError, "invalid type: #{type.inspect}" @@ -30,6 +31,7 @@ class NotificationRecipient 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 @@ -109,6 +111,12 @@ class NotificationRecipient private + # They are disabled if the project or group has disallowed it. + # No need to check the group if there is already a project + def emails_disabled? + @project ? @project.emails_disabled? : @group&.emails_disabled? + end + def read_ability return if @skip_read_ability return @read_ability if instance_variable_defined?(:@read_ability) diff --git a/app/models/project.rb b/app/models/project.rb index 0c57ed3e43a..820a8082b27 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -631,6 +631,13 @@ class Project < ApplicationRecord alias_method :ancestors, :ancestors_upto + def emails_disabled? + strong_memoize(:emails_disabled) do + # disabling in the namespace overrides the project setting + Feature.enabled?(:emails_disabled, self, default_enabled: true) && (super || namespace.emails_disabled?) + end + end + def lfs_enabled? return namespace.lfs_enabled? if self[:lfs_enabled].nil? diff --git a/app/models/project_services/emails_on_push_service.rb b/app/models/project_services/emails_on_push_service.rb index 45de64a9990..8ca40138a8f 100644 --- a/app/models/project_services/emails_on_push_service.rb +++ b/app/models/project_services/emails_on_push_service.rb @@ -24,6 +24,7 @@ class EmailsOnPushService < Service def execute(push_data) return unless supported_events.include?(push_data[:object_kind]) + return if project.emails_disabled? EmailsOnPushWorker.perform_async( project_id, |