- const videoInfosToUpdate: VideoUpdate = req.body
-
- return db.sequelize.transaction(t => {
- let tagsPromise: Promise<TagInstance[]>
- if (!videoInfosToUpdate.tags) {
- tagsPromise = Promise.resolve(null)
- } else {
- tagsPromise = db.Tag.findOrCreateTags(videoInfosToUpdate.tags, t)
- }
-
- return tagsPromise
- .then(tagInstances => {
- const options = {
- transaction: t
- }
-
- if (videoInfosToUpdate.name !== undefined) videoInstance.set('name', videoInfosToUpdate.name)
- if (videoInfosToUpdate.category !== undefined) videoInstance.set('category', videoInfosToUpdate.category)
- if (videoInfosToUpdate.licence !== undefined) videoInstance.set('licence', videoInfosToUpdate.licence)
- if (videoInfosToUpdate.language !== undefined) videoInstance.set('language', videoInfosToUpdate.language)
- if (videoInfosToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfosToUpdate.nsfw)
- if (videoInfosToUpdate.description !== undefined) videoInstance.set('description', videoInfosToUpdate.description)
-
- return videoInstance.save(options).then(() => tagInstances)
- })
- .then(tagInstances => {
- if (!tagInstances) return
-
- const options = { transaction: t }
- return videoInstance.setTags(tagInstances, options)
- .then(() => {
- videoInstance.Tags = tagInstances
-
- return
- })
- })
- .then(() => {
- const json = videoInstance.toUpdateRemoteJSON()
-
- // Now we'll update the video's meta data to our friends
- return updateVideoToFriends(json, t)
- })
- })
- .then(() => {
- logger.info('Video with name %s updated.', videoInstance.name)
- })
- .catch(err => {
- logger.debug('Cannot update the video.', err)
+ const videoInfoToUpdate: VideoUpdate = req.body
+ const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE
+
+ try {
+ await db.sequelize.transaction(async t => {
+ const sequelizeOptions = {
+ transaction: t
+ }
+
+ if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name)
+ if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category)
+ if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence)
+ if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language)
+ if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw)
+ if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', videoInfoToUpdate.privacy)
+ if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
+
+ const videoInstanceUpdated = await videoInstance.save(sequelizeOptions)
+
+ if (videoInfoToUpdate.tags) {
+ const tagInstances = await db.Tag.findOrCreateTags(videoInfoToUpdate.tags, t)
+
+ await videoInstance.setTags(tagInstances, sequelizeOptions)
+ videoInstance.Tags = tagInstances
+ }
+
+ // Now we'll update the video's meta data to our friends
+ if (wasPrivateVideo === false) {
+ await sendUpdateVideo(videoInstanceUpdated, t)
+ }
+
+ // Video is not private anymore, send a create action to remote servers
+ if (wasPrivateVideo === true && videoInstance.privacy !== VideoPrivacy.PRIVATE) {
+ await sendAddVideo(videoInstance, t)
+ await shareVideoByServer(videoInstance, t)
+ }
+ })