]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
93e1258c | 2 | import { extname, join } from 'path' |
b345a804 | 3 | import { VideoCreate, VideoPrivacy, VideoState, VideoUpdate } from '../../../../shared' |
3a6f351b | 4 | import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils' |
da854ddd | 5 | import { logger } from '../../../helpers/logger' |
993cef4b | 6 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' |
06215f15 | 7 | import { getFormattedObjects, getServerActor } from '../../../helpers/utils' |
7ccddd7b | 8 | import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist' |
b345a804 C |
9 | import { |
10 | DEFAULT_AUDIO_RESOLUTION, | |
11 | MIMETYPES, | |
12 | VIDEO_CATEGORIES, | |
13 | VIDEO_LANGUAGES, | |
14 | VIDEO_LICENCES, | |
15 | VIDEO_PRIVACIES | |
16 | } from '../../../initializers/constants' | |
0f320037 C |
17 | import { |
18 | changeVideoChannelShare, | |
2186386c | 19 | federateVideoIfNeeded, |
0f320037 | 20 | fetchRemoteVideoDescription, |
2186386c | 21 | getVideoActivityPubUrl |
0f320037 | 22 | } from '../../../lib/activitypub' |
94a5ff8a | 23 | import { JobQueue } from '../../../lib/job-queue' |
b5c0e955 | 24 | import { Redis } from '../../../lib/redis' |
65fcc311 | 25 | import { |
ac81d1a0 | 26 | asyncMiddleware, |
90d4bb81 | 27 | asyncRetryTransactionMiddleware, |
ac81d1a0 | 28 | authenticate, |
8d427346 | 29 | checkVideoFollowConstraints, |
d525fc39 | 30 | commonVideosFiltersValidator, |
0883b324 | 31 | optionalAuthenticate, |
ac81d1a0 C |
32 | paginationValidator, |
33 | setDefaultPagination, | |
34 | setDefaultSort, | |
35 | videosAddValidator, | |
09209296 | 36 | videosCustomGetValidator, |
ac81d1a0 C |
37 | videosGetValidator, |
38 | videosRemoveValidator, | |
ac81d1a0 C |
39 | videosSortValidator, |
40 | videosUpdateValidator | |
65fcc311 | 41 | } from '../../../middlewares' |
3fd3ab2d C |
42 | import { TagModel } from '../../../models/video/tag' |
43 | import { VideoModel } from '../../../models/video/video' | |
44 | import { VideoFileModel } from '../../../models/video/video-file' | |
65fcc311 C |
45 | import { abuseVideoRouter } from './abuse' |
46 | import { blacklistRouter } from './blacklist' | |
bf1f6508 | 47 | import { videoCommentRouter } from './comment' |
571389d4 | 48 | import { rateVideoRouter } from './rate' |
74d63469 | 49 | import { ownershipVideoRouter } from './ownership' |
0883b324 | 50 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' |
06215f15 | 51 | import { buildNSFWFilter, createReqFiles } from '../../../helpers/express-utils' |
2baea0c7 | 52 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' |
40e87e9e | 53 | import { videoCaptionsRouter } from './captions' |
fbad87b0 | 54 | import { videoImportsRouter } from './import' |
06215f15 | 55 | import { resetSequelizeInstance } from '../../../helpers/database-utils' |
14e2014a | 56 | import { move } from 'fs-extra' |
6e46de09 | 57 | import { watchingRouter } from './watching' |
cef534ed | 58 | import { Notifier } from '../../../lib/notifier' |
1e7eb25f | 59 | import { sendView } from '../../../lib/activitypub/send/send-view' |
6dd9de95 | 60 | import { CONFIG } from '../../../initializers/config' |
74dc3bca | 61 | import { sequelizeTypescript } from '../../../initializers/database' |
3acc5084 | 62 | import { createVideoMiniatureFromExisting, generateVideoMiniature } from '../../../lib/thumbnail' |
e8bafea3 | 63 | import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' |
536598cf | 64 | import { VideoTranscodingPayload } from '../../../lib/job-queue/handlers/video-transcoding' |
65fcc311 | 65 | |
80e36cd9 | 66 | const auditLogger = auditLoggerFactory('videos') |
65fcc311 | 67 | const videosRouter = express.Router() |
9f10b292 | 68 | |
ac81d1a0 C |
69 | const reqVideoFileAdd = createReqFiles( |
70 | [ 'videofile', 'thumbnailfile', 'previewfile' ], | |
14e2014a | 71 | Object.assign({}, MIMETYPES.VIDEO.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT), |
ac81d1a0 | 72 | { |
6040f87d C |
73 | videofile: CONFIG.STORAGE.TMP_DIR, |
74 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, | |
75 | previewfile: CONFIG.STORAGE.TMP_DIR | |
ac81d1a0 C |
76 | } |
77 | ) | |
78 | const reqVideoFileUpdate = createReqFiles( | |
79 | [ 'thumbnailfile', 'previewfile' ], | |
14e2014a | 80 | MIMETYPES.IMAGE.MIMETYPE_EXT, |
ac81d1a0 | 81 | { |
6040f87d C |
82 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, |
83 | previewfile: CONFIG.STORAGE.TMP_DIR | |
ac81d1a0 C |
84 | } |
85 | ) | |
8c308c2b | 86 | |
65fcc311 C |
87 | videosRouter.use('/', abuseVideoRouter) |
88 | videosRouter.use('/', blacklistRouter) | |
89 | videosRouter.use('/', rateVideoRouter) | |
bf1f6508 | 90 | videosRouter.use('/', videoCommentRouter) |
40e87e9e | 91 | videosRouter.use('/', videoCaptionsRouter) |
fbad87b0 | 92 | videosRouter.use('/', videoImportsRouter) |
74d63469 | 93 | videosRouter.use('/', ownershipVideoRouter) |
6e46de09 | 94 | videosRouter.use('/', watchingRouter) |
d33242b0 | 95 | |
65fcc311 C |
96 | videosRouter.get('/categories', listVideoCategories) |
97 | videosRouter.get('/licences', listVideoLicences) | |
98 | videosRouter.get('/languages', listVideoLanguages) | |
fd45e8f4 | 99 | videosRouter.get('/privacies', listVideoPrivacies) |
6e07c3de | 100 | |
65fcc311 C |
101 | videosRouter.get('/', |
102 | paginationValidator, | |
103 | videosSortValidator, | |
1174a847 | 104 | setDefaultSort, |
f05a1c30 | 105 | setDefaultPagination, |
0883b324 | 106 | optionalAuthenticate, |
d525fc39 | 107 | commonVideosFiltersValidator, |
eb080476 | 108 | asyncMiddleware(listVideos) |
fbf1134e | 109 | ) |
65fcc311 C |
110 | videosRouter.put('/:id', |
111 | authenticate, | |
ac81d1a0 | 112 | reqVideoFileUpdate, |
a2431b7d | 113 | asyncMiddleware(videosUpdateValidator), |
90d4bb81 | 114 | asyncRetryTransactionMiddleware(updateVideo) |
7b1f49de | 115 | ) |
e95561cd | 116 | videosRouter.post('/upload', |
65fcc311 | 117 | authenticate, |
ac81d1a0 | 118 | reqVideoFileAdd, |
3fd3ab2d | 119 | asyncMiddleware(videosAddValidator), |
90d4bb81 | 120 | asyncRetryTransactionMiddleware(addVideo) |
fbf1134e | 121 | ) |
9567011b C |
122 | |
123 | videosRouter.get('/:id/description', | |
a2431b7d | 124 | asyncMiddleware(videosGetValidator), |
9567011b C |
125 | asyncMiddleware(getVideoDescription) |
126 | ) | |
65fcc311 | 127 | videosRouter.get('/:id', |
6e46de09 | 128 | optionalAuthenticate, |
09209296 | 129 | asyncMiddleware(videosCustomGetValidator('only-video-with-rights')), |
8d427346 | 130 | asyncMiddleware(checkVideoFollowConstraints), |
09209296 | 131 | asyncMiddleware(getVideo) |
fbf1134e | 132 | ) |
1f3e9fec C |
133 | videosRouter.post('/:id/views', |
134 | asyncMiddleware(videosGetValidator), | |
135 | asyncMiddleware(viewVideo) | |
136 | ) | |
198b205c | 137 | |
65fcc311 C |
138 | videosRouter.delete('/:id', |
139 | authenticate, | |
a2431b7d | 140 | asyncMiddleware(videosRemoveValidator), |
90d4bb81 | 141 | asyncRetryTransactionMiddleware(removeVideo) |
fbf1134e | 142 | ) |
198b205c | 143 | |
9f10b292 | 144 | // --------------------------------------------------------------------------- |
c45f7f84 | 145 | |
65fcc311 C |
146 | export { |
147 | videosRouter | |
148 | } | |
c45f7f84 | 149 | |
9f10b292 | 150 | // --------------------------------------------------------------------------- |
c45f7f84 | 151 | |
556ddc31 | 152 | function listVideoCategories (req: express.Request, res: express.Response) { |
65fcc311 | 153 | res.json(VIDEO_CATEGORIES) |
6e07c3de C |
154 | } |
155 | ||
556ddc31 | 156 | function listVideoLicences (req: express.Request, res: express.Response) { |
65fcc311 | 157 | res.json(VIDEO_LICENCES) |
6f0c39e2 C |
158 | } |
159 | ||
556ddc31 | 160 | function listVideoLanguages (req: express.Request, res: express.Response) { |
65fcc311 | 161 | res.json(VIDEO_LANGUAGES) |
3092476e C |
162 | } |
163 | ||
fd45e8f4 C |
164 | function listVideoPrivacies (req: express.Request, res: express.Response) { |
165 | res.json(VIDEO_PRIVACIES) | |
166 | } | |
167 | ||
90d4bb81 | 168 | async function addVideo (req: express.Request, res: express.Response) { |
8b917537 C |
169 | // Processing the video could be long |
170 | // Set timeout to 10 minutes | |
171 | req.setTimeout(1000 * 60 * 10, () => { | |
172 | logger.error('Upload video has timed out.') | |
173 | return res.sendStatus(408) | |
174 | }) | |
175 | ||
90d4bb81 | 176 | const videoPhysicalFile = req.files['videofile'][0] |
556ddc31 | 177 | const videoInfo: VideoCreate = req.body |
9f10b292 | 178 | |
e11f68a3 C |
179 | // Prepare data so we don't block the transaction |
180 | const videoData = { | |
181 | name: videoInfo.name, | |
182 | remote: false, | |
e11f68a3 C |
183 | category: videoInfo.category, |
184 | licence: videoInfo.licence, | |
185 | language: videoInfo.language, | |
2186386c | 186 | commentsEnabled: videoInfo.commentsEnabled || false, |
7f2cfe3a | 187 | downloadEnabled: videoInfo.downloadEnabled || true, |
2186386c C |
188 | waitTranscoding: videoInfo.waitTranscoding || false, |
189 | state: CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED, | |
190 | nsfw: videoInfo.nsfw || false, | |
e11f68a3 | 191 | description: videoInfo.description, |
2422c46b | 192 | support: videoInfo.support, |
2b4dd7e2 | 193 | privacy: videoInfo.privacy || VideoPrivacy.PRIVATE, |
e11f68a3 | 194 | duration: videoPhysicalFile['duration'], // duration was added by a previous middleware |
1e74f19a | 195 | channelId: res.locals.videoChannel.id, |
196 | originallyPublishedAt: videoInfo.originallyPublishedAt | |
e11f68a3 | 197 | } |
7ccddd7b | 198 | |
3fd3ab2d | 199 | const video = new VideoModel(videoData) |
2186386c | 200 | video.url = getVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object |
eb080476 | 201 | |
46a6db24 | 202 | const videoFile = new VideoFileModel({ |
e11f68a3 | 203 | extname: extname(videoPhysicalFile.filename), |
536598cf | 204 | size: videoPhysicalFile.size |
46a6db24 | 205 | }) |
2186386c | 206 | |
ad3405d0 C |
207 | if (videoFile.isAudio()) { |
208 | videoFile.resolution = DEFAULT_AUDIO_RESOLUTION | |
209 | } else { | |
536598cf C |
210 | videoFile.fps = await getVideoFileFPS(videoPhysicalFile.path) |
211 | videoFile.resolution = (await getVideoFileResolution(videoPhysicalFile.path)).videoFileResolution | |
536598cf C |
212 | } |
213 | ||
2186386c | 214 | // Move physical file |
e11f68a3 | 215 | const videoDir = CONFIG.STORAGE.VIDEOS_DIR |
e11f68a3 | 216 | const destination = join(videoDir, video.getVideoFilename(videoFile)) |
14e2014a | 217 | await move(videoPhysicalFile.path, destination) |
e3a682a8 C |
218 | // This is important in case if there is another attempt in the retry process |
219 | videoPhysicalFile.filename = video.getVideoFilename(videoFile) | |
82815eb6 | 220 | videoPhysicalFile.path = destination |
ac81d1a0 C |
221 | |
222 | // Process thumbnail or create it from the video | |
223 | const thumbnailField = req.files['thumbnailfile'] | |
e8bafea3 | 224 | const thumbnailModel = thumbnailField |
3acc5084 C |
225 | ? await createVideoMiniatureFromExisting(thumbnailField[0].path, video, ThumbnailType.MINIATURE) |
226 | : await generateVideoMiniature(video, videoFile, ThumbnailType.MINIATURE) | |
93e1258c | 227 | |
ac81d1a0 C |
228 | // Process preview or create it from the video |
229 | const previewField = req.files['previewfile'] | |
e8bafea3 | 230 | const previewModel = previewField |
3acc5084 C |
231 | ? await createVideoMiniatureFromExisting(previewField[0].path, video, ThumbnailType.PREVIEW) |
232 | : await generateVideoMiniature(video, videoFile, ThumbnailType.PREVIEW) | |
eb080476 | 233 | |
2186386c | 234 | // Create the torrent file |
ac81d1a0 | 235 | await video.createTorrentAndSetInfoHash(videoFile) |
eb080476 | 236 | |
7ccddd7b | 237 | const { videoCreated, videoWasAutoBlacklisted } = await sequelizeTypescript.transaction(async t => { |
e11f68a3 | 238 | const sequelizeOptions = { transaction: t } |
eb080476 | 239 | |
eb080476 | 240 | const videoCreated = await video.save(sequelizeOptions) |
e8bafea3 | 241 | |
3acc5084 C |
242 | await videoCreated.addAndSaveThumbnail(thumbnailModel, t) |
243 | await videoCreated.addAndSaveThumbnail(previewModel, t) | |
e8bafea3 | 244 | |
eb080476 C |
245 | // Do not forget to add video channel information to the created video |
246 | videoCreated.VideoChannel = res.locals.videoChannel | |
7920c273 | 247 | |
eb080476 | 248 | videoFile.videoId = video.id |
eb080476 | 249 | await videoFile.save(sequelizeOptions) |
e11f68a3 C |
250 | |
251 | video.VideoFiles = [ videoFile ] | |
93e1258c | 252 | |
2baea0c7 | 253 | // Create tags |
2efd32f6 | 254 | if (videoInfo.tags !== undefined) { |
3fd3ab2d | 255 | const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t) |
eb080476 | 256 | |
3fd3ab2d | 257 | await video.$set('Tags', tagInstances, sequelizeOptions) |
eb080476 C |
258 | video.Tags = tagInstances |
259 | } | |
260 | ||
2baea0c7 C |
261 | // Schedule an update in the future? |
262 | if (videoInfo.scheduleUpdate) { | |
263 | await ScheduleVideoUpdateModel.create({ | |
264 | videoId: video.id, | |
265 | updateAt: videoInfo.scheduleUpdate.updateAt, | |
266 | privacy: videoInfo.scheduleUpdate.privacy || null | |
267 | }, { transaction: t }) | |
268 | } | |
269 | ||
7ccddd7b JM |
270 | const videoWasAutoBlacklisted = await autoBlacklistVideoIfNeeded(video, res.locals.oauth.token.User, t) |
271 | ||
272 | if (!videoWasAutoBlacklisted) { | |
273 | await federateVideoIfNeeded(video, true, t) | |
274 | } | |
eb080476 | 275 | |
993cef4b | 276 | auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON())) |
cadb46d8 C |
277 | logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid) |
278 | ||
7ccddd7b | 279 | return { videoCreated, videoWasAutoBlacklisted } |
cadb46d8 | 280 | }) |
94a5ff8a | 281 | |
7ccddd7b JM |
282 | if (videoWasAutoBlacklisted) { |
283 | Notifier.Instance.notifyOnVideoAutoBlacklist(videoCreated) | |
284 | } else { | |
285 | Notifier.Instance.notifyOnNewVideo(videoCreated) | |
286 | } | |
e8d246d5 | 287 | |
2186386c | 288 | if (video.state === VideoState.TO_TRANSCODE) { |
94a5ff8a | 289 | // Put uuid because we don't have id auto incremented for now |
536598cf C |
290 | let dataInput: VideoTranscodingPayload |
291 | ||
292 | if (videoFile.isAudio()) { | |
293 | dataInput = { | |
294 | type: 'merge-audio' as 'merge-audio', | |
295 | resolution: DEFAULT_AUDIO_RESOLUTION, | |
296 | videoUUID: videoCreated.uuid, | |
297 | isNewVideo: true | |
298 | } | |
299 | } else { | |
300 | dataInput = { | |
301 | type: 'optimize' as 'optimize', | |
302 | videoUUID: videoCreated.uuid, | |
303 | isNewVideo: true | |
304 | } | |
94a5ff8a C |
305 | } |
306 | ||
a0327eed | 307 | await JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput }) |
94a5ff8a C |
308 | } |
309 | ||
90d4bb81 C |
310 | return res.json({ |
311 | video: { | |
312 | id: videoCreated.id, | |
313 | uuid: videoCreated.uuid | |
314 | } | |
315 | }).end() | |
ed04d94f C |
316 | } |
317 | ||
eb080476 | 318 | async function updateVideo (req: express.Request, res: express.Response) { |
dae86118 | 319 | const videoInstance = res.locals.video |
7f4e7c36 | 320 | const videoFieldsSave = videoInstance.toJSON() |
80e36cd9 | 321 | const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON()) |
556ddc31 | 322 | const videoInfoToUpdate: VideoUpdate = req.body |
46a6db24 | 323 | |
fd45e8f4 | 324 | const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE |
46a6db24 | 325 | const wasNotPrivateVideo = videoInstance.privacy !== VideoPrivacy.PRIVATE |
cef534ed | 326 | const wasUnlistedVideo = videoInstance.privacy === VideoPrivacy.UNLISTED |
7b1f49de | 327 | |
ac81d1a0 | 328 | // Process thumbnail or create it from the video |
e8bafea3 | 329 | const thumbnailModel = req.files && req.files['thumbnailfile'] |
3acc5084 | 330 | ? await createVideoMiniatureFromExisting(req.files['thumbnailfile'][0].path, videoInstance, ThumbnailType.MINIATURE) |
e8bafea3 | 331 | : undefined |
ac81d1a0 | 332 | |
e8bafea3 | 333 | const previewModel = req.files && req.files['previewfile'] |
3acc5084 | 334 | ? await createVideoMiniatureFromExisting(req.files['previewfile'][0].path, videoInstance, ThumbnailType.PREVIEW) |
e8bafea3 | 335 | : undefined |
ac81d1a0 | 336 | |
eb080476 | 337 | try { |
e8d246d5 C |
338 | const videoInstanceUpdated = await sequelizeTypescript.transaction(async t => { |
339 | const sequelizeOptions = { transaction: t } | |
0f320037 | 340 | const oldVideoChannel = videoInstance.VideoChannel |
7b1f49de | 341 | |
eb080476 C |
342 | if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name) |
343 | if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category) | |
344 | if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence) | |
345 | if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language) | |
346 | if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw) | |
2186386c | 347 | if (videoInfoToUpdate.waitTranscoding !== undefined) videoInstance.set('waitTranscoding', videoInfoToUpdate.waitTranscoding) |
2422c46b | 348 | if (videoInfoToUpdate.support !== undefined) videoInstance.set('support', videoInfoToUpdate.support) |
eb080476 | 349 | if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description) |
47564bbe | 350 | if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.set('commentsEnabled', videoInfoToUpdate.commentsEnabled) |
7f2cfe3a | 351 | if (videoInfoToUpdate.downloadEnabled !== undefined) videoInstance.set('downloadEnabled', videoInfoToUpdate.downloadEnabled) |
b718fd22 C |
352 | |
353 | if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) { | |
1735c825 | 354 | videoInstance.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt) |
1e74f19a | 355 | } |
356 | ||
2922e048 JLB |
357 | if (videoInfoToUpdate.privacy !== undefined) { |
358 | const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10) | |
1735c825 | 359 | videoInstance.privacy = newPrivacy |
2922e048 | 360 | |
46a6db24 | 361 | // The video was private, and is not anymore -> publish it |
2922e048 | 362 | if (wasPrivateVideo === true && newPrivacy !== VideoPrivacy.PRIVATE) { |
1735c825 | 363 | videoInstance.publishedAt = new Date() |
2922e048 | 364 | } |
46a6db24 C |
365 | |
366 | // The video was not private, but now it is -> we need to unfederate it | |
367 | if (wasNotPrivateVideo === true && newPrivacy === VideoPrivacy.PRIVATE) { | |
368 | await VideoModel.sendDelete(videoInstance, { transaction: t }) | |
369 | } | |
2922e048 | 370 | } |
7b1f49de | 371 | |
54141398 | 372 | const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) |
7b1f49de | 373 | |
3acc5084 C |
374 | if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t) |
375 | if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t) | |
e8bafea3 | 376 | |
0f320037 | 377 | // Video tags update? |
2efd32f6 | 378 | if (videoInfoToUpdate.tags !== undefined) { |
3fd3ab2d | 379 | const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t) |
7b1f49de | 380 | |
0f320037 C |
381 | await videoInstanceUpdated.$set('Tags', tagInstances, sequelizeOptions) |
382 | videoInstanceUpdated.Tags = tagInstances | |
eb080476 | 383 | } |
7920c273 | 384 | |
0f320037 C |
385 | // Video channel update? |
386 | if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) { | |
6200d8d9 | 387 | await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t }) |
2186386c | 388 | videoInstanceUpdated.VideoChannel = res.locals.videoChannel |
0f320037 C |
389 | |
390 | if (wasPrivateVideo === false) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t) | |
fd45e8f4 C |
391 | } |
392 | ||
2baea0c7 C |
393 | // Schedule an update in the future? |
394 | if (videoInfoToUpdate.scheduleUpdate) { | |
395 | await ScheduleVideoUpdateModel.upsert({ | |
396 | videoId: videoInstanceUpdated.id, | |
397 | updateAt: videoInfoToUpdate.scheduleUpdate.updateAt, | |
398 | privacy: videoInfoToUpdate.scheduleUpdate.privacy || null | |
399 | }, { transaction: t }) | |
e94fc297 C |
400 | } else if (videoInfoToUpdate.scheduleUpdate === null) { |
401 | await ScheduleVideoUpdateModel.deleteByVideoId(videoInstanceUpdated.id, t) | |
2baea0c7 C |
402 | } |
403 | ||
2186386c | 404 | const isNewVideo = wasPrivateVideo && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE |
5abb9fbb C |
405 | |
406 | // Don't send update if the video was unfederated | |
407 | if (!videoInstanceUpdated.VideoBlacklist || videoInstanceUpdated.VideoBlacklist.unfederated === false) { | |
408 | await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t) | |
409 | } | |
6fcd19ba | 410 | |
80e36cd9 | 411 | auditLogger.update( |
993cef4b | 412 | getAuditIdFromRes(res), |
80e36cd9 AB |
413 | new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()), |
414 | oldVideoAuditView | |
415 | ) | |
416 | logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid) | |
e8d246d5 C |
417 | |
418 | return videoInstanceUpdated | |
80e36cd9 | 419 | }) |
e8d246d5 C |
420 | |
421 | if (wasUnlistedVideo || wasPrivateVideo) { | |
422 | Notifier.Instance.notifyOnNewVideo(videoInstanceUpdated) | |
423 | } | |
eb080476 | 424 | } catch (err) { |
6fcd19ba C |
425 | // Force fields we want to update |
426 | // If the transaction is retried, sequelize will think the object has not changed | |
427 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | |
eb080476 | 428 | resetSequelizeInstance(videoInstance, videoFieldsSave) |
6fcd19ba C |
429 | |
430 | throw err | |
eb080476 | 431 | } |
90d4bb81 C |
432 | |
433 | return res.type('json').status(204).end() | |
9f10b292 | 434 | } |
8c308c2b | 435 | |
09209296 C |
436 | async function getVideo (req: express.Request, res: express.Response) { |
437 | // We need more attributes | |
438 | const userId: number = res.locals.oauth ? res.locals.oauth.token.User.id : null | |
dae86118 | 439 | const video = await VideoModel.loadForGetAPI(res.locals.video.id, undefined, userId) |
1f3e9fec | 440 | |
09209296 C |
441 | if (video.isOutdated()) { |
442 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } }) | |
443 | .catch(err => logger.error('Cannot create AP refresher job for video %s.', video.url, { err })) | |
04b8c3fb C |
444 | } |
445 | ||
09209296 | 446 | return res.json(video.toFormattedDetailsJSON()) |
1f3e9fec C |
447 | } |
448 | ||
449 | async function viewVideo (req: express.Request, res: express.Response) { | |
818f7987 | 450 | const videoInstance = res.locals.video |
9e167724 | 451 | |
490b595a | 452 | const ip = req.ip |
0f6acda1 | 453 | const exists = await Redis.Instance.doesVideoIPViewExist(ip, videoInstance.uuid) |
b5c0e955 C |
454 | if (exists) { |
455 | logger.debug('View for ip %s and video %s already exists.', ip, videoInstance.uuid) | |
456 | return res.status(204).end() | |
457 | } | |
458 | ||
6b616860 C |
459 | await Promise.all([ |
460 | Redis.Instance.addVideoView(videoInstance.id), | |
461 | Redis.Instance.setIPVideoView(ip, videoInstance.uuid) | |
462 | ]) | |
b5c0e955 | 463 | |
a2377d15 | 464 | const serverActor = await getServerActor() |
1e7eb25f | 465 | await sendView(serverActor, videoInstance, undefined) |
9e167724 | 466 | |
1f3e9fec | 467 | return res.status(204).end() |
9f10b292 | 468 | } |
8c308c2b | 469 | |
9567011b C |
470 | async function getVideoDescription (req: express.Request, res: express.Response) { |
471 | const videoInstance = res.locals.video | |
472 | let description = '' | |
473 | ||
474 | if (videoInstance.isOwned()) { | |
475 | description = videoInstance.description | |
476 | } else { | |
571389d4 | 477 | description = await fetchRemoteVideoDescription(videoInstance) |
9567011b C |
478 | } |
479 | ||
480 | return res.json({ description }) | |
481 | } | |
482 | ||
04b8c3fb | 483 | async function listVideos (req: express.Request, res: express.Response) { |
48dce1c9 C |
484 | const resultList = await VideoModel.listForApi({ |
485 | start: req.query.start, | |
486 | count: req.query.count, | |
487 | sort: req.query.sort, | |
06a05d5f | 488 | includeLocalVideos: true, |
d525fc39 C |
489 | categoryOneOf: req.query.categoryOneOf, |
490 | licenceOneOf: req.query.licenceOneOf, | |
491 | languageOneOf: req.query.languageOneOf, | |
492 | tagsOneOf: req.query.tagsOneOf, | |
493 | tagsAllOf: req.query.tagsAllOf, | |
494 | nsfw: buildNSFWFilter(res, req.query.nsfw), | |
48dce1c9 | 495 | filter: req.query.filter as VideoFilter, |
6e46de09 | 496 | withFiles: false, |
7ad9b984 | 497 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined |
48dce1c9 | 498 | }) |
eb080476 C |
499 | |
500 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9f10b292 | 501 | } |
c45f7f84 | 502 | |
eb080476 | 503 | async function removeVideo (req: express.Request, res: express.Response) { |
dae86118 | 504 | const videoInstance = res.locals.video |
91f6f169 | 505 | |
3fd3ab2d | 506 | await sequelizeTypescript.transaction(async t => { |
eb080476 | 507 | await videoInstance.destroy({ transaction: t }) |
91f6f169 | 508 | }) |
eb080476 | 509 | |
993cef4b | 510 | auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) |
eb080476 | 511 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) |
90d4bb81 C |
512 | |
513 | return res.type('json').status(204).end() | |
9f10b292 | 514 | } |