faster checkAndReplace

still need a better solution for this uniqueId business but this is
better than before.
v1.18.x
barisusakli 11 years ago
parent 8aea4ad7fa
commit 7c00bce63c

@ -135,53 +135,41 @@ var async = require('async'),
function checkReplace(uniqueId, uid, newNotifObj, callback) { function checkReplace(uniqueId, uid, newNotifObj, callback) {
var replace = false, matched = false; var replace = false, matched = false;
async.parallel([ function checkAndRemove(set, next) {
function(next) { db.getSortedSetRange(set, 0, -1, function(err, nids) {
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) { if (err || !nids || !nids.length) {
if (nids && nids.length > 0) { return next(err);
async.each(nids, function(nid, next) {
Notifications.get(nid, uid, function(nid_info) {
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);
}
} }
next(); var keys = nids.map(function(nid) {
}); return 'notifications:' + nid;
}, function(err) {
next();
}); });
} else {
next(); db.getObjectsFields(keys, ['uniqueId', 'importance'], function(err, nid_infos) {
if (err) {
return next(err);
} }
});
}, nid_infos.forEach(function(nid_info) {
function(next) {
db.getSortedSetRange('uid:' + uid + ':notifications:read', 0, -1, function(err, nids) {
if (nids && nids.length > 0) {
async.each(nids, function(nid, next) {
Notifications.get(nid, uid, function(nid_info) {
if (nid_info && nid_info.uniqueId === uniqueId) { if (nid_info && nid_info.uniqueId === uniqueId) {
matched = true; matched = true;
if ((nid_info.importance || 5) >= newNotifObj.importance) { if ((nid_info.importance || 5) >= newNotifObj.importance) {
replace = true; replace = true;
db.sortedSetRemove('uid:' + uid + ':notifications:read', nid); db.sortedSetRemove(set, nid_info.nid);
} }
} }
next();
}); });
}, function(err) {
next();
}); });
} else {
next();
}
}); });
} }
async.parallel([
function(next) {
checkAndRemove('uid:' + uid + ':notifications:unread', next);
},
function(next) {
checkAndRemove('uid:' + uid + ':notifcations:read', next);
}
], function(err) { ], function(err) {
if (!err) { if (!err) {
if (replace === false && matched === false) { if (replace === false && matched === false) {

Loading…
Cancel
Save