debian-mirror-gitlab/app/services/notification_recipient_service.rb

408 lines
12 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
#
# Used by NotificationService to determine who should receive notification
#
2017-09-10 17:25:29 +05:30
module NotificationRecipientService
def self.notifiable_users(users, *args)
users.compact.map { |u| NotificationRecipient.new(u, *args) }.select(&:notifiable?).map(&:user)
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
def self.notifiable?(user, *args)
NotificationRecipient.new(user, *args).notifiable?
2017-08-17 22:00:37 +05:30
end
2018-11-18 11:00:15 +05:30
def self.build_recipients(*args)
Builder::Default.new(*args).notification_recipients
2017-08-17 22:00:37 +05:30
end
2018-11-18 11:00:15 +05:30
def self.build_new_note_recipients(*args)
Builder::NewNote.new(*args).notification_recipients
2017-08-17 22:00:37 +05:30
end
2018-11-18 11:00:15 +05:30
def self.build_merge_request_unmergeable_recipients(*args)
Builder::MergeRequestUnmergeable.new(*args).notification_recipients
2018-11-08 19:23:39 +05:30
end
2019-02-13 22:33:31 +05:30
def self.build_project_maintainers_recipients(*args)
Builder::ProjectMaintainers.new(*args).notification_recipients
end
2017-09-10 17:25:29 +05:30
module Builder
class Base
def initialize(*)
raise 'abstract'
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def build!
raise 'abstract'
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def filter!
recipients.select!(&:notifiable?)
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
def acting_user
current_user
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def target
raise 'abstract'
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def project
target.project
end
2017-08-17 22:00:37 +05:30
2018-10-15 14:42:47 +05:30
def group
project&.group || target.try(:group)
end
2017-09-10 17:25:29 +05:30
def recipients
@recipients ||= []
end
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def add_recipients(users, type, reason)
2017-09-10 17:25:29 +05:30
if users.is_a?(ActiveRecord::Relation)
users = users.includes(:notification_settings)
end
2017-08-17 22:00:37 +05:30
2018-10-15 14:42:47 +05:30
users = Array(users).compact
2018-03-17 18:26:18 +05:30
recipients.concat(users.map { |u| make_recipient(u, type, reason) })
2017-09-10 17:25:29 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def user_scope
User.includes(:notification_settings)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def make_recipient(user, type, reason)
2017-09-10 17:25:29 +05:30
NotificationRecipient.new(
user, type,
2018-03-17 18:26:18 +05:30
reason: reason,
2017-09-10 17:25:29 +05:30
project: project,
2018-10-15 14:42:47 +05:30
group: group,
2017-09-10 17:25:29 +05:30
custom_action: custom_action,
target: target,
acting_user: acting_user
)
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def notification_recipients
@notification_recipients ||=
2017-09-10 17:25:29 +05:30
begin
build!
filter!
2018-03-17 18:26:18 +05:30
recipients = self.recipients.sort_by { |r| NotificationReason.priority(r.reason) }.uniq(&:user)
recipients.freeze
2017-09-10 17:25:29 +05:30
end
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def custom_action
nil
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
protected
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def add_participants(user)
return unless target.respond_to?(:participants)
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
add_recipients(target.participants(user), :participating, nil)
end
def add_mentions(user, target:)
return unless target.respond_to?(:mentioned_users)
add_recipients(target.mentioned_users(user), :mention, NotificationReason::MENTIONED)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
# Get project/group users with CUSTOM notification level
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def add_custom_notifications
user_ids = []
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
# Users with a notification setting on group or project
user_ids += user_ids_notifiable_on(project, :custom)
2018-10-15 14:42:47 +05:30
user_ids += user_ids_notifiable_on(group, :custom)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
# Users with global level custom
user_ids_with_project_level_global = user_ids_notifiable_on(project, :global)
2018-10-15 14:42:47 +05:30
user_ids_with_group_level_global = user_ids_notifiable_on(group, :global)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
global_users_ids = user_ids_with_project_level_global.concat(user_ids_with_group_level_global)
user_ids += user_ids_with_global_level_custom(global_users_ids, custom_action)
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
add_recipients(user_scope.where(id: user_ids), :watch, nil)
2017-09-10 17:25:29 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def add_project_watchers
2018-11-20 20:47:30 +05:30
add_recipients(project_watchers, :watch, nil) if project
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2018-10-15 14:42:47 +05:30
def add_group_watchers
add_recipients(group_watchers, :watch, nil)
end
2017-09-10 17:25:29 +05:30
# Get project users with WATCH notification level
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def project_watchers
project_members_ids = user_ids_notifiable_on(project)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
user_ids_with_project_global = user_ids_notifiable_on(project, :global)
user_ids_with_group_global = user_ids_notifiable_on(project.group, :global)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
user_ids = user_ids_with_global_level_watch((user_ids_with_project_global + user_ids_with_group_global).uniq)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
user_ids_with_project_setting = select_project_members_ids(user_ids_with_project_global, user_ids)
user_ids_with_group_setting = select_group_members_ids(project.group, project_members_ids, user_ids_with_group_global, user_ids)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
user_scope.where(id: user_ids_with_project_setting.concat(user_ids_with_group_setting).uniq)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-10-15 14:42:47 +05:30
def group_watchers
user_ids_with_group_global = user_ids_notifiable_on(group, :global)
user_ids = user_ids_with_global_level_watch(user_ids_with_group_global)
user_ids_with_group_setting = select_group_members_ids(group, [], user_ids_with_group_global, user_ids)
user_scope.where(id: user_ids_with_group_setting)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-10-15 14:42:47 +05:30
2017-09-10 17:25:29 +05:30
def add_subscribed_users
return unless target.respond_to? :subscribers
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
add_recipients(target.subscribers(project), :subscription, nil)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def user_ids_notifiable_on(resource, notification_level = nil)
return [] unless resource
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
scope = resource.notification_settings
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
if notification_level
scope = scope.where(level: NotificationSetting.levels[notification_level])
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
scope.pluck(:user_id)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
# Build a list of user_ids based on project notification settings
def select_project_members_ids(global_setting, user_ids_global_level_watch)
user_ids = user_ids_notifiable_on(project, :watch)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
# If project setting is global, add to watch list if global setting is watch
user_ids + (global_setting & user_ids_global_level_watch)
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
# Build a list of user_ids based on group notification settings
def select_group_members_ids(group, project_members, global_setting, user_ids_global_level_watch)
uids = user_ids_notifiable_on(group, :watch)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
# Group setting is global, add to user_ids list if global setting is watch
uids + (global_setting & user_ids_global_level_watch) - project_members
end
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def user_ids_with_global_level_watch(ids)
settings_with_global_level_of(:watch, ids).pluck(:user_id)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def user_ids_with_global_level_custom(ids, action)
settings_with_global_level_of(:custom, ids).pluck(:user_id)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def settings_with_global_level_of(level, ids)
NotificationSetting.where(
user_id: ids,
source_type: nil,
level: NotificationSetting.levels[level]
)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def add_labels_subscribers(labels: nil)
return unless target.respond_to? :labels
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
(labels || target.labels).each do |label|
2018-03-17 18:26:18 +05:30
add_recipients(label.subscribers(project), :subscription, nil)
2017-09-10 17:25:29 +05:30
end
end
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
class Default < Base
2018-11-20 20:47:30 +05:30
MENTION_TYPE_ACTIONS = [:new_issue, :new_merge_request].freeze
2017-09-10 17:25:29 +05:30
attr_reader :target
attr_reader :current_user
attr_reader :action
attr_reader :previous_assignee
attr_reader :skip_current_user
2018-10-15 14:42:47 +05:30
def initialize(target, current_user, action:, custom_action: nil, previous_assignee: nil, skip_current_user: true)
2017-09-10 17:25:29 +05:30
@target = target
@current_user = current_user
@action = action
2018-10-15 14:42:47 +05:30
@custom_action = custom_action
2017-09-10 17:25:29 +05:30
@previous_assignee = previous_assignee
@skip_current_user = skip_current_user
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def build!
add_participants(current_user)
add_project_watchers
add_custom_notifications
# Re-assign is considered as a mention of the new assignee
case custom_action
when :reassign_merge_request
2018-03-17 18:26:18 +05:30
add_recipients(previous_assignee, :mention, nil)
add_recipients(target.assignee, :mention, NotificationReason::ASSIGNED)
2017-09-10 17:25:29 +05:30
when :reassign_issue
previous_assignees = Array(previous_assignee)
2018-03-17 18:26:18 +05:30
add_recipients(previous_assignees, :mention, nil)
add_recipients(target.assignees, :mention, NotificationReason::ASSIGNED)
2017-09-10 17:25:29 +05:30
end
add_subscribed_users
2018-11-20 20:47:30 +05:30
if self.class.mention_type_actions.include?(custom_action)
2018-03-17 18:26:18 +05:30
# These will all be participants as well, but adding with the :mention
# type ensures that users with the mention notification level will
# receive them, too.
add_mentions(current_user, target: target)
# Add the assigned users, if any
2018-10-15 14:42:47 +05:30
assignees = case custom_action
when :new_issue
target.assignees
else
target.assignee
end
2018-03-17 18:26:18 +05:30
# We use the `:participating` notification level in order to match existing legacy behavior as captured
# in existing specs (notification_service_spec.rb ~ line 507)
add_recipients(assignees, :participating, NotificationReason::ASSIGNED) if assignees
2017-09-10 17:25:29 +05:30
add_labels_subscribers
end
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def acting_user
current_user if skip_current_user
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
# Build event key to search on custom notification level
2018-11-20 20:47:30 +05:30
# Check NotificationSetting.email_events
2017-09-10 17:25:29 +05:30
def custom_action
@custom_action ||= "#{action}_#{target.class.model_name.name.underscore}".to_sym
end
2018-11-20 20:47:30 +05:30
def self.mention_type_actions
MENTION_TYPE_ACTIONS.dup
end
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
class NewNote < Base
attr_reader :note
def initialize(note)
@note = note
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def target
note.noteable
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
# NOTE: may be nil, in the case of a PersonalSnippet
#
# (this is okay because NotificationRecipient is written
# to handle nil projects)
def project
note.project
end
2017-08-17 22:00:37 +05:30
2018-10-15 14:42:47 +05:30
def group
if note.for_project_noteable?
project.group
else
target.try(:group)
end
end
2017-09-10 17:25:29 +05:30
def build!
# Add all users participating in the thread (author, assignee, comment authors)
add_participants(note.author)
2018-03-17 18:26:18 +05:30
add_mentions(note.author, target: note)
2017-08-17 22:00:37 +05:30
2018-03-27 19:54:05 +05:30
if note.for_project_noteable?
2017-09-10 17:25:29 +05:30
# Merge project watchers
add_project_watchers
2018-10-15 14:42:47 +05:30
else
add_group_watchers
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2018-10-15 14:42:47 +05:30
add_custom_notifications
2017-09-10 17:25:29 +05:30
add_subscribed_users
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def custom_action
:new_note
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def acting_user
note.author
end
end
2018-11-08 19:23:39 +05:30
class MergeRequestUnmergeable < Base
attr_reader :target
def initialize(merge_request)
@target = merge_request
end
def build!
target.merge_participants.each do |user|
add_recipients(user, :participating, nil)
end
end
def custom_action
:unmergeable_merge_request
end
def acting_user
nil
end
end
2019-02-13 22:33:31 +05:30
class ProjectMaintainers < Base
attr_reader :target
def initialize(target, action:)
@target = target
@action = action
end
def build!
return [] unless project
add_recipients(project.team.maintainers, :watch, nil)
end
def acting_user
nil
end
end
2017-08-17 22:00:37 +05:30
end
end