refactored chat notification system to wait a bit before sending notif/email, closes #2098

This commit is contained in:
Julian Lam
2014-09-16 13:27:26 -04:00
parent 451c5b40fd
commit b85ff61de6
5 changed files with 61 additions and 23 deletions

View File

@@ -2,16 +2,19 @@
var db = require('./database'),
async = require('async'),
nconf = require('nconf'),
winston = require('winston'),
user = require('./user'),
plugins = require('./plugins'),
meta = require('./meta'),
utils = require('../public/src/utils'),
notifications = require('./notifications'),
userNotifications = require('./user/notifications');
userNotifications = require('./user/notifications'),
websockets = require('./socket.io'),
emailer = require('./emailer');
(function(Messaging) {
Messaging.notifyQueue = {}; // Only used to notify a user of a new chat message, see Messaging.notifyUser
function sortUids(fromuid, touid) {
return [fromuid, touid].sort();
@@ -336,6 +339,52 @@ var db = require('./database'),
}
return (matrix[b.length][a.length] / b.length < 0.1);
};
Messaging.notifyUser = function(fromuid, touid, messageObj) {
var queueObj = Messaging.notifyQueue[fromuid + ':' + touid];
if (queueObj) {
queueObj.message.content += '\n' + messageObj.content;
clearTimeout(queueObj.timeout);
} else {
queueObj = Messaging.notifyQueue[fromuid + ':' + touid] = {
message: messageObj
};
}
queueObj.timeout = setTimeout(function() {
sendNotifications(fromuid, touid, queueObj.message, function(err) {
if (!err) {
delete Messaging.notifyQueue[fromuid + ':' + touid];
}
});
}, 1000*60); // wait 60s before sending
};
function sendNotifications(fromuid, touid, messageObj, callback) {
// todo #1798 -- this should check if the user is in room `chat_{uidA}_{uidB}` instead, see `Sockets.uidInRoom(uid, room);`
if (!websockets.isUserOnline(touid)) {
notifications.create({
bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
bodyLong: messageObj.content,
path: nconf.get('relative_path') + '/chats/' + utils.slugify(messageObj.fromUser.username),
nid: 'chat_' + fromuid + '_' + touid,
from: fromuid
}, function(err, notification) {
if (!err && notification) {
notifications.push(notification, [touid], callback);
}
});
emailer.send('notif_chat', touid, {
subject: '[[email:notif.chat.subject, ' + messageObj.fromUser.username + ']]',
username: messageObj.toUser.username,
summary: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
message: messageObj,
site_title: meta.config.site_title || 'NodeBB',
url: nconf.get('url') + '/chats/' + utils.slugify(messageObj.fromUser.username)
});
}
}
}(exports));