3 var env
= require('gitter-web-env');
4 var errorReporter
= env
.errorReporter
;
5 var logger
= env
.logger
;
7 var appEvents
= require('gitter-web-appevents');
8 var presenceService
= require('gitter-web-presence');
9 var onlineNotificationGeneratorService
= require('../services/notifications/online-notification-generator-service');
10 var pushNotificationPostbox
= require('../services/notifications/push-notification-postbox');
11 var debug
= require('debug')('gitter:app:notification-event-listener');
12 var pushNotificationGateway
= require('../gateways/push-notification-gateway');
13 var pushNotificationFilter
= require('gitter-web-push-notification-filter');
16 // This installs the listeners that will listen to events
18 var installed
= false;
19 exports
.install = function() {
20 if (installed
) return;
23 /* New online notification */
24 appEvents
.onNewOnlineNotification(function(troupeId
, chatId
, userIds
) {
25 return onlineNotificationGeneratorService
26 .sendOnlineNotifications(troupeId
, chatId
, userIds
)
27 .catch(function(err
) {
28 logger
.error('Error while generating online notifications: ' + err
, { exception
: err
});
32 /* New push notification */
33 appEvents
.onNewPushNotificationForChat(function(troupeId
, chatId
, userIds
, mentioned
) {
34 return pushNotificationPostbox
35 .queueNotificationsForChat(troupeId
, chatId
, userIds
, mentioned
)
36 .catch(function(err
) {
37 logger
.error('Error while generating push notification: ' + err
, { exception
: err
});
41 /* Badge count update */
42 appEvents
.onBatchUserBadgeCountUpdate(function(data
) {
43 var userIds
= data
.userIds
;
44 debug('Publishing badge count updates for %s users.', userIds
.length
);
46 return pushNotificationGateway
.sendUsersBadgeUpdates(userIds
).catch(function(err
) {
47 logger
.error('Error while calling sendUsersBadgeUpdates. Silently ignoring. ' + err
, {
50 errorReporter(err
, { users
: userIds
}, { module
: 'notification-event-listener' });
54 presenceService
.on('eyeballSignal', function(userId
, troupeId
, eyeballSignal
) {
55 if (!eyeballSignal
) return; // Only clear the notifications when signals eyeballs on
57 return pushNotificationFilter
58 .resetNotificationsForUserTroupe(userId
, troupeId
)
59 .catch(function(err
) {
61 'Error while calling resetNotificationsForUserTroupe. Silently ignoring. ' + err
,