Commit 109ab1a58baec790dceb318e96142132cd5b053c
1 parent
528a641e
Exists in
spb-stable
and in
3 other branches
Add comments, fix style, better names for notification methods.
Showing
1 changed file
with
40 additions
and
29 deletions
Show diff stats
app/services/notification_service.rb
| ... | ... | @@ -178,23 +178,21 @@ class NotificationService |
| 178 | 178 | |
| 179 | 179 | # Get project users with WATCH notification level |
| 180 | 180 | def project_watchers(project) |
| 181 | - project_members = project_notification_list(project) | |
| 181 | + project_members = users_project_notification(project) | |
| 182 | 182 | |
| 183 | - project_global= project_notification_list(project, Notification::N_GLOBAL) | |
| 184 | - group_global = group_notification_list(project, Notification::N_GLOBAL) | |
| 185 | - global_watch = User.where( | |
| 186 | - id: [project_global, group_global].flatten.uniq, | |
| 187 | - notification_level: Notification::N_WATCH | |
| 188 | - ).pluck(:id) | |
| 183 | + users_with_project_level_global = users_project_notification(project, Notification::N_GLOBAL) | |
| 184 | + users_with_group_level_global = users_group_notification(project, Notification::N_GLOBAL) | |
| 185 | + users = users_with_global_level_watch([users_with_project_level_global, users_with_group_level_global].flatten.uniq) | |
| 189 | 186 | |
| 190 | - project_watch = watch_project(project, project_global, global_watch) | |
| 191 | - group_watch = watch_group(project, project_members, group_global, global_watch) | |
| 187 | + users_with_project_setting = select_users_project_setting(project, users_with_project_level_global, users) | |
| 188 | + users_with_group_setting = select_users_group_setting(project, project_members, users_with_group_level_global, users) | |
| 192 | 189 | |
| 193 | - User.where(id: project_watch.concat(group_watch).uniq).to_a | |
| 190 | + User.where(id: users_with_project_setting.concat(users_with_group_setting).uniq).to_a | |
| 194 | 191 | end |
| 195 | 192 | |
| 196 | - def project_notification_list(project, notification_level=nil) | |
| 193 | + def users_project_notification(project, notification_level=nil) | |
| 197 | 194 | project_members = project.users_projects |
| 195 | + | |
| 198 | 196 | if notification_level |
| 199 | 197 | project_members.where(notification_level: notification_level).pluck(:user_id) |
| 200 | 198 | else |
| ... | ... | @@ -202,7 +200,7 @@ class NotificationService |
| 202 | 200 | end |
| 203 | 201 | end |
| 204 | 202 | |
| 205 | - def group_notification_list(project, notification_level) | |
| 203 | + def users_group_notification(project, notification_level) | |
| 206 | 204 | if project.group |
| 207 | 205 | project.group.users_groups.where(notification_level: notification_level).pluck(:user_id) |
| 208 | 206 | else |
| ... | ... | @@ -210,34 +208,47 @@ class NotificationService |
| 210 | 208 | end |
| 211 | 209 | end |
| 212 | 210 | |
| 213 | - def watch_project(project, global_setting, watch_global) | |
| 214 | - uids = project_notification_list(project, Notification::N_WATCH) | |
| 211 | + def users_with_global_level_watch(ids) | |
| 212 | + User.where( | |
| 213 | + id: ids, | |
| 214 | + notification_level: Notification::N_WATCH | |
| 215 | + ).pluck(:id) | |
| 216 | + end | |
| 217 | + | |
| 218 | + # Build a list of users based on project notifcation settings | |
| 219 | + def select_users_project_setting(project, global_setting, users_global_level_watch) | |
| 220 | + users = users_project_notification(project, Notification::N_WATCH) | |
| 215 | 221 | |
| 216 | - global_setting.each do |i| | |
| 217 | - if watch_global.include?(i) | |
| 218 | - uids << i | |
| 222 | + # If project setting is global, add to watch list if global setting is watch | |
| 223 | + global_setting.each do |user_id| | |
| 224 | + if users_global_level_watch.include?(user_id) | |
| 225 | + users << user_id | |
| 219 | 226 | end |
| 220 | 227 | end |
| 221 | - uids.uniq | |
| 228 | + | |
| 229 | + users | |
| 222 | 230 | end |
| 223 | 231 | |
| 224 | - def watch_group(project, project_members, global_setting, watch_global) | |
| 225 | - uids = group_notification_list(project, Notification::N_WATCH) | |
| 226 | - # Group setting is watch, add to watchers list user is not project member | |
| 227 | - watch = [] | |
| 228 | - uids.each do |i| | |
| 229 | - if project_members.exclude?(i) | |
| 230 | - watch << i | |
| 232 | + # Build a list of users based on group notifcation settings | |
| 233 | + def select_users_group_setting(project, project_members, global_setting, users_global_level_watch) | |
| 234 | + uids = users_group_notification(project, Notification::N_WATCH) | |
| 235 | + | |
| 236 | + # Group setting is watch, add to users list if user is not project member | |
| 237 | + users = [] | |
| 238 | + uids.each do |user_id| | |
| 239 | + if project_members.exclude?(user_id) | |
| 240 | + users << user_id | |
| 231 | 241 | end |
| 232 | 242 | end |
| 233 | 243 | |
| 234 | - global_setting.each do |i| | |
| 235 | - if project_members.exclude?(i) && watch_global.include?(i) | |
| 236 | - watch << i | |
| 244 | + # Group setting is global, add to users list if global setting is watch | |
| 245 | + global_setting.each do |user_id| | |
| 246 | + if project_members.exclude?(user_id) && users_global_level_watch.include?(user_id) | |
| 247 | + users << user_id | |
| 237 | 248 | end |
| 238 | 249 | end |
| 239 | 250 | |
| 240 | - watch.uniq | |
| 251 | + users | |
| 241 | 252 | end |
| 242 | 253 | |
| 243 | 254 | # Remove users with disabled notifications from array | ... | ... |