v1.18.x
Julian Lam 11 years ago
parent 828fd6b978
commit b12b2f13ea

@ -29,7 +29,7 @@ var async = require('async'),
if (exists) { if (exists) {
db.sortedSetRank('uid:' + uid + ':notifications:read', nid, function(err, rank) { db.sortedSetRank('uid:' + uid + ':notifications:read', nid, function(err, rank) {
db.getObjectFields('notifications:' + nid, ['nid', 'from', 'text', 'score', 'path', 'datetime', 'uniqueId'], function(err, notification) { db.getObjectFields('notifications:' + nid, ['nid', 'from', 'text', 'importance', 'score', 'path', 'datetime', 'uniqueId'], function(err, notification) {
notification.read = rank !== null ? true:false; notification.read = rank !== null ? true:false;
if (notification.from) { if (notification.from) {
@ -74,6 +74,7 @@ var async = require('async'),
var defaults = { var defaults = {
text: '', text: '',
path: null, path: null,
importance: 5,
datetime: Date.now(), datetime: Date.now(),
uniqueId: utils.generateUUID() uniqueId: utils.generateUUID()
}; };
@ -84,6 +85,7 @@ var async = require('async'),
} }
db.incrObjectField('global', 'nextNid', function(err, nid) { db.incrObjectField('global', 'nextNid', function(err, nid) {
data.nid = nid;
db.setAdd('notifications', nid); db.setAdd('notifications', nid);
db.setObject('notifications:' + nid, data, function(err, status) { db.setObject('notifications:' + nid, data, function(err, status) {
if (!err) { if (!err) {
@ -103,25 +105,26 @@ var async = require('async'),
x; x;
Notifications.get(nid, null, function(notif_data) { Notifications.get(nid, null, function(notif_data) {
for (x = 0; x < numUids; x++) { async.each(uids, function(uid, next) {
if (parseInt(uids[x], 10) > 0) { if (parseInt(uid, 10) > 0) {
(function(uid) { checkReplace(notif_data.uniqueId, uid, notif_data, function(replace) {
remove_by_uniqueId(notif_data.uniqueId, uid, function() { if (replace) {
db.sortedSetAdd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid); db.sortedSetAdd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid);
websockets.in('uid_' + uid).emit('event:new_notification', notif_data); websockets.in('uid_' + uid).emit('event:new_notification', notif_data);
}
if (callback) { if (callback) {
callback(true); callback(true);
} }
}); });
})(uids[x]);
}
} }
}); });
});
}; };
function remove_by_uniqueId(uniqueId, uid, callback) { function checkReplace(uniqueId, uid, newNotifObj, callback) {
var replace = false, matched = false;
async.parallel([ async.parallel([
function(next) { function(next) {
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) { db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
@ -129,8 +132,12 @@ var async = require('async'),
async.each(nids, function(nid, next) { async.each(nids, function(nid, next) {
Notifications.get(nid, uid, function(nid_info) { Notifications.get(nid, uid, function(nid_info) {
if (nid_info && nid_info.uniqueId === uniqueId) { if (nid_info && nid_info.uniqueId === uniqueId) {
matched = true;
if ((nid_info.importance || 5) >= newNotifObj.importance) {
replace = true;
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid); db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid);
} }
}
next(); next();
}); });
@ -148,8 +155,12 @@ var async = require('async'),
async.each(nids, function(nid, next) { async.each(nids, function(nid, next) {
Notifications.get(nid, uid, function(nid_info) { Notifications.get(nid, uid, function(nid_info) {
if (nid_info && nid_info.uniqueId === uniqueId) { if (nid_info && nid_info.uniqueId === uniqueId) {
matched = true;
if ((nid_info.importance || 5) >= newNotifObj.importance) {
replace = true;
db.sortedSetRemove('uid:' + uid + ':notifications:read', nid); db.sortedSetRemove('uid:' + uid + ':notifications:read', nid);
} }
}
next(); next();
}); });
@ -163,7 +174,11 @@ var async = require('async'),
} }
], function(err) { ], function(err) {
if (!err) { if (!err) {
callback(true); if (replace === false && matched === false) {
replace = true;
}
callback(replace);
} }
}); });
} }

Loading…
Cancel
Save