Commit ce30a56f333f61c01314cf32e15381080a9b14d3

Authored by Marin Jankovski
1 parent f4f0a7e0

Add or remove watchers from list based on their notification settings.

Showing 1 changed file with 31 additions and 15 deletions   Show diff stats
app/services/notification_service.rb
... ... @@ -178,22 +178,24 @@ class NotificationService
178 178  
179 179 # Get project users with WATCH notification level
180 180 def project_watchers(project)
181   - # Gather all user ids that have WATCH notification setting for project
182   - project_notification_uids = project_notification_list(project, Notification::N_WATCH)
  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)
183 183  
184   - # Gather all user ids that have WATCH notification setting for group
185   - group_notification_uids = group_notification_list(project, Notification::N_WATCH)
  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)
186 188  
187   - # Gather all user ids that have GLOBAL setting
188   - global_notification_uids = global_notification_list(project)
  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)
189 191  
190   - project_and_group_uids = [project_notification_uids, group_notification_uids].flatten.uniq
191   - group_and_project_watchers = User.where(id: project_and_group_uids)
  192 + # Select watchers based on what the group setting is
  193 + group_watchers, ids = select_watchers(user_ids, global_setting_group, group_uids)
192 194  
193   - # Find all users that have WATCH as their GLOBAL setting
194   - global_watchers = User.where(id: global_notification_uids, notification_level: Notification::N_WATCH)
  195 + project_watchers.concat(group_watchers.concat(ids)).uniq
  196 + watchers = User.where(id: project_watchers)
195 197  
196   - [group_and_project_watchers, global_watchers].flatten.uniq
  198 + watchers.to_a
197 199 end
198 200  
199 201 def project_notification_list(project, notification_level)
... ... @@ -208,11 +210,25 @@ class NotificationService
208 210 end
209 211 end
210 212  
211   - def global_notification_list(project)
  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)
  222 + end
  223 + end
  224 + [watchers, user_ids]
  225 + end
  226 +
  227 + def project_and_group_watchers(project, notification_level)
212 228 [
213   - project_notification_list(project, Notification::N_GLOBAL),
214   - group_notification_list(project, Notification::N_GLOBAL)
215   - ].flatten
  229 + project_notification_list(project, notification_level),
  230 + group_notification_list(project, notification_level)
  231 + ]
216 232 end
217 233  
218 234 # Remove users with disabled notifications from array
... ...