]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/update.ts
Fix retrying update on sql serialization conflict
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / update.ts
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'
22 import { forceNumber } from '@shared/core-utils'
23
24 const lTags = loggerTagsFactory('api', 'video')
25 const auditLogger = auditLoggerFactory('videos')
26 const updateRouter = express.Router()
27
28 const reqVideoFileUpdate = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
29
30 updateRouter.put('/:id',
31 openapiOperationDoc({ operationId: 'putVideo' }),
32 authenticate,
33 reqVideoFileUpdate,
34 asyncMiddleware(videosUpdateValidator),
35 asyncRetryTransactionMiddleware(updateVideo)
36 )
37
38 // ---------------------------------------------------------------------------
39
40 export {
41 updateRouter
42 }
43
44 // ---------------------------------------------------------------------------
45
46 async function updateVideo (req: express.Request, res: express.Response) {
47 const videoFromReq = res.locals.videoAll
48 const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON())
49 const videoInfoToUpdate: VideoUpdate = req.body
50
51 const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation()
52 const oldPrivacy = videoFromReq.privacy
53
54 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
55 video: videoFromReq,
56 files: req.files,
57 fallback: () => Promise.resolve(undefined),
58 automaticallyGenerated: false
59 })
60
61 const videoFileLockReleaser = await VideoPathManager.Instance.lockFiles(videoFromReq.uuid)
62
63 try {
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)
67
68 const oldVideoChannel = video.VideoChannel
69
70 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [
71 'name',
72 'category',
73 'licence',
74 'language',
75 'nsfw',
76 'waitTranscoding',
77 'support',
78 'description',
79 'commentsEnabled',
80 'downloadEnabled'
81 ]
82
83 for (const key of keysToUpdate) {
84 if (videoInfoToUpdate[key] !== undefined) video.set(key, videoInfoToUpdate[key])
85 }
86
87 if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) {
88 video.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt)
89 }
90
91 // Privacy update?
92 let isNewVideo = false
93 if (videoInfoToUpdate.privacy !== undefined) {
94 isNewVideo = await updateVideoPrivacy({ videoInstance: video, videoInfoToUpdate, hadPrivacyForFederation, transaction: t })
95 }
96
97 // Force updatedAt attribute change
98 if (!video.changed()) {
99 await video.setAsRefreshed(t)
100 }
101
102 const videoInstanceUpdated = await video.save({ transaction: t }) as MVideoFullLight
103
104 // Thumbnail & preview updates?
105 if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t)
106 if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t)
107
108 // Video tags update?
109 if (videoInfoToUpdate.tags !== undefined) {
110 await setVideoTags({ video: videoInstanceUpdated, tags: videoInfoToUpdate.tags, transaction: t })
111 }
112
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
117
118 if (hadPrivacyForFederation === true) {
119 await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
120 }
121 }
122
123 // Schedule an update in the future?
124 await updateSchedule(videoInstanceUpdated, videoInfoToUpdate, t)
125
126 await autoBlacklistVideoIfNeeded({
127 video: videoInstanceUpdated,
128 user: res.locals.oauth.token.User,
129 isRemote: false,
130 isNew: false,
131 transaction: t
132 })
133
134 auditLogger.update(
135 getAuditIdFromRes(res),
136 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
137 oldVideoAuditView
138 )
139 logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid))
140
141 return { videoInstanceUpdated, isNewVideo }
142 })
143
144 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res })
145
146 await addVideoJobsAfterUpdate({
147 video: videoInstanceUpdated,
148 nameChanged: !!videoInfoToUpdate.name,
149 oldPrivacy,
150 isNewVideo
151 })
152 } catch (err) {
153 // If the transaction is retried, sequelize will think the object has not changed
154 // So we need to restore the previous fields
155 resetSequelizeInstance(videoFromReq)
156
157 throw err
158 } finally {
159 videoFileLockReleaser()
160 }
161
162 return res.type('json')
163 .status(HttpStatusCode.NO_CONTENT_204)
164 .end()
165 }
166
167 async function updateVideoPrivacy (options: {
168 videoInstance: MVideoFullLight
169 videoInfoToUpdate: VideoUpdate
170 hadPrivacyForFederation: boolean
171 transaction: Transaction
172 }) {
173 const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options
174 const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy)
175
176 const newPrivacy = forceNumber(videoInfoToUpdate.privacy)
177 setVideoPrivacy(videoInstance, newPrivacy)
178
179 // Unfederate the video if the new privacy is not compatible with federation
180 if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) {
181 await VideoModel.sendDelete(videoInstance, { transaction })
182 }
183
184 return isNewVideo
185 }
186
187 function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: VideoUpdate, transaction: Transaction) {
188 if (videoInfoToUpdate.scheduleUpdate) {
189 return ScheduleVideoUpdateModel.upsert({
190 videoId: videoInstance.id,
191 updateAt: new Date(videoInfoToUpdate.scheduleUpdate.updateAt),
192 privacy: videoInfoToUpdate.scheduleUpdate.privacy || null
193 }, { transaction })
194 } else if (videoInfoToUpdate.scheduleUpdate === null) {
195 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)
196 }
197 }