Files
NodeBB/src/messaging/notifications.js

96 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-12-16 11:15:43 +02:00
'use strict';
var async = require('async');
var user = require('../user');
2015-12-16 15:09:14 +02:00
var notifications = require('../notifications');
2015-12-16 11:15:43 +02:00
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,
2017-04-26 16:02:15 -06:00
uids: uids,
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({
2017-03-14 23:03:03 +03:00
type: 'new-chat',
2017-02-22 13:34:57 +03:00
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);
}
2015-12-16 11:15:43 +02:00
}
});
}
2017-02-18 02:30:48 -07:00
};