]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
c158a5fa C |
2 | import { Transaction } from 'sequelize/types' |
3 | import { changeVideoChannelShare } from '@server/lib/activitypub/share' | |
bd911b54 | 4 | import { CreateJobArgument, JobQueue } from '@server/lib/job-queue' |
c158a5fa | 5 | import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' |
c0e8b12e | 6 | import { openapiOperationDoc } from '@server/middlewares/doc' |
c158a5fa C |
7 | import { FilteredModelAttributes } from '@server/types' |
8 | import { MVideoFullLight } from '@server/types/models' | |
f012319a | 9 | import { HttpStatusCode, ManageVideoTorrentPayload, VideoUpdate } from '@shared/models' |
c158a5fa C |
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' | |
c158a5fa C |
14 | import { MIMETYPES } from '../../../initializers/constants' |
15 | import { sequelizeTypescript } from '../../../initializers/database' | |
c158a5fa C |
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 | ||
22 | const lTags = loggerTagsFactory('api', 'video') | |
23 | const auditLogger = auditLoggerFactory('videos') | |
24 | const updateRouter = express.Router() | |
25 | ||
d3d3deaa | 26 | const reqVideoFileUpdate = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) |
c158a5fa C |
27 | |
28 | updateRouter.put('/:id', | |
1c627fd8 | 29 | openapiOperationDoc({ operationId: 'putVideo' }), |
c158a5fa C |
30 | authenticate, |
31 | reqVideoFileUpdate, | |
32 | asyncMiddleware(videosUpdateValidator), | |
33 | asyncRetryTransactionMiddleware(updateVideo) | |
34 | ) | |
35 | ||
36 | // --------------------------------------------------------------------------- | |
37 | ||
38 | export { | |
39 | updateRouter | |
40 | } | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
b46cf4b9 | 44 | async function updateVideo (req: express.Request, res: express.Response) { |
a2a81f5a C |
45 | const videoFromReq = res.locals.videoAll |
46 | const videoFieldsSave = videoFromReq.toJSON() | |
47 | const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON()) | |
c158a5fa C |
48 | const videoInfoToUpdate: VideoUpdate = req.body |
49 | ||
a2a81f5a C |
50 | const wasConfidentialVideo = videoFromReq.isConfidential() |
51 | const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation() | |
c158a5fa C |
52 | |
53 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ | |
a2a81f5a | 54 | video: videoFromReq, |
c158a5fa C |
55 | files: req.files, |
56 | fallback: () => Promise.resolve(undefined), | |
57 | automaticallyGenerated: false | |
58 | }) | |
59 | ||
60 | try { | |
38d69d65 | 61 | const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => { |
a2a81f5a | 62 | // Refresh video since thumbnails to prevent concurrent updates |
4fae2b1f | 63 | const video = await VideoModel.loadFull(videoFromReq.id, t) |
a2a81f5a | 64 | |
c158a5fa | 65 | const sequelizeOptions = { transaction: t } |
a2a81f5a | 66 | const oldVideoChannel = video.VideoChannel |
c158a5fa C |
67 | |
68 | const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [ | |
69 | 'name', | |
70 | 'category', | |
71 | 'licence', | |
72 | 'language', | |
73 | 'nsfw', | |
74 | 'waitTranscoding', | |
75 | 'support', | |
76 | 'description', | |
77 | 'commentsEnabled', | |
78 | 'downloadEnabled' | |
79 | ] | |
80 | ||
81 | for (const key of keysToUpdate) { | |
a2a81f5a | 82 | if (videoInfoToUpdate[key] !== undefined) video.set(key, videoInfoToUpdate[key]) |
c158a5fa C |
83 | } |
84 | ||
85 | if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) { | |
a2a81f5a | 86 | video.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt) |
c158a5fa C |
87 | } |
88 | ||
89 | // Privacy update? | |
90 | let isNewVideo = false | |
91 | if (videoInfoToUpdate.privacy !== undefined) { | |
a2a81f5a | 92 | isNewVideo = await updateVideoPrivacy({ videoInstance: video, videoInfoToUpdate, hadPrivacyForFederation, transaction: t }) |
c158a5fa C |
93 | } |
94 | ||
5cf027bd | 95 | // Force updatedAt attribute change |
a2a81f5a | 96 | if (!video.changed()) { |
597f771f | 97 | await video.setAsRefreshed(t) |
5cf027bd C |
98 | } |
99 | ||
a2a81f5a | 100 | const videoInstanceUpdated = await video.save(sequelizeOptions) as MVideoFullLight |
c158a5fa C |
101 | |
102 | // Thumbnail & preview updates? | |
103 | if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t) | |
104 | if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t) | |
105 | ||
106 | // Video tags update? | |
107 | if (videoInfoToUpdate.tags !== undefined) { | |
108 | await setVideoTags({ video: videoInstanceUpdated, tags: videoInfoToUpdate.tags, transaction: t }) | |
109 | } | |
110 | ||
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 | |
115 | ||
116 | if (hadPrivacyForFederation === true) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t) | |
117 | } | |
118 | ||
119 | // Schedule an update in the future? | |
120 | await updateSchedule(videoInstanceUpdated, videoInfoToUpdate, t) | |
121 | ||
122 | await autoBlacklistVideoIfNeeded({ | |
123 | video: videoInstanceUpdated, | |
124 | user: res.locals.oauth.token.User, | |
125 | isRemote: false, | |
126 | isNew: false, | |
127 | transaction: t | |
128 | }) | |
129 | ||
c158a5fa C |
130 | auditLogger.update( |
131 | getAuditIdFromRes(res), | |
132 | new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()), | |
133 | oldVideoAuditView | |
134 | ) | |
a2a81f5a | 135 | logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid)) |
c158a5fa | 136 | |
38d69d65 | 137 | return { videoInstanceUpdated, isNewVideo } |
c158a5fa C |
138 | }) |
139 | ||
bd911b54 | 140 | Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) |
38d69d65 | 141 | |
bd911b54 | 142 | await addVideoJobsAfterUpdate({ video: videoInstanceUpdated, videoInfoToUpdate, wasConfidentialVideo, isNewVideo }) |
c158a5fa C |
143 | } catch (err) { |
144 | // Force fields we want to update | |
145 | // If the transaction is retried, sequelize will think the object has not changed | |
146 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | |
a2a81f5a | 147 | resetSequelizeInstance(videoFromReq, videoFieldsSave) |
c158a5fa C |
148 | |
149 | throw err | |
150 | } | |
151 | ||
152 | return res.type('json') | |
153 | .status(HttpStatusCode.NO_CONTENT_204) | |
154 | .end() | |
155 | } | |
156 | ||
157 | async function updateVideoPrivacy (options: { | |
158 | videoInstance: MVideoFullLight | |
159 | videoInfoToUpdate: VideoUpdate | |
160 | hadPrivacyForFederation: boolean | |
161 | transaction: Transaction | |
162 | }) { | |
163 | const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options | |
164 | const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy) | |
165 | ||
166 | const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10) | |
167 | videoInstance.setPrivacy(newPrivacy) | |
168 | ||
169 | // Unfederate the video if the new privacy is not compatible with federation | |
170 | if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) { | |
171 | await VideoModel.sendDelete(videoInstance, { transaction }) | |
172 | } | |
173 | ||
174 | return isNewVideo | |
175 | } | |
176 | ||
177 | function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: VideoUpdate, transaction: Transaction) { | |
178 | if (videoInfoToUpdate.scheduleUpdate) { | |
179 | return ScheduleVideoUpdateModel.upsert({ | |
180 | videoId: videoInstance.id, | |
181 | updateAt: new Date(videoInfoToUpdate.scheduleUpdate.updateAt), | |
182 | privacy: videoInfoToUpdate.scheduleUpdate.privacy || null | |
183 | }, { transaction }) | |
184 | } else if (videoInfoToUpdate.scheduleUpdate === null) { | |
185 | return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction) | |
186 | } | |
187 | } | |
9b293cd6 | 188 | |
bd911b54 C |
189 | async function addVideoJobsAfterUpdate (options: { |
190 | video: MVideoFullLight | |
191 | videoInfoToUpdate: VideoUpdate | |
192 | wasConfidentialVideo: boolean | |
193 | isNewVideo: boolean | |
194 | }) { | |
195 | const { video, videoInfoToUpdate, wasConfidentialVideo, isNewVideo } = options | |
196 | const jobs: CreateJobArgument[] = [] | |
197 | ||
198 | if (!video.isLive && videoInfoToUpdate.name) { | |
f012319a | 199 | |
bd911b54 C |
200 | for (const file of (video.VideoFiles || [])) { |
201 | const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id } | |
38d69d65 | 202 | |
bd911b54 C |
203 | jobs.push({ type: 'manage-video-torrent', payload }) |
204 | } | |
6ba93fa6 | 205 | |
bd911b54 | 206 | const hls = video.getHLSPlaylist() |
6ba93fa6 | 207 | |
bd911b54 C |
208 | for (const file of (hls?.VideoFiles || [])) { |
209 | const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id } | |
6ba93fa6 | 210 | |
bd911b54 C |
211 | jobs.push({ type: 'manage-video-torrent', payload }) |
212 | } | |
213 | } | |
214 | ||
215 | jobs.push({ | |
216 | type: 'federate-video', | |
217 | payload: { | |
218 | videoUUID: video.uuid, | |
219 | isNewVideo | |
220 | } | |
221 | }) | |
222 | ||
223 | if (wasConfidentialVideo) { | |
224 | jobs.push({ | |
225 | type: 'notify', | |
226 | payload: { | |
227 | action: 'new-video', | |
228 | videoUUID: video.uuid | |
229 | } | |
230 | }) | |
6ba93fa6 | 231 | } |
f012319a | 232 | |
bd911b54 | 233 | return JobQueue.Instance.createSequentialJobFlow(...jobs) |
9b293cd6 | 234 | } |