2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
module NotificationRecipients
|
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
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
# override if needed
|
|
|
|
def recipients_target
|
|
|
|
target
|
|
|
|
end
|
|
|
|
|
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,
|
2020-04-22 19:07:51 +05:30
|
|
|
target: recipients_target,
|
2017-09-10 17:25:29 +05:30
|
|
|
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
|
2021-04-29 21:17:54 +05:30
|
|
|
notification_by_sources = related_notification_settings_sources(:custom)
|
|
|
|
|
|
|
|
return if notification_by_sources.blank?
|
|
|
|
|
|
|
|
user_ids = NotificationSetting.from_union(notification_by_sources).select(:user_id)
|
|
|
|
|
|
|
|
add_recipients(user_scope.where(id: user_ids), :custom, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def related_notification_settings_sources(level)
|
|
|
|
sources = [project, group].compact
|
|
|
|
|
|
|
|
sources.map do |source|
|
|
|
|
source
|
|
|
|
.notification_settings
|
|
|
|
.where(source_or_global_setting_by_level_query(level)).select(:user_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def global_setting_by_level_query(level)
|
|
|
|
table = NotificationSetting.arel_table
|
|
|
|
aliased_table = table.alias
|
|
|
|
|
|
|
|
table
|
|
|
|
.project('true')
|
|
|
|
.from(aliased_table)
|
|
|
|
.where(
|
|
|
|
aliased_table[:user_id].eq(table[:user_id])
|
|
|
|
.and(aliased_table[:source_id].eq(nil))
|
|
|
|
.and(aliased_table[:source_type].eq(nil))
|
|
|
|
.and(aliased_table[:level].eq(level))
|
|
|
|
).exists
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_or_global_setting_by_level_query(level)
|
|
|
|
table = NotificationSetting.arel_table
|
|
|
|
table.grouping(
|
|
|
|
table[:level].eq(:global).and(global_setting_by_level_query(level))
|
|
|
|
).or(table[:level].eq(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_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
|
2021-06-08 01:23:25 +05:30
|
|
|
notification_by_sources = related_notification_settings_sources(:watch)
|
|
|
|
|
|
|
|
return if notification_by_sources.blank?
|
|
|
|
|
|
|
|
user_ids = NotificationSetting.from_union(notification_by_sources).select(:user_id)
|
|
|
|
|
|
|
|
user_scope.where(id: user_ids)
|
|
|
|
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
|
2021-06-08 01:23:25 +05:30
|
|
|
return [] unless group
|
|
|
|
|
|
|
|
user_ids = group
|
|
|
|
.notification_settings
|
|
|
|
.where(source_or_global_setting_by_level_query(:watch)).select(:user_id)
|
|
|
|
|
|
|
|
user_scope.where(id: user_ids)
|
|
|
|
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
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
add_recipients(target.subscribers(project), :subscription, NotificationReason::SUBSCRIBED)
|
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|
|
2019-12-26 22:10:19 +05:30
|
|
|
add_recipients(label.subscribers(project), :subscription, NotificationReason::SUBSCRIBED)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|