1 import express from 'express'
2 import { Transaction } from 'sequelize/types'
3 import { changeVideoChannelShare } from '@server/lib/activitypub/share'
4 import { addVideoJobsAfterUpdate, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
5 import { setVideoPrivacy } from '@server/lib/video-privacy'
6 import { openapiOperationDoc } from '@server/middlewares/doc'
7 import { FilteredModelAttributes } from '@server/types'
8 import { MVideoFullLight } from '@server/types/models'
9 import { HttpStatusCode, VideoUpdate } from '@shared/models'
10 import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
11 import { resetSequelizeInstance } from '../../../helpers/database-utils'
12 import { createReqFiles } from '../../../helpers/express-utils'
13 import { logger, loggerTagsFactory } from '../../../helpers/logger'
14 import { MIMETYPES } from '../../../initializers/constants'
15 import { sequelizeTypescript } from '../../../initializers/database'
16 import { Hooks } from '../../../lib/plugins/hooks'
17 import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
18 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares'
19 import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
20 import { VideoModel } from '../../../models/video/video'
21 import { VideoPathManager } from '@server/lib/video-path-manager'
23 const lTags = loggerTagsFactory('api', 'video')
24 const auditLogger = auditLoggerFactory('videos')
25 const updateRouter = express.Router()
27 const reqVideoFileUpdate = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
29 updateRouter.put('/:id',
30 openapiOperationDoc({ operationId: 'putVideo' }),
33 asyncMiddleware(videosUpdateValidator),
34 asyncRetryTransactionMiddleware(updateVideo)
37 // ---------------------------------------------------------------------------
43 // ---------------------------------------------------------------------------
45 async function updateVideo (req: express.Request, res: express.Response) {
46 const videoFromReq = res.locals.videoAll
47 const videoFieldsSave = videoFromReq.toJSON()
48 const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON())
49 const videoInfoToUpdate: VideoUpdate = req.body
51 const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation()
52 const oldPrivacy = videoFromReq.privacy
54 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
57 fallback: () => Promise.resolve(undefined),
58 automaticallyGenerated: false
61 const videoFileLockReleaser = await VideoPathManager.Instance.lockFiles(videoFromReq.uuid)
64 const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => {
65 // Refresh video since thumbnails to prevent concurrent updates
66 const video = await VideoModel.loadFull(videoFromReq.id, t)
68 const oldVideoChannel = video.VideoChannel
70 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [
83 for (const key of keysToUpdate) {
84 if (videoInfoToUpdate[key] !== undefined) video.set(key, videoInfoToUpdate[key])
87 if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) {
88 video.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt)
92 let isNewVideo = false
93 if (videoInfoToUpdate.privacy !== undefined) {
94 isNewVideo = await updateVideoPrivacy({ videoInstance: video, videoInfoToUpdate, hadPrivacyForFederation, transaction: t })
97 // Force updatedAt attribute change
98 if (!video.changed()) {
99 await video.setAsRefreshed(t)
102 const videoInstanceUpdated = await video.save({ transaction: t }) as MVideoFullLight
104 // Thumbnail & preview updates?
105 if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t)
106 if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t)
108 // Video tags update?
109 if (videoInfoToUpdate.tags !== undefined) {
110 await setVideoTags({ video: videoInstanceUpdated, tags: videoInfoToUpdate.tags, transaction: t })
113 // Video channel update?
114 if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) {
115 await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t })
116 videoInstanceUpdated.VideoChannel = res.locals.videoChannel
118 if (hadPrivacyForFederation === true) {
119 await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
123 // Schedule an update in the future?
124 await updateSchedule(videoInstanceUpdated, videoInfoToUpdate, t)
126 await autoBlacklistVideoIfNeeded({
127 video: videoInstanceUpdated,
128 user: res.locals.oauth.token.User,
135 getAuditIdFromRes(res),
136 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
139 logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid))
141 return { videoInstanceUpdated, isNewVideo }
144 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res })
146 await addVideoJobsAfterUpdate({
147 video: videoInstanceUpdated,
148 nameChanged: !!videoInfoToUpdate.name,
153 // Force fields we want to update
154 // If the transaction is retried, sequelize will think the object has not changed
155 // So it will skip the SQL request, even if the last one was ROLLBACKed!
156 resetSequelizeInstance(videoFromReq, videoFieldsSave)
160 videoFileLockReleaser()
163 return res.type('json')
164 .status(HttpStatusCode.NO_CONTENT_204)
168 async function updateVideoPrivacy (options: {
169 videoInstance: MVideoFullLight
170 videoInfoToUpdate: VideoUpdate
171 hadPrivacyForFederation: boolean
172 transaction: Transaction
174 const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options
175 const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy)
177 const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10)
178 setVideoPrivacy(videoInstance, newPrivacy)
180 // Unfederate the video if the new privacy is not compatible with federation
181 if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) {
182 await VideoModel.sendDelete(videoInstance, { transaction })
188 function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: VideoUpdate, transaction: Transaction) {
189 if (videoInfoToUpdate.scheduleUpdate) {
190 return ScheduleVideoUpdateModel.upsert({
191 videoId: videoInstance.id,
192 updateAt: new Date(videoInfoToUpdate.scheduleUpdate.updateAt),
193 privacy: videoInfoToUpdate.scheduleUpdate.privacy || null
195 } else if (videoInfoToUpdate.scheduleUpdate === null) {
196 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)