updating notifications.create to accept a hash instead of discrete arguments - breaking change

v1.18.x
Julian Lam 11 years ago
parent 17ae56e555
commit aba1b95cac

@ -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)) {

@ -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(&apos;' + username + '&apos;, ' + socket.uid + ');', 'notification_' + socket.uid + '_' + touid, function(nid) {
notifications.create({
text: notifText,
path: 'javascript:app.openChat(&apos;' + username + '&apos;, ' + socket.uid + ');',
uniqueId: 'notification_' + socket.uid + '_' + touid
}, function(nid) {
notifications.push(nid, [touid], function(success) {
});

@ -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);
});

@ -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]);
}
});

@ -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);
});
});
});
}
});
});
};

Loading…
Cancel
Save