Node.js based forum software built for the modern web
updating notifications.create to accept a hash instead of discrete arguments - breaking change
Julian Lamaba1b95cac2b
diff --git a/src/notifications.js b/src/notifications.jsindex 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.jsindex 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 <strong>' + username + '</strong>';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.jsindex 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.jsindex cd3b770e32..da80168473 100644--- a/src/threadTools.js+++ b/src/threadTools.js@@ -268,7 +268,11 @@ var winston = require('winston'),return next(err);}- notifications.create('<strong>' + username + '</strong> has posted a reply to: "<strong>' + topicData.title + '</strong>"', nconf.get('relative_path') + '/topic/' + topicData.slug + '#' + pid, 'topic:' + tid, function(nid) {+ notifications.create({+ text: '<strong>' + username + '</strong> has posted a reply to: "<strong>' + topicData.title + '</strong>"',+ 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.jsindex 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 = '<strong>' + username + '</strong> 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 = '<strong>' + username + '</strong> 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);+ });});- });+ }});});};