]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/update.ts
07f9690ec07146da13d1812dc45a758dd254be7a
[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 { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
5 import { openapiOperationDoc } from '@server/middlewares/doc'
6 import { FilteredModelAttributes } from '@server/types'
7 import { MVideoFullLight } from '@server/types/models'
8 import { VideoUpdate } from '../../../../shared'
9 import { HttpStatusCode } 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 { CONFIG } from '../../../initializers/config'
15 import { MIMETYPES } from '../../../initializers/constants'
16 import { sequelizeTypescript } from '../../../initializers/database'
17 import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
18 import { Notifier } from '../../../lib/notifier'
19 import { Hooks } from '../../../lib/plugins/hooks'
20 import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
21 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares'
22 import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
23 import { VideoModel } from '../../../models/video/video'
24
25 const lTags = loggerTagsFactory('api', 'video')
26 const auditLogger = auditLoggerFactory('videos')
27 const updateRouter = express.Router()
28
29 const reqVideoFileUpdate = createReqFiles(
30 [ 'thumbnailfile', 'previewfile' ],
31 MIMETYPES.IMAGE.MIMETYPE_EXT,
32 {
33 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
34 previewfile: CONFIG.STORAGE.TMP_DIR
35 }
36 )
37
38 updateRouter.put('/:id',
39 openapiOperationDoc({ operationId: 'putVideo' }),
40 authenticate,
41 reqVideoFileUpdate,
42 asyncMiddleware(videosUpdateValidator),
43 asyncRetryTransactionMiddleware(updateVideo)
44 )
45
46 // ---------------------------------------------------------------------------
47
48 export {
49 updateRouter
50 }
51
52 // ---------------------------------------------------------------------------
53
54 export async function updateVideo (req: express.Request, res: express.Response) {
55 const videoInstance = res.locals.videoAll
56 const videoFieldsSave = videoInstance.toJSON()
57 const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON())
58 const videoInfoToUpdate: VideoUpdate = req.body
59
60 const wasConfidentialVideo = videoInstance.isConfidential()
61 const hadPrivacyForFederation = videoInstance.hasPrivacyForFederation()
62
63 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
64 video: videoInstance,
65 files: req.files,
66 fallback: () => Promise.resolve(undefined),
67 automaticallyGenerated: false
68 })
69
70 try {
71 const videoInstanceUpdated = await sequelizeTypescript.transaction(async t => {
72 const sequelizeOptions = { transaction: t }
73 const oldVideoChannel = videoInstance.VideoChannel
74
75 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [
76 'name',
77 'category',
78 'licence',
79 'language',
80 'nsfw',
81 'waitTranscoding',
82 'support',
83 'description',
84 'commentsEnabled',
85 'downloadEnabled'
86 ]
87
88 for (const key of keysToUpdate) {
89 if (videoInfoToUpdate[key] !== undefined) videoInstance.set(key, videoInfoToUpdate[key])
90 }
91
92 if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) {
93 videoInstance.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt)
94 }
95
96 // Privacy update?
97 let isNewVideo = false
98 if (videoInfoToUpdate.privacy !== undefined) {
99 isNewVideo = await updateVideoPrivacy({ videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction: t })
100 }
101
102 // Force updatedAt attribute change
103 if (!videoInstance.changed()) {
104 await videoInstance.setAsRefreshed()
105 }
106
107 const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) as MVideoFullLight
108
109 // Thumbnail & preview updates?
110 if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t)
111 if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t)
112
113 // Video tags update?
114 if (videoInfoToUpdate.tags !== undefined) {
115 await setVideoTags({ video: videoInstanceUpdated, tags: videoInfoToUpdate.tags, transaction: t })
116 }
117
118 // Video channel update?
119 if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) {
120 await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t })
121 videoInstanceUpdated.VideoChannel = res.locals.videoChannel
122
123 if (hadPrivacyForFederation === true) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
124 }
125
126 // Schedule an update in the future?
127 await updateSchedule(videoInstanceUpdated, videoInfoToUpdate, t)
128
129 await autoBlacklistVideoIfNeeded({
130 video: videoInstanceUpdated,
131 user: res.locals.oauth.token.User,
132 isRemote: false,
133 isNew: false,
134 transaction: t
135 })
136
137 await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
138
139 auditLogger.update(
140 getAuditIdFromRes(res),
141 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
142 oldVideoAuditView
143 )
144 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid, lTags(videoInstance.uuid))
145
146 return videoInstanceUpdated
147 })
148
149 if (wasConfidentialVideo) {
150 Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated)
151 }
152
153 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body })
154 } catch (err) {
155 // Force fields we want to update
156 // If the transaction is retried, sequelize will think the object has not changed
157 // So it will skip the SQL request, even if the last one was ROLLBACKed!
158 resetSequelizeInstance(videoInstance, videoFieldsSave)
159
160 throw err
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)
178 videoInstance.setPrivacy(newPrivacy)
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 }