aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/update.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-05-27 16:12:41 +0200
committerChocobozzz <me@florianbigard.com>2021-05-27 16:12:41 +0200
commit8f608a4cb22ab232cfab20665050764b38bac9c7 (patch)
tree6a6785aae79bf5939ad7b7a50a1bd8031268d2b4 /server/controllers/api/videos/update.ts
parent030ccfce59a8cb8f2fee6ea8dd363ba635c5c5c2 (diff)
parentc215e627b575d2c4085ccb222f4ca8d0237b7552 (diff)
downloadPeerTube-8f608a4cb22ab232cfab20665050764b38bac9c7.tar.gz
PeerTube-8f608a4cb22ab232cfab20665050764b38bac9c7.tar.zst
PeerTube-8f608a4cb22ab232cfab20665050764b38bac9c7.zip
Merge branch 'develop' into shorter-URLs-channels-accounts
Diffstat (limited to 'server/controllers/api/videos/update.ts')
-rw-r--r--server/controllers/api/videos/update.ts191
1 files changed, 191 insertions, 0 deletions
diff --git a/server/controllers/api/videos/update.ts b/server/controllers/api/videos/update.ts
new file mode 100644
index 000000000..2450abd0e
--- /dev/null
+++ b/server/controllers/api/videos/update.ts
@@ -0,0 +1,191 @@
1import * as express from 'express'
2import { Transaction } from 'sequelize/types'
3import { changeVideoChannelShare } from '@server/lib/activitypub/share'
4import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
5import { FilteredModelAttributes } from '@server/types'
6import { MVideoFullLight } from '@server/types/models'
7import { VideoUpdate } from '../../../../shared'
8import { HttpStatusCode } from '../../../../shared/core-utils/miscs'
9import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
10import { resetSequelizeInstance } from '../../../helpers/database-utils'
11import { createReqFiles } from '../../../helpers/express-utils'
12import { logger, loggerTagsFactory } from '../../../helpers/logger'
13import { CONFIG } from '../../../initializers/config'
14import { MIMETYPES } from '../../../initializers/constants'
15import { sequelizeTypescript } from '../../../initializers/database'
16import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
17import { Notifier } from '../../../lib/notifier'
18import { Hooks } from '../../../lib/plugins/hooks'
19import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
20import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares'
21import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
22import { VideoModel } from '../../../models/video/video'
23
24const lTags = loggerTagsFactory('api', 'video')
25const auditLogger = auditLoggerFactory('videos')
26const updateRouter = express.Router()
27
28const reqVideoFileUpdate = createReqFiles(
29 [ 'thumbnailfile', 'previewfile' ],
30 MIMETYPES.IMAGE.MIMETYPE_EXT,
31 {
32 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
33 previewfile: CONFIG.STORAGE.TMP_DIR
34 }
35)
36
37updateRouter.put('/:id',
38 authenticate,
39 reqVideoFileUpdate,
40 asyncMiddleware(videosUpdateValidator),
41 asyncRetryTransactionMiddleware(updateVideo)
42)
43
44// ---------------------------------------------------------------------------
45
46export {
47 updateRouter
48}
49
50// ---------------------------------------------------------------------------
51
52export async function updateVideo (req: express.Request, res: express.Response) {
53 const videoInstance = res.locals.videoAll
54 const videoFieldsSave = videoInstance.toJSON()
55 const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON())
56 const videoInfoToUpdate: VideoUpdate = req.body
57
58 const wasConfidentialVideo = videoInstance.isConfidential()
59 const hadPrivacyForFederation = videoInstance.hasPrivacyForFederation()
60
61 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
62 video: videoInstance,
63 files: req.files,
64 fallback: () => Promise.resolve(undefined),
65 automaticallyGenerated: false
66 })
67
68 try {
69 const videoInstanceUpdated = await sequelizeTypescript.transaction(async t => {
70 const sequelizeOptions = { transaction: t }
71 const oldVideoChannel = videoInstance.VideoChannel
72
73 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [
74 'name',
75 'category',
76 'licence',
77 'language',
78 'nsfw',
79 'waitTranscoding',
80 'support',
81 'description',
82 'commentsEnabled',
83 'downloadEnabled'
84 ]
85
86 for (const key of keysToUpdate) {
87 if (videoInfoToUpdate[key] !== undefined) videoInstance.set(key, videoInfoToUpdate[key])
88 }
89
90 if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) {
91 videoInstance.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt)
92 }
93
94 // Privacy update?
95 let isNewVideo = false
96 if (videoInfoToUpdate.privacy !== undefined) {
97 isNewVideo = await updateVideoPrivacy({ videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction: t })
98 }
99
100 const videoInstanceUpdated = await videoInstance.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 await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
131
132 auditLogger.update(
133 getAuditIdFromRes(res),
134 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
135 oldVideoAuditView
136 )
137 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid, lTags(videoInstance.uuid))
138
139 return videoInstanceUpdated
140 })
141
142 if (wasConfidentialVideo) {
143 Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated)
144 }
145
146 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body })
147 } catch (err) {
148 // Force fields we want to update
149 // If the transaction is retried, sequelize will think the object has not changed
150 // So it will skip the SQL request, even if the last one was ROLLBACKed!
151 resetSequelizeInstance(videoInstance, videoFieldsSave)
152
153 throw err
154 }
155
156 return res.type('json')
157 .status(HttpStatusCode.NO_CONTENT_204)
158 .end()
159}
160
161async function updateVideoPrivacy (options: {
162 videoInstance: MVideoFullLight
163 videoInfoToUpdate: VideoUpdate
164 hadPrivacyForFederation: boolean
165 transaction: Transaction
166}) {
167 const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options
168 const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy)
169
170 const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10)
171 videoInstance.setPrivacy(newPrivacy)
172
173 // Unfederate the video if the new privacy is not compatible with federation
174 if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) {
175 await VideoModel.sendDelete(videoInstance, { transaction })
176 }
177
178 return isNewVideo
179}
180
181function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: VideoUpdate, transaction: Transaction) {
182 if (videoInfoToUpdate.scheduleUpdate) {
183 return ScheduleVideoUpdateModel.upsert({
184 videoId: videoInstance.id,
185 updateAt: new Date(videoInfoToUpdate.scheduleUpdate.updateAt),
186 privacy: videoInfoToUpdate.scheduleUpdate.privacy || null
187 }, { transaction })
188 } else if (videoInfoToUpdate.scheduleUpdate === null) {
189 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)
190 }
191}