+ 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)
+
+ 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)
+ })
+ }
+
+ ], function andFinally (err, t) {
+ if (err) {
+ logger.debug('Cannot update the video.', { error: err })
+
+ // Abort transaction?
+ if (t) t.rollback()
+
+ // Force fields we want to update
+ // If the transaction is retried, sequelize will think the object has not changed
+ // So it will skip the SQL request, even if the last one was ROLLBACKed!
+ Object.keys(videoFieldsSave).forEach(function (key) {
+ const value = videoFieldsSave[key]
+ videoInstance.set(key, value)
+ })
+
+ return finalCallback(err)
+ }
+
+ // Commit transaction
+ t.commit().asCallback(function (err) {
+ if (err) return finalCallback(err)
+
+ logger.info('Video with name %s updated.', videoInfosToUpdate.name)
+ return finalCallback(null)
+ })