+ )
+}
+
+function updateVideo (req, res, finalCallback) {
+ const videoInstance = res.locals.video
+ const videoInfosToUpdate = req.body
+
+ waterfall([
+
+ function startTransaction (callback) {
+ db.sequelize.transaction().asCallback(function (err, t) {
+ return callback(err, t)
+ })
+ },
+
+ function findOrCreateTags (t, callback) {
+ if (videoInfosToUpdate.tags) {
+ db.Tag.findOrCreateTags(videoInfosToUpdate.tags, t, function (err, tagInstances) {
+ return callback(err, t, tagInstances)
+ })
+ } else {
+ return callback(null, t, null)
+ }
+ },
+
+ function updateVideoIntoDB (t, tagInstances, callback) {
+ const options = { transaction: t }
+
+ if (videoInfosToUpdate.name) videoInstance.set('name', videoInfosToUpdate.name)
+ if (videoInfosToUpdate.description) videoInstance.set('description', videoInfosToUpdate.description)
+
+ // Add tags association
+ videoInstance.save(options).asCallback(function (err) {
+ return callback(err, t, tagInstances)
+ })
+ },
+
+ function associateTagsToVideo (t, tagInstances, callback) {
+ if (tagInstances) {
+ const options = { transaction: t }
+
+ videoInstance.setTags(tagInstances, options).asCallback(function (err) {
+ videoInstance.Tags = tagInstances
+
+ return callback(err, t)
+ })
+ } else {
+ return callback(null, t)
+ }
+ },
+
+ function sendToFriends (t, callback) {
+ const json = videoInstance.toUpdateRemoteJSON()
+
+ // Now we'll update the video's meta data to our friends
+ friends.updateVideoToFriends(json, t, function (err) {
+ return callback(err, t)
+ })
+ }