]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/update.ts
Add uk-UA locale support
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / update.ts
CommitLineData
41fb13c3 1import express from 'express'
c158a5fa
C
2import { Transaction } from 'sequelize/types'
3import { changeVideoChannelShare } from '@server/lib/activitypub/share'
3545e72c
C
4import { addVideoJobsAfterUpdate, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
5import { setVideoPrivacy } from '@server/lib/video-privacy'
c0e8b12e 6import { openapiOperationDoc } from '@server/middlewares/doc'
c158a5fa
C
7import { FilteredModelAttributes } from '@server/types'
8import { MVideoFullLight } from '@server/types/models'
3545e72c 9import { HttpStatusCode, VideoUpdate } from '@shared/models'
c158a5fa
C
10import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
11import { resetSequelizeInstance } from '../../../helpers/database-utils'
12import { createReqFiles } from '../../../helpers/express-utils'
13import { logger, loggerTagsFactory } from '../../../helpers/logger'
c158a5fa
C
14import { MIMETYPES } from '../../../initializers/constants'
15import { sequelizeTypescript } from '../../../initializers/database'
c158a5fa
C
16import { Hooks } from '../../../lib/plugins/hooks'
17import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
18import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares'
19import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
20import { VideoModel } from '../../../models/video/video'
3545e72c 21import { VideoPathManager } from '@server/lib/video-path-manager'
4638cd71 22import { forceNumber } from '@shared/core-utils'
c158a5fa
C
23
24const lTags = loggerTagsFactory('api', 'video')
25const auditLogger = auditLoggerFactory('videos')
26const updateRouter = express.Router()
27
d3d3deaa 28const reqVideoFileUpdate = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
c158a5fa
C
29
30updateRouter.put('/:id',
1c627fd8 31 openapiOperationDoc({ operationId: 'putVideo' }),
c158a5fa
C
32 authenticate,
33 reqVideoFileUpdate,
34 asyncMiddleware(videosUpdateValidator),
35 asyncRetryTransactionMiddleware(updateVideo)
36)
37
38// ---------------------------------------------------------------------------
39
40export {
41 updateRouter
42}
43
44// ---------------------------------------------------------------------------
45
b46cf4b9 46async function updateVideo (req: express.Request, res: express.Response) {
a2a81f5a
C
47 const videoFromReq = res.locals.videoAll
48 const videoFieldsSave = videoFromReq.toJSON()
49 const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON())
c158a5fa
C
50 const videoInfoToUpdate: VideoUpdate = req.body
51
a2a81f5a 52 const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation()
3545e72c 53 const oldPrivacy = videoFromReq.privacy
c158a5fa
C
54
55 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
a2a81f5a 56 video: videoFromReq,
c158a5fa
C
57 files: req.files,
58 fallback: () => Promise.resolve(undefined),
59 automaticallyGenerated: false
60 })
61
3545e72c
C
62 const videoFileLockReleaser = await VideoPathManager.Instance.lockFiles(videoFromReq.uuid)
63
c158a5fa 64 try {
38d69d65 65 const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => {
a2a81f5a 66 // Refresh video since thumbnails to prevent concurrent updates
4fae2b1f 67 const video = await VideoModel.loadFull(videoFromReq.id, t)
a2a81f5a 68
a2a81f5a 69 const oldVideoChannel = video.VideoChannel
c158a5fa
C
70
71 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [
72 'name',
73 'category',
74 'licence',
75 'language',
76 'nsfw',
77 'waitTranscoding',
78 'support',
79 'description',
80 'commentsEnabled',
81 'downloadEnabled'
82 ]
83
84 for (const key of keysToUpdate) {
a2a81f5a 85 if (videoInfoToUpdate[key] !== undefined) video.set(key, videoInfoToUpdate[key])
c158a5fa
C
86 }
87
88 if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) {
a2a81f5a 89 video.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt)
c158a5fa
C
90 }
91
92 // Privacy update?
93 let isNewVideo = false
94 if (videoInfoToUpdate.privacy !== undefined) {
a2a81f5a 95 isNewVideo = await updateVideoPrivacy({ videoInstance: video, videoInfoToUpdate, hadPrivacyForFederation, transaction: t })
c158a5fa
C
96 }
97
5cf027bd 98 // Force updatedAt attribute change
a2a81f5a 99 if (!video.changed()) {
597f771f 100 await video.setAsRefreshed(t)
5cf027bd
C
101 }
102
3545e72c 103 const videoInstanceUpdated = await video.save({ transaction: t }) as MVideoFullLight
c158a5fa
C
104
105 // Thumbnail & preview updates?
106 if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t)
107 if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t)
108
109 // Video tags update?
110 if (videoInfoToUpdate.tags !== undefined) {
111 await setVideoTags({ video: videoInstanceUpdated, tags: videoInfoToUpdate.tags, transaction: t })
112 }
113
114 // Video channel update?
115 if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) {
116 await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t })
117 videoInstanceUpdated.VideoChannel = res.locals.videoChannel
118
3545e72c
C
119 if (hadPrivacyForFederation === true) {
120 await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
121 }
c158a5fa
C
122 }
123
124 // Schedule an update in the future?
125 await updateSchedule(videoInstanceUpdated, videoInfoToUpdate, t)
126
127 await autoBlacklistVideoIfNeeded({
128 video: videoInstanceUpdated,
129 user: res.locals.oauth.token.User,
130 isRemote: false,
131 isNew: false,
132 transaction: t
133 })
134
c158a5fa
C
135 auditLogger.update(
136 getAuditIdFromRes(res),
137 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
138 oldVideoAuditView
139 )
a2a81f5a 140 logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid))
c158a5fa 141
38d69d65 142 return { videoInstanceUpdated, isNewVideo }
c158a5fa
C
143 })
144
bd911b54 145 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res })
38d69d65 146
3545e72c
C
147 await addVideoJobsAfterUpdate({
148 video: videoInstanceUpdated,
149 nameChanged: !!videoInfoToUpdate.name,
150 oldPrivacy,
151 isNewVideo
152 })
c158a5fa
C
153 } catch (err) {
154 // Force fields we want to update
155 // If the transaction is retried, sequelize will think the object has not changed
156 // So it will skip the SQL request, even if the last one was ROLLBACKed!
a2a81f5a 157 resetSequelizeInstance(videoFromReq, videoFieldsSave)
c158a5fa
C
158
159 throw err
3545e72c
C
160 } finally {
161 videoFileLockReleaser()
c158a5fa
C
162 }
163
164 return res.type('json')
165 .status(HttpStatusCode.NO_CONTENT_204)
166 .end()
167}
168
169async function updateVideoPrivacy (options: {
170 videoInstance: MVideoFullLight
171 videoInfoToUpdate: VideoUpdate
172 hadPrivacyForFederation: boolean
173 transaction: Transaction
174}) {
175 const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options
176 const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy)
177
4638cd71 178 const newPrivacy = forceNumber(videoInfoToUpdate.privacy)
3545e72c 179 setVideoPrivacy(videoInstance, newPrivacy)
c158a5fa
C
180
181 // Unfederate the video if the new privacy is not compatible with federation
182 if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) {
183 await VideoModel.sendDelete(videoInstance, { transaction })
184 }
185
186 return isNewVideo
187}
188
189function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: VideoUpdate, transaction: Transaction) {
190 if (videoInfoToUpdate.scheduleUpdate) {
191 return ScheduleVideoUpdateModel.upsert({
192 videoId: videoInstance.id,
193 updateAt: new Date(videoInfoToUpdate.scheduleUpdate.updateAt),
194 privacy: videoInfoToUpdate.scheduleUpdate.privacy || null
195 }, { transaction })
196 } else if (videoInfoToUpdate.scheduleUpdate === null) {
197 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)
198 }
199}