cleanup and optimize notifications

v1.18.x
barisusakli 11 years ago
parent cfd7edbf34
commit 7bf655541c

@ -264,7 +264,7 @@ var async = require('async'),
}; };
Notifications.markAllRead = function(uid, callback) { Notifications.markAllRead = function(uid, callback) {
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, 99, function(err, nids) { db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, function(err, nids) {
if (err) { if (err) {
return callback(err); return callback(err);
} }

@ -19,16 +19,7 @@ var async = require('async'),
(function(UserNotifications) { (function(UserNotifications) {
UserNotifications.get = function(uid, callback) { UserNotifications.get = function(uid, callback) {
var maxNotifs = 15; getNotifications(uid, 10, function(err, notifications) {
async.parallel({
unread: function(next) {
getNotificationsFromSet('uid:' + uid + ':notifications:unread', uid, 0, 9, maxNotifs, next);
},
read: function(next) {
getNotificationsFromSet('uid:' + uid + ':notifications:read', uid, 0, 9, maxNotifs, next);
}
}, function(err, notifications) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
@ -36,7 +27,7 @@ var async = require('async'),
notifications.read = notifications.read.filter(Boolean); notifications.read = notifications.read.filter(Boolean);
notifications.unread = notifications.unread.filter(Boolean); notifications.unread = notifications.unread.filter(Boolean);
// Limit the number of notifications to `maxNotifs`, prioritising unread notifications var maxNotifs = 15;
if (notifications.read.length + notifications.unread.length > maxNotifs) { if (notifications.read.length + notifications.unread.length > maxNotifs) {
notifications.read.length = maxNotifs - notifications.unread.length; notifications.read.length = maxNotifs - notifications.unread.length;
} }
@ -45,7 +36,18 @@ var async = require('async'),
}); });
}; };
function getNotificationsFromSet(set, uid, start, stop, max, callback) { function getNotifications(uid, count, callback) {
async.parallel({
unread: function(next) {
getNotificationsFromSet('uid:' + uid + ':notifications:unread', false, uid, 0, count - 1, next);
},
read: function(next) {
getNotificationsFromSet('uid:' + uid + ':notifications:read', true, uid, 0, count - 1, next);
}
}, callback);
}
function getNotificationsFromSet(set, read, uid, start, stop, callback) {
db.getSortedSetRevRange(set, start, stop, function(err, nids) { db.getSortedSetRevRange(set, start, stop, function(err, nids) {
if (err) { if (err) {
return callback(err); return callback(err);
@ -55,10 +57,6 @@ var async = require('async'),
return callback(null, []); return callback(null, []);
} }
if (nids.length > max) {
nids.length = max;
}
UserNotifications.getNotifications(nids, uid, function(err, notifications) { UserNotifications.getNotifications(nids, uid, function(err, notifications) {
if (err) { if (err) {
return callback(err); return callback(err);
@ -73,6 +71,9 @@ var async = require('async'),
} }
deletedNids.push(nids[index]); deletedNids.push(nids[index]);
} else {
notification.read = read;
notification.readClass = !notification.read ? 'label-warning' : '';
} }
}); });
@ -86,31 +87,17 @@ var async = require('async'),
} }
UserNotifications.getAll = function(uid, count, callback) { UserNotifications.getAll = function(uid, count, callback) {
async.parallel({ getNotifications(uid, count, function(err, notifs) {
unread: function(next) {
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, count, next);
},
read: function(next) {
db.getSortedSetRevRange('uid:' + uid + ':notifications:read', 0, count, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
var nids = results.unread.concat(results.read);
UserNotifications.getNotifications(nids, uid, function(err, notifs) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
notifs = notifs.unread.concat(notifs.read);
notifs = notifs.filter(Boolean).sort(function(a, b) { notifs = notifs.filter(Boolean).sort(function(a, b) {
return b.datetime - a.datetime; return b.datetime - a.datetime;
}); });
callback(null, notifs); callback(null, notifs);
}); });
});
}; };
UserNotifications.getNotifications = function(nids, uid, callback) { UserNotifications.getNotifications = function(nids, uid, callback) {
@ -119,11 +106,6 @@ var async = require('async'),
return callback(err); return callback(err);
} }
db.isSortedSetMembers('uid:' + uid + ':notifications:read', nids, function(err, hasRead) {
if (err) {
return callback(err);
}
var pids = notifications.map(function(notification) { var pids = notifications.map(function(notification) {
return notification ? notification.pid : null; return notification ? notification.pid : null;
}); });
@ -138,17 +120,14 @@ var async = require('async'),
return null; return null;
} }
notification.read = hasRead[index];
notification.path = pidToPaths[notification.pid] || notification.path || ''; notification.path = pidToPaths[notification.pid] || notification.path || '';
notification.datetimeISO = utils.toISOString(notification.datetime); notification.datetimeISO = utils.toISOString(notification.datetime);
notification.readClass = !notification.read ? 'label-warning' : '';
return notification; return notification;
}); });
callback(null, notifications); callback(null, notifications);
}); });
}); });
});
}; };
function generatePostPaths(pids, uid, callback) { function generatePostPaths(pids, uid, callback) {
@ -206,7 +185,7 @@ var async = require('async'),
return callback(null, []); return callback(null, []);
} }
UserNotifications.getNotifications(nids, uid, callback); UserNotifications.getNotifications(nids, uid);
}); });
}; };

Loading…
Cancel
Save