]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { Transaction } from 'sequelize/types' | |
3 | import { changeVideoChannelShare } from '@server/lib/activitypub/share' | |
4 | import { CreateJobArgument, JobQueue } from '@server/lib/job-queue' | |
5 | import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' | |
6 | import { openapiOperationDoc } from '@server/middlewares/doc' | |
7 | import { FilteredModelAttributes } from '@server/types' | |
8 | import { MVideoFullLight } from '@server/types/models' | |
9 | import { HttpStatusCode, ManageVideoTorrentPayload, 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 | ||
22 | const lTags = loggerTagsFactory('api', 'video') | |
23 | const auditLogger = auditLoggerFactory('videos') | |
24 | const updateRouter = express.Router() | |
25 | ||
26 | const reqVideoFileUpdate = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) | |
27 | ||
28 | updateRouter.put('/:id', | |
29 | openapiOperationDoc({ operationId: 'putVideo' }), | |
30 | authenticate, | |
31 | reqVideoFileUpdate, | |
32 | asyncMiddleware(videosUpdateValidator), | |
33 | asyncRetryTransactionMiddleware(updateVideo) | |
34 | ) | |
35 | ||
36 | // --------------------------------------------------------------------------- | |
37 | ||
38 | export { | |
39 | updateRouter | |
40 | } | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
44 | async function updateVideo (req: express.Request, res: express.Response) { | |
45 | const videoFromReq = res.locals.videoAll | |
46 | const videoFieldsSave = videoFromReq.toJSON() | |
47 | const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON()) | |
48 | const videoInfoToUpdate: VideoUpdate = req.body | |
49 | ||
50 | const wasConfidentialVideo = videoFromReq.isConfidential() | |
51 | const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation() | |
52 | ||
53 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ | |
54 | video: videoFromReq, | |
55 | files: req.files, | |
56 | fallback: () => Promise.resolve(undefined), | |
57 | automaticallyGenerated: false | |
58 | }) | |
59 | ||
60 | try { | |
61 | const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => { | |
62 | // Refresh video since thumbnails to prevent concurrent updates | |
63 | const video = await VideoModel.loadFull(videoFromReq.id, t) | |
64 | ||
65 | const sequelizeOptions = { transaction: t } | |
66 | const oldVideoChannel = video.VideoChannel | |
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) { | |
82 | if (videoInfoToUpdate[key] !== undefined) video.set(key, videoInfoToUpdate[key]) | |
83 | } | |
84 | ||
85 | if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) { | |
86 | video.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt) | |
87 | } | |
88 | ||
89 | // Privacy update? | |
90 | let isNewVideo = false | |
91 | if (videoInfoToUpdate.privacy !== undefined) { | |
92 | isNewVideo = await updateVideoPrivacy({ videoInstance: video, videoInfoToUpdate, hadPrivacyForFederation, transaction: t }) | |
93 | } | |
94 | ||
95 | // Force updatedAt attribute change | |
96 | if (!video.changed()) { | |
97 | await video.setAsRefreshed(t) | |
98 | } | |
99 | ||
100 | const videoInstanceUpdated = await video.save(sequelizeOptions) as MVideoFullLight | |
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 | ||
130 | auditLogger.update( | |
131 | getAuditIdFromRes(res), | |
132 | new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()), | |
133 | oldVideoAuditView | |
134 | ) | |
135 | logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid)) | |
136 | ||
137 | return { videoInstanceUpdated, isNewVideo } | |
138 | }) | |
139 | ||
140 | Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) | |
141 | ||
142 | await addVideoJobsAfterUpdate({ video: videoInstanceUpdated, videoInfoToUpdate, wasConfidentialVideo, isNewVideo }) | |
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! | |
147 | resetSequelizeInstance(videoFromReq, videoFieldsSave) | |
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 | } | |
188 | ||
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) { | |
199 | ||
200 | for (const file of (video.VideoFiles || [])) { | |
201 | const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id } | |
202 | ||
203 | jobs.push({ type: 'manage-video-torrent', payload }) | |
204 | } | |
205 | ||
206 | const hls = video.getHLSPlaylist() | |
207 | ||
208 | for (const file of (hls?.VideoFiles || [])) { | |
209 | const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id } | |
210 | ||
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 | }) | |
231 | } | |
232 | ||
233 | return JobQueue.Instance.createSequentialJobFlow(...jobs) | |
234 | } |