feat: user/notifications refactor

v1.18.x
Barış Soner Uşaklı 6 years ago
parent f2a6f888fb
commit 580f786095

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

Loading…
Cancel
Save