-async function updateVideo (req: express.Request, res: express.Response) {
- const videoInstance = res.locals.videoAll
- const videoFieldsSave = videoInstance.toJSON()
- const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON())
- const videoInfoToUpdate: VideoUpdate = req.body
-
- const wasConfidentialVideo = videoInstance.isConfidential()
- const hadPrivacyForFederation = videoInstance.hasPrivacyForFederation()
-
- // Process thumbnail or create it from the video
- const thumbnailModel = req.files && req.files['thumbnailfile']
- ? await createVideoMiniatureFromExisting(req.files['thumbnailfile'][0].path, videoInstance, ThumbnailType.MINIATURE, false)
- : undefined
-
- const previewModel = req.files && req.files['previewfile']
- ? await createVideoMiniatureFromExisting(req.files['previewfile'][0].path, videoInstance, ThumbnailType.PREVIEW, false)
- : undefined
-
- try {
- const videoInstanceUpdated = await sequelizeTypescript.transaction(async t => {
- const sequelizeOptions = { transaction: t }
- const oldVideoChannel = videoInstance.VideoChannel
-
- if (videoInfoToUpdate.name !== undefined) videoInstance.name = videoInfoToUpdate.name
- if (videoInfoToUpdate.category !== undefined) videoInstance.category = videoInfoToUpdate.category
- if (videoInfoToUpdate.licence !== undefined) videoInstance.licence = videoInfoToUpdate.licence
- if (videoInfoToUpdate.language !== undefined) videoInstance.language = videoInfoToUpdate.language
- if (videoInfoToUpdate.nsfw !== undefined) videoInstance.nsfw = videoInfoToUpdate.nsfw
- if (videoInfoToUpdate.waitTranscoding !== undefined) videoInstance.waitTranscoding = videoInfoToUpdate.waitTranscoding
- if (videoInfoToUpdate.support !== undefined) videoInstance.support = videoInfoToUpdate.support
- if (videoInfoToUpdate.description !== undefined) videoInstance.description = videoInfoToUpdate.description
- if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.commentsEnabled = videoInfoToUpdate.commentsEnabled
- if (videoInfoToUpdate.downloadEnabled !== undefined) videoInstance.downloadEnabled = videoInfoToUpdate.downloadEnabled
-
- if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) {
- videoInstance.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt)
- }
-
- let isNewVideo = false
- if (videoInfoToUpdate.privacy !== undefined) {
- isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy)
-
- const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10)
- videoInstance.setPrivacy(newPrivacy)
-
- // Unfederate the video if the new privacy is not compatible with federation
- if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) {
- await VideoModel.sendDelete(videoInstance, { transaction: t })
- }
- }
-
- const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) as MVideoFullLight
-
- if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t)
- if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t)
-
- // Video tags update?
- if (videoInfoToUpdate.tags !== undefined) {
- const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t)
-
- await videoInstanceUpdated.$set('Tags', tagInstances, sequelizeOptions)
- videoInstanceUpdated.Tags = tagInstances
- }
-
- // Video channel update?
- if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) {
- await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t })
- videoInstanceUpdated.VideoChannel = res.locals.videoChannel
-
- if (hadPrivacyForFederation === true) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
- }
-
- // Schedule an update in the future?
- if (videoInfoToUpdate.scheduleUpdate) {
- await ScheduleVideoUpdateModel.upsert({
- videoId: videoInstanceUpdated.id,
- updateAt: videoInfoToUpdate.scheduleUpdate.updateAt,
- privacy: videoInfoToUpdate.scheduleUpdate.privacy || null
- }, { transaction: t })
- } else if (videoInfoToUpdate.scheduleUpdate === null) {
- await ScheduleVideoUpdateModel.deleteByVideoId(videoInstanceUpdated.id, t)
- }
-
- await autoBlacklistVideoIfNeeded({
- video: videoInstanceUpdated,
- user: res.locals.oauth.token.User,
- isRemote: false,
- isNew: false,
- transaction: t
- })
-
- await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
-
- auditLogger.update(
- getAuditIdFromRes(res),
- new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
- oldVideoAuditView
- )
- logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
-
- return videoInstanceUpdated
- })
-
- if (wasConfidentialVideo) {
- Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated)
- }
-
- Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated })
- } catch (err) {
- // 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!
- resetSequelizeInstance(videoInstance, videoFieldsSave)
-
- throw err