limiting notification counts (issue #112)

v1.18.x
Julian Lam 12 years ago
parent 48e14e9464
commit 123aac0862

@ -983,11 +983,17 @@ var utils = require('./../public/src/utils.js'),
User.notifications = { User.notifications = {
get: function(uid, callback) { get: function(uid, callback) {
var maxNotifs = 15;
async.parallel({ async.parallel({
unread: function(next) { unread: function(next) {
RDB.zrevrangebyscore('uid:' + uid + ':notifications:unread', 10, 0, function(err, nids) { RDB.zrevrangebyscore('uid:' + uid + ':notifications:unread', 10, 0, function(err, nids) {
// @todo handle err // @todo handle err
var unread = []; var unread = [];
// Cap the number of notifications returned
if (nids.length > maxNotifs) nids.length = maxNotifs;
if (nids && nids.length > 0) { if (nids && nids.length > 0) {
async.eachSeries(nids, function(nid, next) { async.eachSeries(nids, function(nid, next) {
notifications.get(nid, function(notif_data) { notifications.get(nid, function(notif_data) {
@ -1006,6 +1012,10 @@ var utils = require('./../public/src/utils.js'),
RDB.zrevrangebyscore('uid:' + uid + ':notifications:read', 10, 0, function(err, nids) { RDB.zrevrangebyscore('uid:' + uid + ':notifications:read', 10, 0, function(err, nids) {
// @todo handle err // @todo handle err
var read = []; var read = [];
// Cap the number of notifications returned
if (nids.length > maxNotifs) nids.length = maxNotifs;
if (nids && nids.length > 0) { if (nids && nids.length > 0) {
async.eachSeries(nids, function(nid, next) { async.eachSeries(nids, function(nid, next) {
notifications.get(nid, function(notif_data) { notifications.get(nid, function(notif_data) {
@ -1022,16 +1032,26 @@ var utils = require('./../public/src/utils.js'),
} }
}, function(err, notifications) { }, function(err, notifications) {
// While maintaining score sorting, sort by time // While maintaining score sorting, sort by time
var readCount = notifications.read.length,
unreadCount = notifications.unread.length;
notifications.read.sort(function(a, b) { notifications.read.sort(function(a, b) {
if (a.score === b.score) { if (a.score === b.score) {
return (a.datetime - b.datetime) > 0 ? -1 : 1; return (a.datetime - b.datetime) > 0 ? -1 : 1;
} }
}); });
notifications.unread.sort(function(a, b) { notifications.unread.sort(function(a, b) {
if (a.score === b.score) { if (a.score === b.score) {
return (a.datetime - b.datetime) > 0 ? -1 : 1; return (a.datetime - b.datetime) > 0 ? -1 : 1;
} }
}); });
// Limit the number of notifications to `maxNotifs`, prioritising unread notifications
if (notifications.read.length + notifications.unread.length > maxNotifs) {
notifications.read.length = maxNotifs - notifications.unread.length;
}
callback(notifications); callback(notifications);
}); });
}, },

Loading…
Cancel
Save