1 import * as express from 'express'
2 import { Transaction } from 'sequelize/types'
3 import { changeVideoChannelShare } from '@server/lib/activitypub/share'
4 import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
5 import { FilteredModelAttributes } from '@server/types'
6 import { MVideoFullLight } from '@server/types/models'
7 import { VideoUpdate } from '../../../../shared'
8 import { HttpStatusCode } from '../../../../shared/core-utils/miscs'
9 import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
10 import { resetSequelizeInstance } from '../../../helpers/database-utils'
11 import { createReqFiles } from '../../../helpers/express-utils'
12 import { logger, loggerTagsFactory } from '../../../helpers/logger'
13 import { CONFIG } from '../../../initializers/config'
14 import { MIMETYPES } from '../../../initializers/constants'
15 import { sequelizeTypescript } from '../../../initializers/database'
16 import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
17 import { Notifier } from '../../../lib/notifier'
18 import { Hooks } from '../../../lib/plugins/hooks'
19 import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
20 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares'
21 import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
22 import { VideoModel } from '../../../models/video/video'
24 const lTags = loggerTagsFactory('api', 'video')
25 const auditLogger = auditLoggerFactory('videos')
26 const updateRouter = express.Router()
28 const reqVideoFileUpdate = createReqFiles(
29 [ 'thumbnailfile', 'previewfile' ],
30 MIMETYPES.IMAGE.MIMETYPE_EXT,
32 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
33 previewfile: CONFIG.STORAGE.TMP_DIR
37 updateRouter.put('/:id',
40 asyncMiddleware(videosUpdateValidator),
41 asyncRetryTransactionMiddleware(updateVideo)
44 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
52 export async function updateVideo (req: express.Request, res: express.Response) {
53 const videoInstance = res.locals.videoAll
54 const videoFieldsSave = videoInstance.toJSON()
55 const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON())
56 const videoInfoToUpdate: VideoUpdate = req.body
58 const wasConfidentialVideo = videoInstance.isConfidential()
59 const hadPrivacyForFederation = videoInstance.hasPrivacyForFederation()
61 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
64 fallback: () => Promise.resolve(undefined),
65 automaticallyGenerated: false
69 const videoInstanceUpdated = await sequelizeTypescript.transaction(async t => {
70 const sequelizeOptions = { transaction: t }
71 const oldVideoChannel = videoInstance.VideoChannel
73 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [
86 for (const key of keysToUpdate) {
87 if (videoInfoToUpdate[key] !== undefined) videoInstance.set(key, videoInfoToUpdate[key])
90 if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) {
91 videoInstance.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt)
95 let isNewVideo = false
96 if (videoInfoToUpdate.privacy !== undefined) {
97 isNewVideo = await updateVideoPrivacy({ videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction: t })
100 const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) as MVideoFullLight
102 // Thumbnail & preview updates?
103 if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t)
104 if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t)
106 // Video tags update?
107 if (videoInfoToUpdate.tags !== undefined) {
108 await setVideoTags({ video: videoInstanceUpdated, tags: videoInfoToUpdate.tags, transaction: t })
111 // Video channel update?
112 if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) {
113 await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t })
114 videoInstanceUpdated.VideoChannel = res.locals.videoChannel
116 if (hadPrivacyForFederation === true) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
119 // Schedule an update in the future?
120 await updateSchedule(videoInstanceUpdated, videoInfoToUpdate, t)
122 await autoBlacklistVideoIfNeeded({
123 video: videoInstanceUpdated,
124 user: res.locals.oauth.token.User,
130 await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
133 getAuditIdFromRes(res),
134 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
137 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid, lTags(videoInstance.uuid))
139 return videoInstanceUpdated
142 if (wasConfidentialVideo) {
143 Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated)
146 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body })
148 // Force fields we want to update
149 // If the transaction is retried, sequelize will think the object has not changed
150 // So it will skip the SQL request, even if the last one was ROLLBACKed!
151 resetSequelizeInstance(videoInstance, videoFieldsSave)
156 return res.type('json')
157 .status(HttpStatusCode.NO_CONTENT_204)
161 async function updateVideoPrivacy (options: {
162 videoInstance: MVideoFullLight
163 videoInfoToUpdate: VideoUpdate
164 hadPrivacyForFederation: boolean
165 transaction: Transaction
167 const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options
168 const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy)
170 const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10)
171 videoInstance.setPrivacy(newPrivacy)
173 // Unfederate the video if the new privacy is not compatible with federation
174 if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) {
175 await VideoModel.sendDelete(videoInstance, { transaction })
181 function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: VideoUpdate, transaction: Transaction) {
182 if (videoInfoToUpdate.scheduleUpdate) {
183 return ScheduleVideoUpdateModel.upsert({
184 videoId: videoInstance.id,
185 updateAt: new Date(videoInfoToUpdate.scheduleUpdate.updateAt),
186 privacy: videoInfoToUpdate.scheduleUpdate.privacy || null
188 } else if (videoInfoToUpdate.scheduleUpdate === null) {
189 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)