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)
|
|
|
|
Builder::Default.new(*a).recipient_users
|
|
|
|
end
|
2017-07-20 14:57:42 -04:00
|
|
|
|
2017-07-26 11:36:49 -04:00
|
|
|
def self.build_new_note_recipients(*a)
|
|
|
|
Builder::NewNote.new(*a).recipient_users
|
2017-03-17 13:56:04 -04:00
|
|
|
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
|
|
|
|
|
2017-07-20 19:10:01 -04:00
|
|
|
def recipients
|
|
|
|
@recipients ||= []
|
|
|
|
end
|
|
|
|
|
2017-08-01 14:43:24 -04:00
|
|
|
def <<(pair)
|
|
|
|
users, type = pair
|
2017-07-31 21:56:56 -04:00
|
|
|
|
|
|
|
if users.is_a?(ActiveRecord::Relation)
|
|
|
|
users = users.includes(:notification_settings)
|
|
|
|
end
|
|
|
|
|
2017-08-01 15:55:04 -04:00
|
|
|
users = Array(users)
|
|
|
|
users.compact!
|
2017-07-26 01:22:31 -04:00
|
|
|
recipients.concat(users.map { |u| make_recipient(u, type) })
|
|
|
|
end
|
|
|
|
|
2017-07-31 21:56:56 -04:00
|
|
|
def user_scope
|
|
|
|
User.includes(:notification_settings)
|
|
|
|
end
|
|
|
|
|
2017-07-26 01:22:31 -04:00
|
|
|
def make_recipient(user, type)
|
2017-07-31 18:04:42 -04:00
|
|
|
NotificationRecipient.new(
|
|
|
|
user, type,
|
2017-07-31 12:30:35 -04:00
|
|
|
project: project,
|
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
|
|
|
|
|
|
|
|
def recipient_users
|
|
|
|
@recipient_users ||=
|
|
|
|
begin
|
|
|
|
build!
|
|
|
|
filter!
|
|
|
|
users = recipients.map(&:user)
|
|
|
|
users.uniq!
|
|
|
|
users.freeze
|
|
|
|
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-08-01 15:55:13 -04:00
|
|
|
self << [target.participants(user), :watch]
|
2017-03-30 07:29:52 -04: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)
|
|
|
|
user_ids += user_ids_notifiable_on(project.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)
|
|
|
|
user_ids_with_group_level_global = user_ids_notifiable_on(project.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-07-31 21:56:56 -04:00
|
|
|
self << [user_scope.where(id: user_ids), :watch]
|
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-08-01 15:55:04 -04:00
|
|
|
self << [project_watchers, :watch]
|
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
|
|
|
# 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
|
|
|
|
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-08-01 15:55:04 -04:00
|
|
|
self << [target.subscribers(project), :subscription]
|
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-08-01 15:55:04 -04:00
|
|
|
self << [label.subscribers(project), :subscription]
|
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
|
2017-08-01 14:53:43 -04:00
|
|
|
def initialize(target, current_user, action:, previous_assignee: nil, skip_current_user: true)
|
2017-07-20 15:25:32 -04:00
|
|
|
@target = target
|
|
|
|
@current_user = current_user
|
|
|
|
@action = action
|
|
|
|
@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-08-01 15:55:04 -04:00
|
|
|
self << [previous_assignee, :mention]
|
|
|
|
self << [target.assignee, :mention]
|
2017-07-20 15:21:47 -04:00
|
|
|
when :reassign_issue
|
|
|
|
previous_assignees = Array(previous_assignee)
|
2017-08-01 15:55:04 -04:00
|
|
|
self << [previous_assignees, :mention]
|
|
|
|
self << [target.assignees, :mention]
|
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
|
|
|
|
|
|
|
if [:new_issue, :new_merge_request].include?(custom_action)
|
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
|
|
|
|
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-08-01 15:55:13 -04:00
|
|
|
self << [note.mentioned_users, :mention]
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
unless note.for_personal_snippet?
|
|
|
|
# Merge project watchers
|
2017-07-20 19:10:01 -04:00
|
|
|
add_project_watchers
|
2017-03-17 13:56:04 -04:00
|
|
|
|
2017-07-20 15:21:47 -04:00
|
|
|
# Merge project with custom notification
|
2017-08-01 15:55:13 -04:00
|
|
|
add_custom_notifications
|
2017-07-20 15:21:47 -04:00
|
|
|
end
|
2017-03-17 13:56:04 -04:00
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|