1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# frozen_string_literal: true
module NotificationRecipients
module Builder
class Base
def initialize(*)
raise 'abstract'
end
def build!
raise 'abstract'
end
def filter!
recipients.select!(&:notifiable?)
end
def acting_user
current_user
end
def target
raise 'abstract'
end
# override if needed
def recipients_target
target
end
def project
target.project
end
def group
project&.group || target.try(:group)
end
def recipients
@recipients ||= []
end
# rubocop: disable CodeReuse/ActiveRecord
def add_recipients(users, type, reason)
if users.is_a?(ActiveRecord::Relation)
users = users.includes(:notification_settings)
end
users = Array(users).compact
preload_users_namespace_bans(users)
recipients.concat(users.map { |u| make_recipient(u, type, reason) })
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def user_scope
User.includes(:notification_settings)
end
# rubocop: enable CodeReuse/ActiveRecord
def make_recipient(user, type, reason)
NotificationRecipient.new(
user, type,
reason: reason,
project: project,
group: group,
custom_action: custom_action,
target: recipients_target,
acting_user: acting_user
)
end
def notification_recipients
@notification_recipients ||=
begin
build!
filter!
recipients = self.recipients.sort_by { |r| NotificationReason.priority(r.reason) }.uniq(&:user)
recipients.freeze
end
end
def custom_action
nil
end
protected
def add_participants(user)
return unless target.respond_to?(:participants)
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)
end
# Get project/group users with CUSTOM notification level
# rubocop: disable CodeReuse/ActiveRecord
def add_custom_notifications
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
# rubocop: enable CodeReuse/ActiveRecord
def add_project_watchers
add_recipients(project_watchers, :watch, nil) if project
end
def add_group_watchers
add_recipients(group_watchers, :watch, nil)
end
# Get project users with WATCH notification level
# rubocop: disable CodeReuse/ActiveRecord
def project_watchers
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
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def group_watchers
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
# rubocop: enable CodeReuse/ActiveRecord
def add_subscribed_users
return unless target.respond_to? :subscribers
add_recipients(target.subscribers(project), :subscription, NotificationReason::SUBSCRIBED)
end
def add_labels_subscribers(labels: nil)
return unless target.respond_to? :labels
(labels || target.labels).each do |label|
add_recipients(label.subscribers(project), :subscription, NotificationReason::SUBSCRIBED)
end
end
private
def preload_users_namespace_bans(_users)
# overridden in EE
end
end
end
end
NotificationRecipients::Builder::Base.prepend_mod_with('NotificationRecipients::Builder::Base')
|