Commit 528a641e9ee5f72f3f5a1da1e1a31b9b61262efd

Authored by Marin Jankovski
1 parent ce30a56f

Use group notification settings while honoring project settings.

Showing 1 changed file with 41 additions and 32 deletions   Show diff stats
app/services/notification_service.rb
@@ -178,28 +178,28 @@ class NotificationService @@ -178,28 +178,28 @@ class NotificationService
178 178
179 # Get project users with WATCH notification level 179 # Get project users with WATCH notification level
180 def project_watchers(project) 180 def project_watchers(project)
181 - # Gather all user that have WATCH notification setting for project and group  
182 - project_uids, group_uids = project_and_group_watchers(project, Notification::N_WATCH) 181 + project_members = project_notification_list(project)
183 182
184 - # Gather all user that have WATCH as their GLOBAL setting  
185 - global_setting_project, global_setting_group = project_and_group_watchers(project, Notification::N_GLOBAL)  
186 - global_uids = [global_setting_project, global_setting_group].flatten.uniq  
187 - global_watchers_ids = User.where(id: global_uids, notification_level: Notification::N_WATCH).pluck(:id) 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)
188 189
189 - # Select watchers based on what the project setting is  
190 - project_watchers, user_ids = select_watchers(global_watchers_ids, global_setting_project, project_uids) 190 + project_watch = watch_project(project, project_global, global_watch)
  191 + group_watch = watch_group(project, project_members, group_global, global_watch)
191 192
192 - # Select watchers based on what the group setting is  
193 - group_watchers, ids = select_watchers(user_ids, global_setting_group, group_uids)  
194 -  
195 - project_watchers.concat(group_watchers.concat(ids)).uniq  
196 - watchers = User.where(id: project_watchers)  
197 -  
198 - watchers.to_a 193 + User.where(id: project_watch.concat(group_watch).uniq).to_a
199 end 194 end
200 195
201 - def project_notification_list(project, notification_level)  
202 - project.users_projects.where(notification_level: notification_level).pluck(:user_id) 196 + def project_notification_list(project, notification_level=nil)
  197 + project_members = project.users_projects
  198 + if notification_level
  199 + project_members.where(notification_level: notification_level).pluck(:user_id)
  200 + else
  201 + project_members.pluck(:user_id)
  202 + end
203 end 203 end
204 204
205 def group_notification_list(project, notification_level) 205 def group_notification_list(project, notification_level)
@@ -210,25 +210,34 @@ class NotificationService @@ -210,25 +210,34 @@ class NotificationService
210 end 210 end
211 end 211 end
212 212
213 - def select_watchers(user_ids, global_setting, setting)  
214 - watchers = []  
215 - user_ids.each do |i|  
216 - if setting.include?(i)  
217 - watchers << i  
218 - elsif global_setting.include?(i)  
219 - watchers << i  
220 - else  
221 - user_ids.delete(i) 213 + def watch_project(project, global_setting, watch_global)
  214 + uids = project_notification_list(project, Notification::N_WATCH)
  215 +
  216 + global_setting.each do |i|
  217 + if watch_global.include?(i)
  218 + uids << i
222 end 219 end
223 end 220 end
224 - [watchers, user_ids] 221 + uids.uniq
225 end 222 end
226 223
227 - def project_and_group_watchers(project, notification_level)  
228 - [  
229 - project_notification_list(project, notification_level),  
230 - group_notification_list(project, notification_level)  
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
  231 + end
  232 + end
  233 +
  234 + global_setting.each do |i|
  235 + if project_members.exclude?(i) && watch_global.include?(i)
  236 + watch << i
  237 + end
  238 + end
  239 +
  240 + watch.uniq
232 end 241 end
233 242
234 # Remove users with disabled notifications from array 243 # Remove users with disabled notifications from array