Files
NodeBB/src/messaging/notifications.js

140 lines
3.8 KiB
JavaScript
Raw Normal View History

2015-12-16 11:15:43 +02:00
'use strict';
var async = require('async');
var nconf = require('nconf');
2016-07-25 14:36:58 +03:00
var winston = require('winston');
2015-12-16 11:15:43 +02:00
var user = require('../user');
var emailer = require('../emailer');
2015-12-16 15:09:14 +02:00
var notifications = require('../notifications');
2015-12-16 11:15:43 +02:00
var meta = require('../meta');
var sockets = require('../socket.io');
2017-04-26 10:45:40 -06:00
var plugins = require('../plugins');
2015-12-16 11:15:43 +02:00
module.exports = function (Messaging) {
2015-12-16 11:30:10 +02:00
Messaging.notifyQueue = {}; // Only used to notify a user of a new chat message, see Messaging.notifyUser
2017-02-22 13:34:57 +03:00
Messaging.notificationSendDelay = 1000 * 60;
2015-12-16 11:15:43 +02:00
2017-02-22 13:34:57 +03:00
Messaging.notifyUsersInRoom = function (fromUid, roomId, messageObj) {
async.waterfall([
function (next) {
Messaging.getUidsInRoom(roomId, 0, -1, next);
},
function (uids, next) {
var data = {
roomId: roomId,
fromUid: fromUid,
2017-02-17 19:31:21 -07:00
message: messageObj,
2015-12-16 11:15:43 +02:00
};
plugins.fireHook('filter:messaging.notify', data, next);
2017-04-26 10:45:40 -06:00
},
function (data, next) {
if (!data || !data.uids || !data.uids.length) {
return next();
}
var uids = data.uids;
2017-02-22 13:34:57 +03:00
uids.forEach(function (uid) {
data.self = parseInt(uid, 10) === parseInt(fromUid, 10) ? 1 : 0;
2017-02-22 13:34:57 +03:00
Messaging.pushUnreadCount(uid);
sockets.in('uid_' + uid).emit('event:chats.receive', data);
2015-12-16 11:15:43 +02:00
});
2017-02-22 13:34:57 +03:00
// Delayed notifications
var queueObj = Messaging.notifyQueue[fromUid + ':' + roomId];
if (queueObj) {
queueObj.message.content += '\n' + messageObj.content;
clearTimeout(queueObj.timeout);
} else {
queueObj = {
message: messageObj,
2017-02-22 13:34:57 +03:00
};
Messaging.notifyQueue[fromUid + ':' + roomId] = queueObj;
2017-02-22 13:34:57 +03:00
}
2015-12-16 11:15:43 +02:00
2017-02-22 13:34:57 +03:00
queueObj.timeout = setTimeout(function () {
sendNotifications(fromUid, uids, roomId, queueObj.message);
}, Messaging.notificationSendDelay);
next();
},
2017-02-22 13:34:57 +03:00
]);
};
2015-12-16 11:15:43 +02:00
2017-02-22 13:34:57 +03:00
function sendNotifications(fromuid, uids, roomId, messageObj) {
async.waterfall([
function (next) {
user.isOnline(uids, next);
},
function (isOnline, next) {
uids = uids.filter(function (uid, index) {
return !isOnline[index] && parseInt(fromuid, 10) !== parseInt(uid, 10);
});
2015-12-16 11:15:43 +02:00
2017-02-22 13:34:57 +03:00
if (!uids.length) {
return;
2015-12-16 11:15:43 +02:00
}
2017-02-22 13:34:57 +03:00
notifications.create({
bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
bodyLong: messageObj.content,
nid: 'chat_' + fromuid + '_' + roomId,
from: fromuid,
path: '/chats/' + messageObj.roomId,
2017-02-22 13:34:57 +03:00
}, next);
},
2017-02-22 13:34:57 +03:00
], function (err, notification) {
if (!err) {
delete Messaging.notifyQueue[fromuid + ':' + roomId];
if (notification) {
notifications.push(notification, uids);
}
sendNotificationEmails(uids, messageObj);
}
2016-07-25 14:36:58 +03:00
});
}
function sendNotificationEmails(uids, messageObj) {
if (parseInt(meta.config.disableEmailSubscriptions, 10) === 1) {
return;
}
2017-02-22 13:34:57 +03:00
async.waterfall([
function (next) {
async.parallel({
userData: function (next) {
user.getUsersFields(uids, ['uid', 'username', 'userslug'], next);
},
userSettings: function (next) {
user.getMultipleUserSettings(uids, next);
},
2017-02-22 13:34:57 +03:00
}, next);
2016-07-25 14:36:58 +03:00
},
2017-02-22 13:34:57 +03:00
function (results, next) {
results.userData = results.userData.filter(function (userData, index) {
return userData && results.userSettings[index] && results.userSettings[index].sendChatNotifications;
});
async.each(results.userData, function (userData, next) {
emailer.send('notif_chat', userData.uid, {
subject: '[[email:notif.chat.subject, ' + messageObj.fromUser.username + ']]',
summary: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
message: messageObj,
site_title: meta.config.title || 'NodeBB',
url: nconf.get('url'),
roomId: messageObj.roomId,
username: userData.username,
userslug: userData.userslug,
2017-02-22 13:34:57 +03:00
}, next);
}, next);
2017-02-17 19:31:21 -07:00
},
2017-02-22 13:34:57 +03:00
], function (err) {
2016-07-25 14:36:58 +03:00
if (err) {
return winston.error(err);
2015-12-16 11:15:43 +02:00
}
});
}
2017-02-18 02:30:48 -07:00
};