Commit c34186f88258aadd03c7e692606ff2499d221d2b

Authored by Marin Jankovski
1 parent e6fbf79b

Refactor project watchers collecting.

Showing 1 changed file with 33 additions and 13 deletions   Show diff stats
app/services/notification_service.rb
... ... @@ -178,21 +178,41 @@ class NotificationService
178 178  
179 179 # Get project users with WATCH notification level
180 180 def project_watchers(project)
181   - project_watchers = []
182   - member_methods = { project => :users_projects }
183   - member_methods.merge!(project.group => :users_groups) if project.group
184   -
185   - member_methods.each do |object, member_method|
186   - # Get project notification settings since it has higher priority
187   - user_ids = object.send(member_method).where(notification_level: Notification::N_WATCH).pluck(:user_id)
188   - project_watchers += User.where(id: user_ids)
189   -
190   - # next collect users who use global settings with watch state
191   - user_ids = object.send(member_method).where(notification_level: Notification::N_GLOBAL).pluck(:user_id)
192   - project_watchers += User.where(id: user_ids, notification_level: Notification::N_WATCH)
  181 + # Gather all user ids that have WATCH notification setting for project
  182 + project_notification_uids = project_notification_list(project, Notification::N_WATCH)
  183 +
  184 + # Gather all user ids that have WATCH notification setting for group
  185 + group_notification_uids = group_notification_list(project, Notification::N_WATCH)
  186 +
  187 + # Gather all user ids that have GLOBAL setting
  188 + global_notification_uids = global_notification_list(project)
  189 +
  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 +
  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 +
  196 + [group_and_project_watchers, global_watchers].flatten.uniq
  197 + end
  198 +
  199 + def project_notification_list(project, notification_level)
  200 + project.users_projects.where(notification_level: notification_level).pluck(:user_id)
  201 + end
  202 +
  203 + def group_notification_list(project, notification_level)
  204 + if project.group
  205 + project.group.users_groups.where(notification_level: notification_level).pluck(:user_id)
  206 + else
  207 + []
193 208 end
  209 + end
194 210  
195   - project_watchers.uniq
  211 + def global_notification_list(project)
  212 + [
  213 + project_notification_list(project, Notification::N_GLOBAL),
  214 + group_notification_list(project, Notification::N_GLOBAL)
  215 + ].flatten
196 216 end
197 217  
198 218 # Remove users with disabled notifications from array
... ...