error checks more err first, replaced a eachSeries with async.map

v1.18.x
Baris Soner Usakli 11 years ago
parent 1b5e44a763
commit a8e5c7d149

@ -248,7 +248,10 @@ var fs = require('fs'),
} }
if (userData) { if (userData) {
user.getFollowing(userData.uid, function (followingData) { user.getFollowing(userData.uid, function (err, followingData) {
if(err) {
return next(err);
}
userData.following = followingData; userData.following = followingData;
userData.followingCount = followingData.length; userData.followingCount = followingData.length;
res.json(userData); res.json(userData);
@ -271,7 +274,10 @@ var fs = require('fs'),
} }
if (userData) { if (userData) {
user.getFollowers(userData.uid, function (followersData) { user.getFollowers(userData.uid, function (err, followersData) {
if(err) {
return next(err);
}
userData.followers = followersData; userData.followers = followersData;
userData.followersCount = followersData.length; userData.followersCount = followersData.length;
res.json(userData); res.json(userData);
@ -634,14 +640,15 @@ var fs = require('fs'),
userData.disableSignatures = meta.config.disableSignatures !== undefined && parseInt(meta.config.disableSignatures, 10) === 1; userData.disableSignatures = meta.config.disableSignatures !== undefined && parseInt(meta.config.disableSignatures, 10) === 1;
user.getFollowingCount(userData.uid, function (followingCount) { user.getFollowStats(userData.uid, function (err, followStats) {
user.getFollowerCount(userData.uid, function (followerCount) { if(err) {
userData.followingCount = followingCount; return callback(err);
userData.followerCount = followerCount; }
userData.followingCount = followStats.followingCount;
userData.followerCount = followStats.followerCount;
callback(null, userData); callback(null, userData);
}); });
}); });
});
} }
}; };

@ -548,7 +548,7 @@ var bcrypt = require('bcryptjs'),
return usernamesHash[username]; return usernamesHash[username];
}); });
User.getDataForUsers(results, function(userdata) { User.getDataForUsers(results, function(err, userdata) {
var diff = process.hrtime(start); var diff = process.hrtime(start);
var timing = (diff[0] * 1e3 + diff[1] / 1e6).toFixed(1); var timing = (diff[0] * 1e3 + diff[1] / 1e6).toFixed(1);
callback(null, {timing: timing, users: userdata}); callback(null, {timing: timing, users: userdata});
@ -610,73 +610,76 @@ var bcrypt = require('bcryptjs'),
User.getFollowing = function(uid, callback) { User.getFollowing = function(uid, callback) {
db.getSetMembers('following:' + uid, function(err, userIds) { db.getSetMembers('following:' + uid, function(err, userIds) {
if (!err) { if(err) {
User.getDataForUsers(userIds, callback); return callback(err);
} else {
console.log(err);
} }
User.getDataForUsers(userIds, callback);
}); });
}; };
User.getFollowers = function(uid, callback) { User.getFollowers = function(uid, callback) {
db.getSetMembers('followers:' + uid, function(err, userIds) { db.getSetMembers('followers:' + uid, function(err, userIds) {
if (!err) { if(err) {
User.getDataForUsers(userIds, callback); return callback(err);
} else {
console.log(err);
} }
User.getDataForUsers(userIds, callback);
}); });
}; };
User.getFollowingCount = function(uid, callback) { User.getFollowingCount = function(uid, callback) {
db.getSetMembers('following:' + uid, function(err, userIds) { db.getSetMembers('following:' + uid, function(err, userIds) {
if (err) { if (err) {
console.log(err); return callback(err);
} else { }
userIds = userIds.filter(function(value) { userIds = userIds.filter(function(value) {
return parseInt(value, 10) !== 0; return parseInt(value, 10) !== 0;
}); });
callback(userIds.length); callback(null, userIds.length);
}
}); });
}; };
User.getFollowerCount = function(uid, callback) { User.getFollowerCount = function(uid, callback) {
db.getSetMembers('followers:' + uid, function(err, userIds) { db.getSetMembers('followers:' + uid, function(err, userIds) {
if(err) { if(err) {
console.log(err); return callback(err);
} else { }
userIds = userIds.filter(function(value) { userIds = userIds.filter(function(value) {
return parseInt(value, 10) !== 0; return parseInt(value, 10) !== 0;
}); });
callback(userIds.length); callback(null, userIds.length);
}
}); });
}; };
User.getFollowStats = function (uid, callback) {
async.parallel({
followingCount: function(next) {
User.getFollowingCount(uid, next);
},
followerCount : function(next) {
User.getFollowerCount(uid, next);
}
}, callback);
}
User.getDataForUsers = function(uids, callback) { User.getDataForUsers = function(uids, callback) {
var returnData = [];
if (!uids || !Array.isArray(uids) || uids.length === 0) { if (!uids || !Array.isArray(uids) || uids.length === 0) {
callback(returnData); return callback(null, []);
return;
} }
function iterator(uid, callback) { function getUserData(uid, next) {
if(parseInt(uid, 10) === 0) { if(parseInt(uid, 10) === 0) {
return callback(null); return next(null, null);
} }
User.getUserData(uid, function(err, userData) { User.getUserData(uid, next);
returnData.push(userData);
callback(null);
});
} }
async.eachSeries(uids, iterator, function(err) { async.map(uids, getUserData, callback);
callback(returnData);
});
}; };
User.sendPostNotificationToFollowers = function(uid, tid, pid) { User.sendPostNotificationToFollowers = function(uid, tid, pid) {

Loading…
Cancel
Save