Created
August 21, 2013 04:32
-
-
Save tim-smart/6290338 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require('async'); | |
function getUserFriends(userName, next) { | |
var s = {} | |
db.users.findOne({name:userName}, foundUser); | |
function foundUser(err, user) { | |
if (err != null) return next(err); | |
s.user = user | |
getFriendsById(s.user.id, gotFriends); | |
} | |
function gotFriends(err, friends) { | |
if (err != null) return next(err); | |
s.friends = friends | |
if (s.user.type == 'power user') { | |
async.map(s.friends, getFriendsById, wtfFriendAction); | |
} else { | |
return next(null, s.friends); | |
} | |
} | |
function wtfFriendAction(err, friendsOfFriends) { | |
for (var i = 0; i < friendsOfFriends.length; i++) { | |
for (var j = 0; j < friendsOfFriends[i].length; j++) { | |
if (s.friends.indexOf(friendsOfFriends[i][j]) != -1) { | |
s.friends.push(friendsOfFriends[i][j]); | |
} | |
} | |
} | |
return next(null, s.friends); | |
} | |
} | |
function getFriendsById(userId, next) { | |
db.friends.find({userId:userId}, function (err, friends) { | |
if (err != null) return next(err); | |
return next(null, friends); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require('async'); | |
var getUserFriends = function (userName, next) { | |
db.users.findOne({name:userName}, function (err, user) { | |
if (err != null) return next(err); | |
getFriendsById(user.id, function (err, friends) { | |
if (err != null) return next(err); | |
if (user.type == 'power user') { | |
async.map(friends, getFriendsById, function (err, friendsOfFriends) { | |
for (var i = 0; i < friendsOfFriends.length; i++) { | |
for (var j = 0; j < friendsOfFriends[i].length; j++) { | |
if (friends.indexOf(friendsOfFriends[i][j]) != -1) { | |
friends.push(friendsOfFriends[i][j]); | |
} | |
} | |
} | |
return next(null, friends); | |
}); | |
} else { | |
return next(null, friends); | |
} | |
}); | |
}); | |
} | |
var getFriendsById = function (userId, next) { | |
db.friends.find({userId:userId}, function (err, friends) { | |
if (err != null) return next(err); | |
return next(null, friends); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment