cleanup and optimize notifications

v1.18.x
barisusakli
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,30 +87,16 @@ 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) { if (err) {
return callback(err); return callback(err);
} }
notifs = notifs.unread.concat(notifs.read);
var nids = results.unread.concat(results.read); notifs = notifs.filter(Boolean).sort(function(a, b) {
UserNotifications.getNotifications(nids, uid, function(err, notifs) { return b.datetime - a.datetime;
if (err) {
return callback(err);
}
notifs = notifs.filter(Boolean).sort(function(a, b) {
return b.datetime - a.datetime;
});
callback(null, notifs);
}); });
callback(null, notifs);
}); });
}; };
@ -119,34 +106,26 @@ var async = require('async'),
return callback(err); return callback(err);
} }
db.isSortedSetMembers('uid:' + uid + ':notifications:read', nids, function(err, hasRead) { var pids = notifications.map(function(notification) {
return notification ? notification.pid : null;
});
generatePostPaths(pids, uid, function(err, pidToPaths) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
var pids = notifications.map(function(notification) { notifications = notifications.map(function(notification, index) {
return notification ? notification.pid : null; if (!notification) {
}); return null;
generatePostPaths(pids, uid, function(err, pidToPaths) {
if (err) {
return callback(err);
} }
notifications = notifications.map(function(notification, index) { notification.path = pidToPaths[notification.pid] || notification.path || '';
if (!notification) { notification.datetimeISO = utils.toISOString(notification.datetime);
return null; return notification;
}
notification.read = hasRead[index];
notification.path = pidToPaths[notification.pid] || notification.path || '';
notification.datetimeISO = utils.toISOString(notification.datetime);
notification.readClass = !notification.read ? 'label-warning' : '';
return notification;
});
callback(null, notifications);
}); });
callback(null, notifications);
}); });
}); });
}; };
@ -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