diff --git a/src/notifications.js b/src/notifications.js index fde6a7209c..1310ff8f35 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -53,21 +53,30 @@ var async = require('async'), }); }; - Notifications.create = function(text, path, uniqueId, callback) { + Notifications.create = function(data, callback) { /** - * uniqueId is used solely to override stale nids. + * data.uniqueId is used solely to override stale nids. * If a new nid is pushed to a user and an existing nid in the user's * (un)read list contains the same uniqueId, it will be removed, and * the new one put in its place. */ + + // Add default values to data Object if not already set + var defaults = { + text: '', + path: null, + datetime: Date.now(), + uniqueId: utils.generateUUID() + }; + for(var v in defaults) { + if (defaults.hasOwnProperty(v) && !data[v]) { + data[v] = defaults[v]; + } + } + db.incrObjectField('global', 'nextNid', function(err, nid) { db.setAdd('notifications', nid); - db.setObject('notifications:' + nid, { - text: text || '', - path: path || null, - datetime: Date.now(), - uniqueId: uniqueId || utils.generateUUID() - }, function(err, status) { + db.setObject('notifications:' + nid, data, function(err, status) { if (!err) { callback(nid); } @@ -75,18 +84,6 @@ var async = require('async'), }); }; - function destroy(nid) { - - db.delete('notifications:' + nid, function(err, result) { - db.setRemove('notifications', nid, function(err, result) { - if (err) { - winston.error('Problem deleting expired notifications. Stack follows.'); - winston.error(err.stack); - } - }); - }); - } - Notifications.push = function(nid, uids, callback) { var websockets = require('./socket.io'); if (!Array.isArray(uids)) { diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js index 66f638cadb..f7e2b57369 100644 --- a/src/socket.io/modules.js +++ b/src/socket.io/modules.js @@ -103,7 +103,11 @@ SocketModules.chats.send = function(socket, data) { notifText = 'New message from ' + username + ''; if (!module.parent.exports.isUserOnline(touid)) { - notifications.create(notifText, 'javascript:app.openChat('' + username + '', ' + socket.uid + ');', 'notification_' + socket.uid + '_' + touid, function(nid) { + notifications.create({ + text: notifText, + path: 'javascript:app.openChat('' + username + '', ' + socket.uid + ');', + uniqueId: 'notification_' + socket.uid + '_' + touid + }, function(nid) { notifications.push(nid, [touid], function(success) { }); diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index fd14805f31..87f94c195f 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -260,7 +260,11 @@ SocketPosts.flag = function(socket, pid, callback) { }, function(adminGroup, next) { - notifications.create(message, path, 'post_flag:' + pid, function(nid) { + notifications.create({ + text: message, + path: path, + uniqueId: 'post_flag:' + pid + }, function(nid) { notifications.push(nid, adminGroup.members, function() { next(null); }); diff --git a/src/threadTools.js b/src/threadTools.js index cd3b770e32..da80168473 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -268,7 +268,11 @@ var winston = require('winston'), return next(err); } - notifications.create('' + username + ' has posted a reply to: "' + topicData.title + '"', nconf.get('relative_path') + '/topic/' + topicData.slug + '#' + pid, 'topic:' + tid, function(nid) { + notifications.create({ + text: '' + username + ' has posted a reply to: "' + topicData.title + '"', + path: nconf.get('relative_path') + '/topic/' + topicData.slug + '#' + pid, + uniqueId: 'topic:' + tid + }, function(nid) { next(null, nid); }); }); @@ -289,7 +293,7 @@ var winston = require('winston'), }); } ], function(err, results) { - if (!err) { + if (!err && results[1].length) { notifications.push(results[0], results[1]); } }); diff --git a/src/user.js b/src/user.js index d0a22520df..d3105325d5 100644 --- a/src/user.js +++ b/src/user.js @@ -710,13 +710,19 @@ var bcrypt = require('bcryptjs'), User.sendPostNotificationToFollowers = function(uid, tid, pid) { User.getUserField(uid, 'username', function(err, username) { db.getSetMembers('followers:' + uid, function(err, followers) { - topics.getTopicField(tid, 'slug', function(err, slug) { - var message = '' + username + ' made a new post'; - - notifications.create(message, nconf.get('relative_path') + '/topic/' + slug + '#' + pid, 'topic:' + tid, function(nid) { - notifications.push(nid, followers); + if (followers && followers.length) { + topics.getTopicField(tid, 'slug', function(err, slug) { + var message = '' + username + ' made a new post'; + + notifications.create({ + text: message, + path: nconf.get('relative_path') + '/topic/' + slug + '#' + pid, + uniqueId: 'topic:' + tid + }, function(nid) { + notifications.push(nid, followers); + }); }); - }); + } }); }); };