diff --git a/src/user/notifications.js b/src/user/notifications.js index 244cee7ba0..355dac53a2 100644 --- a/src/user/notifications.js +++ b/src/user/notifications.js @@ -3,6 +3,7 @@ var async = require('async'); var winston = require('winston'); +var _ = require('lodash'); var db = require('../database'); var meta = require('../meta'); @@ -45,17 +46,11 @@ function filterNotifications(nids, filter, callback) { } async.waterfall([ function (next) { - var keys = nids.map(function (nid) { - return 'notifications:' + nid; - }); + const keys = nids.map(nid => 'notifications:' + nid); db.getObjectsFields(keys, ['nid', 'type'], next); }, function (notifications, next) { - nids = notifications.filter(function (notification) { - return notification && notification.nid && notification.type === filter; - }).map(function (notification) { - return notification.nid; - }); + nids = notifications.filter(n => n && n.nid && n.type === filter).map(n => n.nid); next(null, nids); }, ], callback); @@ -65,17 +60,13 @@ UserNotifications.getAll = function (uid, filter, callback) { var nids; async.waterfall([ function (next) { - async.parallel({ - unread: function (next) { - db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, -1, next); - }, - read: function (next) { - db.getSortedSetRevRange('uid:' + uid + ':notifications:read', 0, -1, next); - }, - }, next); + db.getSortedSetRevRange([ + 'uid:' + uid + ':notifications:unread', + 'uid:' + uid + ':notifications:read', + ], 0, -1, next); }, - function (results, next) { - nids = results.unread.concat(results.read); + function (_nids, next) { + nids = _.uniq(_nids); db.isSortedSetMembers('notifications', nids, next); }, function (exists, next) { @@ -183,17 +174,12 @@ UserNotifications.getUnreadCount = function (uid, callback) { notifications.filterExists(nids, next); }, function (nids, next) { - var keys = nids.map(function (nid) { - return 'notifications:' + nid; - }); - + const keys = nids.map(nid => 'notifications:' + nid); db.getObjectsFields(keys, ['mergeId'], next); }, function (mergeIds, next) { // Collapse any notifications with identical mergeIds - mergeIds = mergeIds.map(function (set) { - return set.mergeId; - }); + mergeIds = mergeIds.map(set => set.mergeId); next(null, mergeIds.reduce(function (count, mergeId, idx, arr) { // A missing (null) mergeId means that notification is counted separately. @@ -219,15 +205,12 @@ UserNotifications.getUnreadByField = function (uid, field, values, callback) { return callback(null, []); } - var keys = nids.map(nid => 'notifications:' + nid); + const keys = nids.map(nid => 'notifications:' + nid); db.getObjectsFields(keys, ['nid', field], next); }, function (notifications, next) { const valuesSet = new Set(values.map(value => String(value))); - nids = notifications.filter(function (notification) { - return notification && notification[field] && valuesSet.has(String(notification[field])); - }).map(notification => notification.nid); - + nids = notifications.filter(n => n && n[field] && valuesSet.has(String(n[field]))).map(n => n.nid); next(null, nids); }, ], callback); @@ -237,13 +220,9 @@ UserNotifications.deleteAll = function (uid, callback) { if (parseInt(uid, 10) <= 0) { return setImmediate(callback); } - async.parallel([ - function (next) { - db.delete('uid:' + uid + ':notifications:unread', next); - }, - function (next) { - db.delete('uid:' + uid + ':notifications:read', next); - }, + db.deleteAll([ + 'uid:' + uid + ':notifications:unread', + 'uid:' + uid + ':notifications:read', ], callback); };