notifications refactor

added getMultiple which works with an array of nids
v1.18.x
barisusakli 11 years ago
parent dd9a383f46
commit 5ae7c92d55

@ -478,7 +478,7 @@ accountsController.uploadPicture = function (req, res, next) {
}; };
accountsController.getNotifications = function(req, res, next) { accountsController.getNotifications = function(req, res, next) {
user.notifications.getAll(req.user.uid, null, null, function(err, notifications) { user.notifications.getAll(req.user.uid, 25, function(err, notifications) {
res.render('notifications', { res.render('notifications', {
notifications: notifications notifications: notifications
}); });

@ -24,14 +24,24 @@ var async = require('async'),
}; };
Notifications.get = function(nid, callback) { Notifications.get = function(nid, callback) {
db.getObject('notifications:' + nid, function(err, notification) { Notifications.getMultiple([nid], function(err, notifications) {
callback(err, Array.isArray(notifications) && notifications.length ? notifications[0] : null);
});
};
Notifications.getMultiple = function(nids, callback) {
var keys = nids.map(function(nid) {
return 'notifications:' + nid;
});
db.getObjects(keys, function(err, notifications) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
async.map(notifications, function(notification, next) {
if (!notification) { if (!notification) {
winston.info('[notifications.get] Could not retrieve nid ' + nid); return next(null, null);
return callback(null, null);
} }
// Backwards compatibility for old notification schema // Backwards compatibility for old notification schema
@ -48,10 +58,10 @@ var async = require('async'),
if (notification.from && !notification.image) { if (notification.from && !notification.image) {
User.getUserField(notification.from, 'picture', function(err, picture) { User.getUserField(notification.from, 'picture', function(err, picture) {
if (err) { if (err) {
return callback(err); return next(err);
} }
notification.image = picture; notification.image = picture;
callback(null, notification); next(null, notification);
}); });
return; return;
} else if (notification.image) { } else if (notification.image) {
@ -61,10 +71,10 @@ var async = require('async'),
break; break;
} }
return callback(null, notification); return next(null, notification);
} }
callback(null, notification); }, callback);
}); });
}; };

@ -17,23 +17,49 @@ var async = require('async'),
(function(UserNotifications) { (function(UserNotifications) {
UserNotifications.get = function(uid, callback) { UserNotifications.get = function(uid, callback) {
function getNotifications(set, start, stop, iterator, done) { var maxNotifs = 15;
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) {
return callback(err);
}
notifications.read = notifications.read.filter(Boolean);
notifications.unread = notifications.unread.filter(Boolean);
// 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(null, notifications);
});
};
function getNotificationsFromSet(set, uid, start, stop, max, callback) {
db.getSortedSetRevRange(set, start, stop, function(err, uniqueIds) { db.getSortedSetRevRange(set, start, stop, function(err, uniqueIds) {
if(err) { if (err) {
return done(err); return callback(err);
} }
if(!Array.isArray(uniqueIds) || !uniqueIds.length) { if(!Array.isArray(uniqueIds) || !uniqueIds.length) {
return done(null, []); return callback(null, []);
} }
if (uniqueIds.length > maxNotifs) { if (uniqueIds.length > max) {
uniqueIds.length = maxNotifs; uniqueIds.length = max;
} }
db.getObjectFields('uid:' + uid + ':notifications:uniqueId:nid', uniqueIds, function(err, uniqueIdToNids) { db.getObjectFields('uid:' + uid + ':notifications:uniqueId:nid', uniqueIds, function(err, uniqueIdToNids) {
if (err) { if (err) {
return done(err); return callback(err);
} }
var nidsToUniqueIds = {}; var nidsToUniqueIds = {};
@ -43,123 +69,82 @@ var async = require('async'),
nids.push(uniqueIdToNids[uniqueId]); nids.push(uniqueIdToNids[uniqueId]);
}); });
async.map(nids, function(nid, next) { UserNotifications.getNotifications(nids, uid, function(err, notifications) {
notifications.get(nid, function(err, notif_data) {
if (err) { if (err) {
return next(err); return callback(err);
} }
if (!notif_data) { notifications.forEach(function(notification, index) {
if (!notification) {
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
winston.info('[notifications.get] nid ' + nid + ' not found. Removing.'); winston.info('[notifications.get] nid ' + nids[index] + ' not found. Removing.');
} }
db.sortedSetRemove(set, nidsToUniqueIds[nid]); db.sortedSetRemove(set, nidsToUniqueIds[nids[index]]);
db.deleteObjectField('uid:' + uid + ':notifications:uniqueId:nid', nidsToUniqueIds[nid]); db.deleteObjectField('uid:' + uid + ':notifications:uniqueId:nid', nidsToUniqueIds[nids[index]]);
return next();
} }
});
if (typeof iterator === 'function') { callback(null, notifications);
iterator(notif_data, next);
} else {
next(null, notif_data);
}
}); });
}, done);
}); });
}); });
} }
var maxNotifs = 15; UserNotifications.getAll = function(uid, limit, callback) {
if (!limit || parseInt(limit, 10) <= 0) {
async.parallel({ limit = 25;
unread: function(next) {
getNotifications('uid:' + uid + ':notifications:unread', 0, 9, function(notif_data, next) {
notif_data.read = false;
notif_data.readClass = !notif_data.read ? 'label-warning' : '';
next(null, notif_data);
}, next);
},
read: function(next) {
getNotifications('uid:' + uid + ':notifications:read', 0, 9, function(notif_data, next) {
notif_data.read = true;
next(null, notif_data);
}, next);
}
}, function(err, notifications) {
function filterDeleted(notifObj) {
return !!notifObj;
} }
db.getObjectValues('uid:' + uid + ':notifications:uniqueId:nid', function(err, nids) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
notifications.read = notifications.read.filter(filterDeleted); UserNotifications.getNotifications(nids, uid, function(err, notifs) {
notifications.unread = notifications.unread.filter(filterDeleted); if (err) {
return callback(err);
// 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(null, notifications); notifs = notifs.filter(Boolean).sort(function(a, b) {
return parseInt(b.datetime, 10) - parseInt(a.datetime, 10);
}); });
};
UserNotifications.getAll = function(uid, limit, before, callback) {
var now = new Date();
if (!limit || parseInt(limit, 10) <= 0) { callback(null, notifs);
limit = 25; });
} });
if (before) { };
before = new Date(parseInt(before, 10));
}
db.getObjectValues('uid:' + uid + ':notifications:uniqueId:nid', function(err, nids) { UserNotifications.getNotifications = function(nids, uid, callback) {
notifications.getMultiple(nids, function(err, notifications) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
async.map(nids, function(nid, next) { var uniqueIds = notifications.map(function(notification) {
notifications.get(nid, function(err, notif_data) { return notification ? notification.uniqueId : null;
if (err || !notif_data) {
return next(err);
}
UserNotifications.isNotificationRead(notif_data.nid, uid, function(err, isRead) {
if (err) {
return next(err);
}
notif_data.read = isRead;
next(null, notif_data);
}); });
});
}, function(err, notifs) { db.isSortedSetMembers('uid:' + uid + ':notifications:read', uniqueIds, function(err, hasRead) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
notifs = notifs.filter(function(notif) { notifications = notifications.map(function(notification, index) {
return !!notif; if (!notification) {
}).sort(function(a, b) { return null;
return parseInt(b.datetime, 10) - parseInt(a.datetime, 10); }
}).map(function(notif) { notification.read = hasRead[index];
notif.datetimeISO = utils.toISOString(notif.datetime); notification.datetimeISO = utils.toISOString(notification.datetime);
notif.readClass = !notif.read ? 'label-warning' : ''; notification.readClass = !notification.read ? 'label-warning' : '';
return notif; return notification;
}); });
callback(null, notifs); callback(null, notifications);
}); });
}); });
}; };
UserNotifications.isNotificationRead = function(nid, uid, callback) {
db.isSortedSetMember('uid:' + uid + ':notifications:read', nid, callback);
};
UserNotifications.getDailyUnread = function(uid, callback) { UserNotifications.getDailyUnread = function(uid, callback) {
var now = Date.now(), var now = Date.now(),
yesterday = now - (1000*60*60*24); // Approximate, can be more or less depending on time changes, makes no difference really. yesterday = now - (1000*60*60*24); // Approximate, can be more or less depending on time changes, makes no difference really.
@ -182,9 +167,7 @@ var async = require('async'),
return uniqueIdToNids[uniqueId]; return uniqueIdToNids[uniqueId];
}); });
async.map(nids, function(nid, next) { UserNotifications.getNotifications(nids, uid, callback);
notifications.get(nid, next);
}, callback);
}); });
}); });
}; };
@ -212,15 +195,17 @@ var async = require('async'),
return uniqueIdsToNids[uniqueId]; return uniqueIdsToNids[uniqueId];
}); });
async.filter(nids, function(nid, next) { UserNotifications.getNotifications(nids, uid, function(err, notifications) {
notifications.get(nid, function(err, notifObj) { if (err) {
if (err || !notifObj) { return callback(err);
return next(false);
} }
next(notifObj[field] === value.toString()); notifications = notifications.filter(function(notification) {
return !!notification || notification[field] !== value.toString();
}).map(function(notification) {
return notification.nid;
}); });
}, function(nids) {
callback(null, nids); callback(null, nids);
}); });
}); });

Loading…
Cancel
Save