]>
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' | |
3545e72c C |
4 | import { addVideoJobsAfterUpdate, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' |
5 | import { setVideoPrivacy } from '@server/lib/video-privacy' | |
c0e8b12e | 6 | import { openapiOperationDoc } from '@server/middlewares/doc' |
c158a5fa C |
7 | import { FilteredModelAttributes } from '@server/types' |
8 | import { MVideoFullLight } from '@server/types/models' | |
3545e72c | 9 | import { HttpStatusCode, 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' | |
3545e72c | 21 | import { VideoPathManager } from '@server/lib/video-path-manager' |
c158a5fa C |
22 | |
23 | const lTags = loggerTagsFactory('api', 'video') | |
24 | const auditLogger = auditLoggerFactory('videos') | |
25 | const updateRouter = express.Router() | |
26 | ||
d3d3deaa | 27 | const reqVideoFileUpdate = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) |
c158a5fa C |
28 | |
29 | updateRouter.put('/:id', | |
1c627fd8 | 30 | openapiOperationDoc({ operationId: 'putVideo' }), |
c158a5fa C |
31 | authenticate, |
32 | reqVideoFileUpdate, | |
33 | asyncMiddleware(videosUpdateValidator), | |
34 | asyncRetryTransactionMiddleware(updateVideo) | |
35 | ) | |
36 | ||
37 | // --------------------------------------------------------------------------- | |
38 | ||
39 | export { | |
40 | updateRouter | |
41 | } | |
42 | ||
43 | // --------------------------------------------------------------------------- | |
44 | ||
b46cf4b9 | 45 | async function updateVideo (req: express.Request, res: express.Response) { |
a2a81f5a C |
46 | const videoFromReq = res.locals.videoAll |
47 | const videoFieldsSave = videoFromReq.toJSON() | |
48 | const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON()) | |
c158a5fa C |
49 | const videoInfoToUpdate: VideoUpdate = req.body |
50 | ||
a2a81f5a | 51 | const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation() |
3545e72c | 52 | const oldPrivacy = videoFromReq.privacy |
c158a5fa C |
53 | |
54 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ | |
a2a81f5a | 55 | video: videoFromReq, |
c158a5fa C |
56 | files: req.files, |
57 | fallback: () => Promise.resolve(undefined), | |
58 | automaticallyGenerated: false | |
59 | }) | |
60 | ||
3545e72c C |
61 | const videoFileLockReleaser = await VideoPathManager.Instance.lockFiles(videoFromReq.uuid) |
62 | ||
c158a5fa | 63 | try { |
38d69d65 | 64 | const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => { |
a2a81f5a | 65 | // Refresh video since thumbnails to prevent concurrent updates |
4fae2b1f | 66 | const video = await VideoModel.loadFull(videoFromReq.id, t) |
a2a81f5a | 67 | |
a2a81f5a | 68 | const oldVideoChannel = video.VideoChannel |
c158a5fa C |
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) { | |
a2a81f5a | 84 | if (videoInfoToUpdate[key] !== undefined) video.set(key, videoInfoToUpdate[key]) |
c158a5fa C |
85 | } |
86 | ||
87 | if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) { | |
a2a81f5a | 88 | video.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt) |
c158a5fa C |
89 | } |
90 | ||
91 | // Privacy update? | |
92 | let isNewVideo = false | |
93 | if (videoInfoToUpdate.privacy !== undefined) { | |
a2a81f5a | 94 | isNewVideo = await updateVideoPrivacy({ videoInstance: video, videoInfoToUpdate, hadPrivacyForFederation, transaction: t }) |
c158a5fa C |
95 | } |
96 | ||
5cf027bd | 97 | // Force updatedAt attribute change |
a2a81f5a | 98 | if (!video.changed()) { |
597f771f | 99 | await video.setAsRefreshed(t) |
5cf027bd C |
100 | } |
101 | ||
3545e72c | 102 | const videoInstanceUpdated = await video.save({ transaction: t }) as MVideoFullLight |
c158a5fa C |
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 | ||
3545e72c C |
118 | if (hadPrivacyForFederation === true) { |
119 | await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t) | |
120 | } | |
c158a5fa C |
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 | ||
c158a5fa C |
134 | auditLogger.update( |
135 | getAuditIdFromRes(res), | |
136 | new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()), | |
137 | oldVideoAuditView | |
138 | ) | |
a2a81f5a | 139 | logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid)) |
c158a5fa | 140 | |
38d69d65 | 141 | return { videoInstanceUpdated, isNewVideo } |
c158a5fa C |
142 | }) |
143 | ||
bd911b54 | 144 | Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) |
38d69d65 | 145 | |
3545e72c C |
146 | await addVideoJobsAfterUpdate({ |
147 | video: videoInstanceUpdated, | |
148 | nameChanged: !!videoInfoToUpdate.name, | |
149 | oldPrivacy, | |
150 | isNewVideo | |
151 | }) | |
c158a5fa C |
152 | } catch (err) { |
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! | |
a2a81f5a | 156 | resetSequelizeInstance(videoFromReq, videoFieldsSave) |
c158a5fa C |
157 | |
158 | throw err | |
3545e72c C |
159 | } finally { |
160 | videoFileLockReleaser() | |
c158a5fa C |
161 | } |
162 | ||
163 | return res.type('json') | |
164 | .status(HttpStatusCode.NO_CONTENT_204) | |
165 | .end() | |
166 | } | |
167 | ||
168 | async function updateVideoPrivacy (options: { | |
169 | videoInstance: MVideoFullLight | |
170 | videoInfoToUpdate: VideoUpdate | |
171 | hadPrivacyForFederation: boolean | |
172 | transaction: Transaction | |
173 | }) { | |
174 | const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options | |
175 | const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy) | |
176 | ||
177 | const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10) | |
3545e72c | 178 | setVideoPrivacy(videoInstance, newPrivacy) |
c158a5fa C |
179 | |
180 | // Unfederate the video if the new privacy is not compatible with federation | |
181 | if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) { | |
182 | await VideoModel.sendDelete(videoInstance, { transaction }) | |
183 | } | |
184 | ||
185 | return isNewVideo | |
186 | } | |
187 | ||
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 | |
194 | }, { transaction }) | |
195 | } else if (videoInfoToUpdate.scheduleUpdate === null) { | |
196 | return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction) | |
197 | } | |
198 | } |