remove dupe code

v1.18.x
Barış Soner Uşaklı 8 years ago
parent 6fb90a17e0
commit 114f416ec0

@ -26,6 +26,10 @@ module.exports = function (User) {
};
User.getUsersFields = function (uids, fields, callback) {
if (!Array.isArray(uids) || !uids.length) {
return callback(null, []);
}
var fieldsToRemove = [];
function addField(field) {
if (fields.indexOf(field) === -1) {
@ -34,23 +38,6 @@ module.exports = function (User) {
}
}
// Eliminate duplicates and build ref table
var uniqueUids = uids.filter(function (uid, index) {
return index === uids.indexOf(uid);
});
var ref = uniqueUids.reduce(function (memo, cur, idx) {
memo[cur] = idx;
return memo;
}, {});
if (!Array.isArray(uniqueUids) || !uniqueUids.length) {
return callback(null, []);
}
var keys = uniqueUids.map(function (uid) {
return 'user:' + uid;
});
if (fields.indexOf('uid') === -1) {
fields.push('uid');
}
@ -64,21 +51,17 @@ module.exports = function (User) {
addField('lastonline');
}
var uniqueUids = uids.filter(function (uid, index) {
return index === uids.indexOf(uid);
});
async.waterfall([
function (next) {
db.getObjectsFields(keys, fields, function (err, users) {
if (err) {
return callback(err);
}
users = uids.map(function (uid) {
return users[ref[uid]];
});
next(null, users);
});
db.getObjectsFields(uidsToUserKeys(uniqueUids), fields, next);
},
function (users, next) {
users = uidsToUsers(uids, uniqueUids, users);
modifyUserData(users, fieldsToRemove, next);
},
], callback);
@ -100,39 +83,39 @@ module.exports = function (User) {
return callback(null, []);
}
// Eliminate duplicates and build ref table
var uniqueUids = uids.filter(function (uid, index) {
return index === uids.indexOf(uid);
});
var ref = uniqueUids.reduce(function (memo, cur, idx) {
memo[cur] = idx;
return memo;
}, {});
var keys = uniqueUids.map(function (uid) {
return 'user:' + uid;
});
async.waterfall([
function (next) {
db.getObjects(keys, function (err, users) {
if (err) {
return callback(err);
}
users = uids.map(function (uid) {
return users[ref[uid]];
});
next(null, users);
});
db.getObjects(uidsToUserKeys(uniqueUids), next);
},
function (users, next) {
users = uidsToUsers(uids, uniqueUids, users);
modifyUserData(users, [], next);
},
], callback);
};
function uidsToUsers(uids, uniqueUids, usersData) {
var ref = uniqueUids.reduce(function (memo, cur, idx) {
memo[cur] = idx;
return memo;
}, {});
var users = uids.map(function (uid) {
return usersData[ref[uid]];
});
return users;
}
function uidsToUserKeys(uids) {
return uids.map(function (uid) {
return 'user:' + uid;
});
}
function modifyUserData(users, fieldsToRemove, callback) {
users.forEach(function (user) {
if (!user) {

Loading…
Cancel
Save