From c560f8fb75237362d8d5d97afbe4f9e90c4f8b5c Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Tue, 11 Feb 2014 23:38:25 -0500 Subject: [PATCH] reduced DRY fail --- src/user.js | 92 ++++++++++++++++++++--------------------------------- 1 file changed, 35 insertions(+), 57 deletions(-) diff --git a/src/user.js b/src/user.js index 685d5f2939..c340c5618a 100644 --- a/src/user.js +++ b/src/user.js @@ -956,69 +956,47 @@ var bcrypt = require('bcryptjs'), User.notifications = { get: function(uid, callback) { + + function getNotifications(set, start, stop, iterator, done) { + db.getSortedSetRevRange(set, start, stop, function(err, nids) { + if(err) { + return done(err); + } + + if(!nids || nids.length === 0) { + return done(null, []); + } + + if (nids.length > maxNotifs) { + nids.length = maxNotifs; + } + + async.map(nids, function(nid, next) { + notifications.get(nid, uid, function(notif_data) { + if(!notif_data) { + db.sortedSetRemove(set, nid); + } else { + if(typeof iterator === 'function') { + iterator(notif_data); + } + } + + next(null, notif_data); + }); + }, done); + }); + } + var maxNotifs = 15; async.parallel({ unread: function(next) { - db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) { - // @todo handle err - var unread = []; - - // Cap the number of notifications returned - if (nids.length > maxNotifs) { - nids.length = maxNotifs; - } - - if (nids && nids.length > 0) { - async.eachSeries(nids, function(nid, next) { - notifications.get(nid, uid, function(notif_data) { - // If the notification could not be found, silently drop it - if (notif_data) { - notif_data.readClass = !notif_data.read ? 'label-warning' : ''; - unread.push(notif_data); - } else { - db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid); - } - - next(); - }); - }, function(err) { - next(null, unread); - }); - } else { - next(null, unread); - } - }); + getNotifications('uid:' + uid + ':notifications:unread', 0, 10, function(notif_data) { + notif_data.readClass = !notif_data.read ? 'label-warning' : ''; + }, next); }, read: function(next) { - db.getSortedSetRevRange('uid:' + uid + ':notifications:read', 0, 10, function(err, nids) { - // @todo handle err - var read = []; - - // Cap the number of notifications returned - if (nids.length > maxNotifs) { - nids.length = maxNotifs; - } - - if (nids && nids.length > 0) { - async.eachSeries(nids, function(nid, next) { - notifications.get(nid, uid, function(notif_data) { - // If the notification could not be found, silently drop it - if (notif_data) { - read.push(notif_data); - } else { - db.sortedSetRemove('uid:' + uid + ':notifications:read', nid); - } - - next(); - }); - }, function(err) { - next(null, read); - }); - } else { - next(null, read); - } - }); + getNotifications('uid:' + uid + 'notifications:read', 0, 10, null, next); } }, function(err, notifications) { // Limit the number of notifications to `maxNotifs`, prioritising unread notifications