Created
March 31, 2014 17:17
-
-
Save npfitz/9897273 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
'timeline': function(req, res){ | |
Project.findOne(req.param("id")) | |
.populate("discussions") | |
.populate("moodboards") | |
.exec(function(err, project){ | |
var retval = []; | |
//First, do all the stuff for discussions | |
async.forEach(project.discussions, function(discussion, cb){ | |
var object = { | |
origin: "discussion", | |
action: "create", | |
createdAt: discussion.createdAt, | |
source: discussion | |
}; | |
User.findOne(discussion.creator) | |
.exec(function(err, user){ | |
object.creator = user; | |
retval.push(object); | |
cb(); | |
}) | |
}, function(err){ | |
async.forEach(project.moodboards, function(moodboard, cb){ | |
//Start building the event | |
var object = { | |
origin: "moodboard", | |
action: "create", | |
createdAt: moodboard.createdAt, | |
source: moodboard | |
}; | |
//Attatch the creator | |
User.findOne(moodboard.creator) | |
.exec(function(err, user){ | |
object.creator = user; | |
retval.push(object); | |
//Check to see if there are any images to include | |
MoodboardImage.find({ | |
moodboard: moodboard.id | |
}) | |
.populate("creator") | |
.exec(function(err, moodboardimages){ | |
var grouped_images = _.groupBy(moodboardimages, function(image){ | |
var created = image.createdAt; | |
return created.getDay().toString() + "-" + created.getHours().toString(); | |
}) | |
_.each(grouped_images, function(group){ | |
var group = _.groupBy(group, function(image){ | |
return image.creator.id | |
}) | |
_.each(group, function(update){ | |
var object = { | |
origin: "moodboard", | |
action: "update", | |
createdAt: update[0].createdAt, | |
images: update, | |
moodboard: moodboard, | |
creator: update[0].creator | |
} | |
retval.push(object); | |
}) | |
}) | |
cb(); | |
}) | |
}) | |
}, function(err){ | |
res.send(_.sortBy(retval, "createdAt").reverse()); | |
}) | |
}) | |
}) | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment