Created
May 11, 2014 05:22
-
-
Save henryboldi/f65dcbcfb2e7724de934 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
// render the profile view (e.g. /views/show.ejs) | |
show: function(req, res, next) { | |
User.findOne({ username: req.param('id') }, function foundUser(err, user) { | |
if (err) return next(err); | |
if (!user) return next(); | |
Hacks.find({ owner: user.id }.sort('createdAt DESC').exec(function foundHacks(err, hacks){ | |
hackID = hacks.map(function (item){ return item.id}); | |
Comment.find().where({ postID: hackID }).exec(function(err, comments){ | |
if(err) return next(err); | |
if (!comments) return next(); | |
Follow.find({userFollowed: user.id}).sort('createdAt DESC').exec(function foundFollowers(err, followers){ | |
if(err) return next(err); | |
if (!followers) return next(); | |
if (followers == '') { | |
//checking if has any followers to display | |
var allFollowers = ''; | |
} else if (followers) { | |
var allFollowers = followers.map(function (item){ return item.follower }); | |
} | |
User.find({id: allFollowers}).sort('createdAt DESC').exec(function foundFollowers(err, userFollowers){ | |
if(err) return next(err); | |
if (!userFollowers) return next(); | |
Follow.find({userFollowed: user.id, follower: req.session.User.id }).sort('createdAt DESC').exec(function foundFollowMessage(err, followMessageInit){ | |
if(err) return next(err); | |
if(followMessageInit != '') { | |
var followMessage = 'Unfollow'; | |
} else if ( user.id == req.session.User.id ) { | |
var followMessage = false; | |
} else { | |
var followMessage = 'Follow'; | |
} | |
Follow.find({follower: user.id }).sort('createdAt DESC').exec(function foundFollowMessage(err, following){ | |
if(err) return next(err); | |
if (following == '') { | |
//checking if has any followers to display | |
var allFollowing = ''; | |
} else if (following) { | |
var allFollowing = following.map(function (item){ return item.userFollowed }); | |
} | |
User.find({id: allFollowing}).sort('createdAt DESC').exec(function foundFollowers(err, userFollowing){ | |
if(err) return next(err); | |
if (!userFollowing) return next(); | |
res.view({ | |
user: user, | |
hacks: hacks, | |
followers: followers, | |
followOrUnfollow: followMessage, | |
following: following, | |
userFollowers: userFollowers, | |
userFollowing: userFollowing, | |
comments: comments | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment