@@ -9,6 +9,7 @@ const config = require('./config');
99const notificationServer = require ( '../index' ) ;
1010const _ = require ( 'lodash' ) ;
1111const service = require ( './service' ) ;
12+ const helpers = require ( './helpers' ) ;
1213const { BUS_API_EVENT } = require ( './constants' ) ;
1314const EVENTS = require ( './events-config' ) . EVENTS ;
1415const PROJECT_ROLE_RULES = require ( './events-config' ) . PROJECT_ROLE_RULES ;
@@ -246,6 +247,83 @@ const getNotificationsForTopicStarter = (eventConfig, topicId) => {
246247 } ) ;
247248} ;
248249
250+ /**
251+ * Filter members by project roles
252+ *
253+ * @params {Array} List of project roles
254+ * @params {Array} List of project members
255+ *
256+ * @returns {Array } List of objects with user ids
257+ */
258+ const filterMembersByRoles = ( roles , members ) => {
259+ let result = [ ] ;
260+
261+ roles . forEach ( projectRole => {
262+ result = result . concat (
263+ _ . filter ( members , PROJECT_ROLE_RULES [ projectRole ] )
264+ . map ( projectMember => ( {
265+ userId : projectMember . userId . toString ( ) ,
266+ } ) )
267+ ) ;
268+ } ) ;
269+
270+ return result ;
271+ } ;
272+
273+ /**
274+ * Exclude private posts notification
275+ *
276+ * @param {Object } eventConfig event configuration
277+ * @param {Object } project project details
278+ * @param {Array } tags list of message tags
279+ *
280+ * @return {Promise } resolves to a list of notifications
281+ */
282+ const getExcludedPrivatePostNotifications = ( eventConfig , project , tags ) => {
283+ // skip if message is not private or exclusion rule is not configured
284+ if ( ! _ . includes ( tags , 'MESSAGES' ) || ! eventConfig . privatePostsForProjectRoles ) {
285+ return Promise . resolve ( [ ] ) ;
286+ }
287+
288+ const members = _ . get ( project , 'members' , [ ] ) ;
289+ const notifications = filterMembersByRoles ( eventConfig . privatePostsForProjectRoles , members ) ;
290+
291+ return Promise . resolve ( notifications ) ;
292+ } ;
293+
294+ /**
295+ * Exclude notifications about posts inside draft phases
296+ *
297+ * @param {Object } eventConfig event configuration
298+ * @param {Object } project project details
299+ * @param {Array } tags list of message tags
300+ *
301+ * @return {Promise } resolves to a list of notifications
302+ */
303+ const getExcludeDraftPhasesNotifications = ( eventConfig , project , tags ) => {
304+ // skip is no exclusion rule is configured
305+ if ( ! eventConfig . draftPhasesForProjectRoles ) {
306+ return Promise . resolve ( [ ] ) ;
307+ }
308+
309+ const phaseId = helpers . extractPhaseId ( tags ) ;
310+ // skip if it is not phase notification
311+ if ( ! phaseId ) {
312+ return Promise . resolve ( [ ] ) ;
313+ }
314+
315+ // exclude all user with configured roles if phase is in draft state
316+ return service . getPhase ( project . id , phaseId )
317+ . then ( ( phase ) => {
318+ if ( phase . status === 'draft' ) {
319+ const members = _ . get ( project , 'members' , [ ] ) ;
320+ const notifications = filterMembersByRoles ( eventConfig . draftPhasesForProjectRoles , members ) ;
321+
322+ return Promise . resolve ( notifications ) ;
323+ }
324+ } ) ;
325+ } ;
326+
249327/**
250328 * Exclude notifications using exclude rules of the event config
251329 *
@@ -272,12 +350,17 @@ const excludeNotifications = (notifications, eventConfig, message, data) => {
272350 // and after filter out such notifications from the notifications list
273351 // TODO move this promise all together with `_.uniqBy` to one function
274352 // and reuse it here and in `handler` function
353+ const tags = _ . get ( message , 'tags' , [ ] ) ;
354+
275355 return Promise . all ( [
276356 getNotificationsForTopicStarter ( excludeEventConfig , message . topicId ) ,
277357 getNotificationsForUserId ( excludeEventConfig , message . userId ) ,
278- getNotificationsForMentionedUser ( eventConfig , message . postContent ) ,
358+ getNotificationsForMentionedUser ( excludeEventConfig , message . postContent ) ,
279359 getProjectMembersNotifications ( excludeEventConfig , project ) ,
280360 getTopCoderMembersNotifications ( excludeEventConfig ) ,
361+ // these are special exclude rules which are only working for excluding notifications but not including
362+ getExcludedPrivatePostNotifications ( excludeEventConfig , project , tags ) ,
363+ getExcludeDraftPhasesNotifications ( excludeEventConfig , project , tags ) ,
281364 ] ) . then ( ( notificationsPerSource ) => (
282365 _ . uniqBy ( _ . flatten ( notificationsPerSource ) , 'userId' )
283366 ) ) . then ( ( excludedNotifications ) => {
0 commit comments