Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / event-listeners / notification-event-listener.js
blobc0c003cab575f621050dd4f8208bc9e2bb99d26c
1 'use strict';
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;
21 installed = true;
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 });
29 });
30 });
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 });
38 });
39 });
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, {
48 exception: err
49 });
50 errorReporter(err, { users: userIds }, { module: 'notification-event-listener' });
51 });
52 });
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) {
60 logger.error(
61 'Error while calling resetNotificationsForUserTroupe. Silently ignoring. ' + err,
62 { exception: err }
64 });
65 });