2017-03-17 13:56:04 -04:00
|
|
|
#
|
|
|
|
# Used by NotificationService to determine who should receive notification
|
|
|
|
#
|
2017-07-26 11:36:49 -04:00
|
|
|
module NotificationRecipientService
|
2017-07-26 11:43:27 -04:00
|
|
|
def self.notifiable_users(users, *args)
|
2017-08-01 15:51:14 -04:00
|
|
|
users.compact.map { |u| NotificationRecipient.new(u, *args) }.select(&:notifiable?).map(&:user)
|
2017-07-26 11:43:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.notifiable?(user, *args)
|
|
|
|
NotificationRecipient.new(user, *args).notifiable?
|
2017-07-26 11:36:49 -04:00
|
|
|
end
|
2017-07-20 14:57:42 -04:00
|
|
|
|
2017-07-26 11:36:49 -04:00
|
|
|
def self.build_recipients(*a)
|
2017-12-28 12:25:02 -05:00
|
|
|
Builder::Default.new(*a).notification_recipients
|
2017-07-26 11:36:49 -04:00
|
|
|
end
|
2017-07-20 14:57:42 -04:00
|
|
|
|
2017-07-26 11:36:49 -04:00
|
|
|
def self.build_new_note_recipients(*a)
|
2017-12-28 12:25:02 -05:00
|
|
|
Builder::NewNote.new(*a).notification_recipients
|
2017-03-17 13:56:04 -04:00
|
|
|
end
|
|
|
|
|
2018-05-09 05:49:33 -04:00
|
|
|
def self.build_merge_request_unmergeable_recipients(*a)
|
|
|
|
Builder::MergeRequestUnmergeable.new(*a).notification_recipients
|
|
|
|
end
|
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
module Builder
|
|
|
|
class Base
|
2017-07-20 15:25:32 -04:00
|
|
|
def initialize(*)
|
|
|
|
raise 'abstract'
|
|
|
|
end
|
|
|
|
|
2017-08-01 15:55:04 -04:00
|
|
|
def build!
|
|
|
|
raise 'abstract'
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter!
|
2017-08-01 15:55:13 -04:00
|
|
|
recipients.select!(&:notifiable?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def acting_user
|
|
|
|
current_user
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 15:36:37 -04:00
|
|
|
def target
|
2017-07-20 15:21:47 -04:00
|
|
|
raise 'abstract'
|
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-08-01 16:18:51 -04:00
|
|
|
# rubocop:disable Rails/Delegate
|
2017-08-01 14:53:43 -04:00
|
|
|
def project
|
|
|
|
target.project
|
|
|
|
end
|
|
|
|
|
2018-05-03 17:32:20 -04:00
|
|
|
def group
|
|
|
|
project&.group || target.try(:group)
|
|
|
|
end
|
|
|
|
|
2017-07-20 19:10:01 -04:00
|
|
|
def recipients
|
|
|
|
@recipients ||= []
|
|
|
|
end
|
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
def add_recipients(users, type, reason)
|
2017-07-31 21:56:56 -04:00
|
|
|
if users.is_a?(ActiveRecord::Relation)
|
|
|
|
users = users.includes(:notification_settings)
|
|
|
|
end
|
|
|
|
|
2018-04-09 04:16:40 -04:00
|
|
|
users = Array(users).compact
|
2017-12-28 12:25:02 -05:00
|
|
|
recipients.concat(users.map { |u| make_recipient(u, type, reason) })
|
2017-07-26 01:22:31 -04:00
|
|
|
end
|
|
|
|
|
2017-07-31 21:56:56 -04:00
|
|
|
def user_scope
|
|
|
|
User.includes(:notification_settings)
|
|
|
|
end
|
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
def make_recipient(user, type, reason)
|
2017-07-31 18:04:42 -04:00
|
|
|
NotificationRecipient.new(
|
|
|
|
user, type,
|
2017-12-28 12:25:02 -05:00
|
|
|
reason: reason,
|
2017-07-31 12:30:35 -04:00
|
|
|
project: project,
|
2018-05-03 17:32:20 -04:00
|
|
|
group: group,
|
2017-07-26 01:22:31 -04:00
|
|
|
custom_action: custom_action,
|
|
|
|
target: target,
|
2017-08-02 21:54:18 -04:00
|
|
|
acting_user: acting_user
|
2017-07-26 01:22:31 -04:00
|
|
|
)
|
2017-08-01 15:55:04 -04:00
|
|
|
end
|
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
def notification_recipients
|
|
|
|
@notification_recipients ||=
|
2017-08-01 15:55:04 -04:00
|
|
|
begin
|
|
|
|
build!
|
|
|
|
filter!
|
2017-12-28 12:25:02 -05:00
|
|
|
recipients = self.recipients.sort_by { |r| NotificationReason.priority(r.reason) }.uniq(&:user)
|
|
|
|
recipients.freeze
|
2017-08-01 15:55:04 -04:00
|
|
|
end
|
2017-07-20 19:10:01 -04:00
|
|
|
end
|
|
|
|
|
2017-07-26 12:32:37 -04:00
|
|
|
def custom_action
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
protected
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 19:10:01 -04:00
|
|
|
def add_participants(user)
|
2017-07-20 15:21:47 -04:00
|
|
|
return unless target.respond_to?(:participants)
|
2017-03-30 07:29:52 -04:00
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
add_recipients(target.participants(user), :participating, nil)
|
2017-03-30 07:29:52 -04:00
|
|
|
end
|
|
|
|
|
2017-12-01 10:47:28 -05:00
|
|
|
def add_mentions(user, target:)
|
|
|
|
return unless target.respond_to?(:mentioned_users)
|
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
add_recipients(target.mentioned_users(user), :mention, NotificationReason::MENTIONED)
|
2017-12-01 10:47:28 -05:00
|
|
|
end
|
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
# Get project/group users with CUSTOM notification level
|
2017-08-01 15:55:13 -04:00
|
|
|
def add_custom_notifications
|
2017-07-20 15:21:47 -04:00
|
|
|
user_ids = []
|
2017-03-30 07:29:52 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
# Users with a notification setting on group or project
|
2017-08-01 15:55:13 -04:00
|
|
|
user_ids += user_ids_notifiable_on(project, :custom)
|
2018-05-03 17:32:20 -04:00
|
|
|
user_ids += user_ids_notifiable_on(group, :custom)
|
2017-03-30 07:29:52 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
# Users with global level custom
|
|
|
|
user_ids_with_project_level_global = user_ids_notifiable_on(project, :global)
|
2018-05-03 17:32:20 -04:00
|
|
|
user_ids_with_group_level_global = user_ids_notifiable_on(group, :global)
|
2017-03-30 07:29:52 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
global_users_ids = user_ids_with_project_level_global.concat(user_ids_with_group_level_global)
|
2017-08-01 15:55:13 -04:00
|
|
|
user_ids += user_ids_with_global_level_custom(global_users_ids, custom_action)
|
2017-03-30 07:29:52 -04:00
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
add_recipients(user_scope.where(id: user_ids), :watch, nil)
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-03-30 07:29:52 -04:00
|
|
|
|
2017-07-20 19:10:01 -04:00
|
|
|
def add_project_watchers
|
2017-12-28 12:25:02 -05:00
|
|
|
add_recipients(project_watchers, :watch, nil)
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2018-05-03 17:32:20 -04:00
|
|
|
def add_group_watchers
|
|
|
|
add_recipients(group_watchers, :watch, nil)
|
|
|
|
end
|
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
# Get project users with WATCH notification level
|
|
|
|
def project_watchers
|
|
|
|
project_members_ids = user_ids_notifiable_on(project)
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
user_ids_with_project_global = user_ids_notifiable_on(project, :global)
|
|
|
|
user_ids_with_group_global = user_ids_notifiable_on(project.group, :global)
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
user_ids = user_ids_with_global_level_watch((user_ids_with_project_global + user_ids_with_group_global).uniq)
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-08-01 14:53:43 -04:00
|
|
|
user_ids_with_project_setting = select_project_members_ids(user_ids_with_project_global, user_ids)
|
2017-07-20 15:21:47 -04:00
|
|
|
user_ids_with_group_setting = select_group_members_ids(project.group, project_members_ids, user_ids_with_group_global, user_ids)
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-31 21:56:56 -04:00
|
|
|
user_scope.where(id: user_ids_with_project_setting.concat(user_ids_with_group_setting).uniq)
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2018-05-03 17:32:20 -04:00
|
|
|
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
|
|
|
|
|
2017-07-20 19:10:01 -04:00
|
|
|
def add_subscribed_users
|
|
|
|
return unless target.respond_to? :subscribers
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
add_recipients(target.subscribers(project), :subscription, nil)
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-08-01 15:55:13 -04:00
|
|
|
def user_ids_notifiable_on(resource, notification_level = nil)
|
2017-07-20 15:21:47 -04:00
|
|
|
return [] unless resource
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-08-01 15:55:13 -04:00
|
|
|
scope = resource.notification_settings
|
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
if notification_level
|
2017-08-01 15:55:13 -04:00
|
|
|
scope = scope.where(level: NotificationSetting.levels[notification_level])
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-08-01 15:55:13 -04:00
|
|
|
|
|
|
|
scope.pluck(:user_id)
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
# Build a list of user_ids based on project notification settings
|
2017-08-01 14:53:43 -04:00
|
|
|
def select_project_members_ids(global_setting, user_ids_global_level_watch)
|
2017-07-20 15:21:47 -04:00
|
|
|
user_ids = user_ids_notifiable_on(project, :watch)
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
# If project setting is global, add to watch list if global setting is watch
|
2017-07-26 12:33:01 -04:00
|
|
|
user_ids + (global_setting & user_ids_global_level_watch)
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-06-28 06:44:32 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
# 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)
|
|
|
|
|
|
|
|
# Group setting is global, add to user_ids list if global setting is watch
|
2017-07-26 12:33:01 -04:00
|
|
|
uids + (global_setting & user_ids_global_level_watch) - project_members
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-06-28 06:44:32 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
def user_ids_with_global_level_watch(ids)
|
|
|
|
settings_with_global_level_of(:watch, ids).pluck(:user_id)
|
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
def user_ids_with_global_level_custom(ids, action)
|
2017-07-31 20:09:01 -04:00
|
|
|
settings_with_global_level_of(:custom, ids).pluck(:user_id)
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
def settings_with_global_level_of(level, ids)
|
|
|
|
NotificationSetting.where(
|
|
|
|
user_id: ids,
|
|
|
|
source_type: nil,
|
|
|
|
level: NotificationSetting.levels[level]
|
|
|
|
)
|
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 19:10:01 -04:00
|
|
|
def add_labels_subscribers(labels: nil)
|
|
|
|
return unless target.respond_to? :labels
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
(labels || target.labels).each do |label|
|
2017-12-28 12:25:02 -05:00
|
|
|
add_recipients(label.subscribers(project), :subscription, nil)
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
class Default < Base
|
2017-07-20 15:25:32 -04:00
|
|
|
attr_reader :target
|
|
|
|
attr_reader :current_user
|
|
|
|
attr_reader :action
|
|
|
|
attr_reader :previous_assignee
|
|
|
|
attr_reader :skip_current_user
|
2018-03-30 08:25:46 -04:00
|
|
|
def initialize(target, current_user, action:, custom_action: nil, previous_assignee: nil, skip_current_user: true)
|
2017-07-20 15:25:32 -04:00
|
|
|
@target = target
|
|
|
|
@current_user = current_user
|
|
|
|
@action = action
|
2018-03-30 08:25:46 -04:00
|
|
|
@custom_action = custom_action
|
2017-07-20 15:25:32 -04:00
|
|
|
@previous_assignee = previous_assignee
|
|
|
|
@skip_current_user = skip_current_user
|
|
|
|
end
|
|
|
|
|
2017-08-01 15:55:04 -04:00
|
|
|
def build!
|
2017-07-20 19:10:01 -04:00
|
|
|
add_participants(current_user)
|
|
|
|
add_project_watchers
|
2017-08-01 15:55:13 -04:00
|
|
|
add_custom_notifications
|
2017-07-20 15:21:47 -04:00
|
|
|
|
2017-08-01 15:55:13 -04:00
|
|
|
# Re-assign is considered as a mention of the new assignee
|
2017-07-20 15:21:47 -04:00
|
|
|
case custom_action
|
|
|
|
when :reassign_merge_request
|
2017-12-28 12:25:02 -05:00
|
|
|
add_recipients(previous_assignee, :mention, nil)
|
|
|
|
add_recipients(target.assignee, :mention, NotificationReason::ASSIGNED)
|
2017-07-20 15:21:47 -04:00
|
|
|
when :reassign_issue
|
|
|
|
previous_assignees = Array(previous_assignee)
|
2017-12-28 12:25:02 -05:00
|
|
|
add_recipients(previous_assignees, :mention, nil)
|
|
|
|
add_recipients(target.assignees, :mention, NotificationReason::ASSIGNED)
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
|
|
|
|
2017-07-20 19:10:01 -04:00
|
|
|
add_subscribed_users
|
2017-07-20 15:21:47 -04:00
|
|
|
|
2018-03-30 07:58:06 -04:00
|
|
|
if [:new_issue, :new_merge_request].include?(custom_action)
|
2017-12-01 10:47:28 -05:00
|
|
|
# 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)
|
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
# Add the assigned users, if any
|
2018-02-06 13:09:59 -05:00
|
|
|
assignees = case custom_action
|
2018-03-30 07:58:06 -04:00
|
|
|
when :new_issue
|
2018-02-06 13:09:59 -05:00
|
|
|
target.assignees
|
|
|
|
else
|
|
|
|
target.assignee
|
|
|
|
end
|
|
|
|
|
2017-12-28 12:25:02 -05:00
|
|
|
# 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-07-20 19:10:01 -04:00
|
|
|
add_labels_subscribers
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-08-01 15:55:04 -04:00
|
|
|
end
|
2017-07-20 15:21:47 -04:00
|
|
|
|
2017-08-01 15:55:13 -04:00
|
|
|
def acting_user
|
|
|
|
current_user if skip_current_user
|
2017-03-17 13:56:04 -04:00
|
|
|
end
|
2017-07-20 15:37:36 -04:00
|
|
|
|
|
|
|
# Build event key to search on custom notification level
|
|
|
|
# Check NotificationSetting::EMAIL_EVENTS
|
|
|
|
def custom_action
|
|
|
|
@custom_action ||= "#{action}_#{target.class.model_name.name.underscore}".to_sym
|
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
end
|
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
class NewNote < Base
|
2017-07-20 15:25:32 -04:00
|
|
|
attr_reader :note
|
2017-08-01 14:53:43 -04:00
|
|
|
def initialize(note)
|
2017-07-20 15:25:32 -04:00
|
|
|
@note = note
|
2017-08-01 14:53:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def target
|
|
|
|
note.noteable
|
|
|
|
end
|
|
|
|
|
2017-08-02 17:21:31 -04:00
|
|
|
# NOTE: may be nil, in the case of a PersonalSnippet
|
|
|
|
#
|
|
|
|
# (this is okay because NotificationRecipient is written
|
|
|
|
# to handle nil projects)
|
2017-08-01 14:53:43 -04:00
|
|
|
def project
|
|
|
|
note.project
|
2017-07-20 15:25:32 -04:00
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2018-05-03 17:32:20 -04:00
|
|
|
def group
|
|
|
|
if note.for_project_noteable?
|
|
|
|
project.group
|
|
|
|
else
|
|
|
|
target.try(:group)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-01 15:55:04 -04:00
|
|
|
def build!
|
2017-07-20 15:21:47 -04:00
|
|
|
# Add all users participating in the thread (author, assignee, comment authors)
|
2017-07-20 19:10:01 -04:00
|
|
|
add_participants(note.author)
|
2017-12-01 10:47:28 -05:00
|
|
|
add_mentions(note.author, target: note)
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2018-02-28 02:48:23 -05:00
|
|
|
if note.for_project_noteable?
|
2017-07-20 15:21:47 -04:00
|
|
|
# Merge project watchers
|
2017-07-20 19:10:01 -04:00
|
|
|
add_project_watchers
|
2018-05-03 17:32:20 -04:00
|
|
|
else
|
|
|
|
add_group_watchers
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2018-05-03 17:32:20 -04:00
|
|
|
add_custom_notifications
|
2017-07-20 19:10:01 -04:00
|
|
|
add_subscribed_users
|
2017-08-01 15:55:04 -04:00
|
|
|
end
|
|
|
|
|
2017-08-01 15:55:13 -04:00
|
|
|
def custom_action
|
|
|
|
:new_note
|
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-08-01 15:55:13 -04:00
|
|
|
def acting_user
|
|
|
|
note.author
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
end
|
2018-05-09 05:49:33 -04:00
|
|
|
|
|
|
|
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
|
2017-03-17 13:56:04 -04:00
|
|
|
end
|
|
|
|
end
|