-
-
Save myndzi/6101585 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Friend.findOne({ | |
userId: req.signedCookies.userid | |
}, function (err, signedInUser) { | |
var friends = signedInUser.friends.map( | |
function (a) { return { user: a, type: 'friend' }; } | |
), | |
sentRequests = signedInUser.request_sent_to.map( | |
function (a) { return { user: a, type: 'sent' }; } | |
), | |
receivedRequests = signedInUser.request_received_from.map( | |
function (a) { return { user: a, type: 'received' }; } | |
); | |
var res = [ { user: signedInUser, type: 'self' } ] | |
.concat(friends) | |
.concat(sentRequests) | |
.concat(receivedRequests); | |
// weed out any duplicates (erroneous!) | |
var seen = []; | |
res = res.filter(function (a) { | |
if (seen.indexOf(a) === -1) { | |
seen.push(a); | |
return true; | |
} | |
console.error('Friend state error:', { | |
'self': loggedInUser, | |
'culprit': a | |
}); | |
return false; | |
}); | |
doSomethingWith(res); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment