1 import * as express from 'express'
2 import { body, param, query, ValidationChain } from 'express-validator/check'
3 import { UserRight } from '../../../../shared'
4 import { logger } from '../../../helpers/logger'
5 import { UserModel } from '../../../models/account/user'
6 import { areValidationErrors } from '../utils'
7 import { isVideoExist, isVideoFileInfoHashValid, isVideoImage } from '../../../helpers/custom-validators/videos'
8 import { CONSTRAINTS_FIELDS } from '../../../initializers'
9 import { isArrayOf, isIdOrUUIDValid, isIdValid, isUUIDValid, toArray, toValueOrNull, toIntArray } from '../../../helpers/custom-validators/misc'
11 isVideoPlaylistDescriptionValid,
13 isVideoPlaylistNameValid,
14 isVideoPlaylistPrivacyValid,
15 isVideoPlaylistTimestampValid,
16 isVideoPlaylistTypeValid
17 } from '../../../helpers/custom-validators/video-playlists'
18 import { VideoPlaylistModel } from '../../../models/video/video-playlist'
19 import { cleanUpReqFiles } from '../../../helpers/express-utils'
20 import { isVideoChannelIdExist } from '../../../helpers/custom-validators/video-channels'
21 import { VideoPlaylistElementModel } from '../../../models/video/video-playlist-element'
22 import { VideoModel } from '../../../models/video/video'
23 import { authenticatePromiseIfNeeded } from '../../oauth'
24 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
25 import { VideoPlaylistType } from '../../../../shared/models/videos/playlist/video-playlist-type.model'
26 import { areValidActorHandles } from '../../../helpers/custom-validators/activitypub/actor'
28 const videoPlaylistsAddValidator = getCommonPlaylistEditAttributes().concat([
29 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
30 logger.debug('Checking videoPlaylistsAddValidator parameters', { parameters: req.body })
32 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
34 if (req.body.videoChannelId && !await isVideoChannelIdExist(req.body.videoChannelId, res)) return cleanUpReqFiles(req)
40 const videoPlaylistsUpdateValidator = getCommonPlaylistEditAttributes().concat([
42 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
44 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
45 logger.debug('Checking videoPlaylistsUpdateValidator parameters', { parameters: req.body })
47 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
49 if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return cleanUpReqFiles(req)
51 const videoPlaylist = res.locals.videoPlaylist
53 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) {
54 return cleanUpReqFiles(req)
57 if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PRIVATE && req.body.privacy === VideoPlaylistPrivacy.PRIVATE) {
59 return res.status(409)
60 .json({ error: 'Cannot set "private" a video playlist that was not private.' })
63 if (videoPlaylist.type === VideoPlaylistType.WATCH_LATER) {
65 return res.status(409)
66 .json({ error: 'Cannot update a watch later playlist.' })
69 if (req.body.videoChannelId && !await isVideoChannelIdExist(req.body.videoChannelId, res)) return cleanUpReqFiles(req)
75 const videoPlaylistsDeleteValidator = [
77 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
79 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
80 logger.debug('Checking videoPlaylistsDeleteValidator parameters', { parameters: req.params })
82 if (areValidationErrors(req, res)) return
84 if (!await isVideoPlaylistExist(req.params.playlistId, res)) return
86 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
87 if (videoPlaylist.type === VideoPlaylistType.WATCH_LATER) {
88 return res.status(409)
89 .json({ error: 'Cannot delete a watch later playlist.' })
92 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) {
100 const videoPlaylistsGetValidator = [
102 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
104 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
105 logger.debug('Checking videoPlaylistsGetValidator parameters', { parameters: req.params })
107 if (areValidationErrors(req, res)) return
109 if (!await isVideoPlaylistExist(req.params.playlistId, res)) return
111 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
113 // Video is unlisted, check we used the uuid to fetch it
114 if (videoPlaylist.privacy === VideoPlaylistPrivacy.UNLISTED) {
115 if (isUUIDValid(req.params.playlistId)) return next()
117 return res.status(404).end()
120 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
121 await authenticatePromiseIfNeeded(req, res)
123 const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : null
127 (videoPlaylist.OwnerAccount.userId !== user.id && !user.hasRight(UserRight.UPDATE_ANY_VIDEO_PLAYLIST))
129 return res.status(403)
130 .json({ error: 'Cannot get this private video playlist.' })
140 const videoPlaylistsAddVideoValidator = [
142 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
144 .custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid'),
145 body('startTimestamp')
147 .custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid start timestamp'),
148 body('stopTimestamp')
150 .custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid stop timestamp'),
152 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
153 logger.debug('Checking videoPlaylistsAddVideoValidator parameters', { parameters: req.params })
155 if (areValidationErrors(req, res)) return
157 if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return
158 if (!await isVideoExist(req.body.videoId, res, 'only-video')) return
160 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
161 const video: VideoModel = res.locals.video
163 const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideo(videoPlaylist.id, video.id)
164 if (videoPlaylistElement) {
166 .json({ error: 'This video in this playlist already exists' })
172 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) {
180 const videoPlaylistsUpdateOrRemoveVideoValidator = [
182 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
184 .custom(isIdOrUUIDValid).withMessage('Should have an video id/uuid'),
185 body('startTimestamp')
187 .custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid start timestamp'),
188 body('stopTimestamp')
190 .custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid stop timestamp'),
192 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
193 logger.debug('Checking videoPlaylistsRemoveVideoValidator parameters', { parameters: req.params })
195 if (areValidationErrors(req, res)) return
197 if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return
198 if (!await isVideoExist(req.params.videoId, res, 'id')) return
200 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
201 const video: VideoModel = res.locals.video
203 const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideo(videoPlaylist.id, video.id)
204 if (!videoPlaylistElement) {
206 .json({ error: 'Video playlist element not found' })
211 res.locals.videoPlaylistElement = videoPlaylistElement
213 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return
219 const videoPlaylistElementAPGetValidator = [
221 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
223 .custom(isIdOrUUIDValid).withMessage('Should have an video id/uuid'),
225 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
226 logger.debug('Checking videoPlaylistElementAPGetValidator parameters', { parameters: req.params })
228 if (areValidationErrors(req, res)) return
230 const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideoForAP(req.params.playlistId, req.params.videoId)
231 if (!videoPlaylistElement) {
233 .json({ error: 'Video playlist element not found' })
239 if (videoPlaylistElement.VideoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
240 return res.status(403).end()
243 res.locals.videoPlaylistElement = videoPlaylistElement
249 const videoPlaylistsReorderVideosValidator = [
251 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
252 body('startPosition')
253 .isInt({ min: 1 }).withMessage('Should have a valid start position'),
254 body('insertAfterPosition')
255 .isInt({ min: 0 }).withMessage('Should have a valid insert after position'),
256 body('reorderLength')
258 .isInt({ min: 1 }).withMessage('Should have a valid range length'),
260 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
261 logger.debug('Checking videoPlaylistsReorderVideosValidator parameters', { parameters: req.params })
263 if (areValidationErrors(req, res)) return
265 if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return
267 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
268 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return
270 const nextPosition = await VideoPlaylistElementModel.getNextPositionOf(videoPlaylist.id)
271 const startPosition: number = req.body.startPosition
272 const insertAfterPosition: number = req.body.insertAfterPosition
273 const reorderLength: number = req.body.reorderLength
275 if (startPosition >= nextPosition || insertAfterPosition >= nextPosition) {
277 .json({ error: `Start position or insert after position exceed the playlist limits (max: ${nextPosition - 1})` })
283 if (reorderLength && reorderLength + startPosition > nextPosition) {
285 .json({ error: `Reorder length with this start position exceeds the playlist limits (max: ${nextPosition - startPosition})` })
295 const commonVideoPlaylistFiltersValidator = [
296 query('playlistType')
298 .custom(isVideoPlaylistTypeValid).withMessage('Should have a valid playlist type'),
300 (req: express.Request, res: express.Response, next: express.NextFunction) => {
301 logger.debug('Checking commonVideoPlaylistFiltersValidator parameters', { parameters: req.params })
303 if (areValidationErrors(req, res)) return
309 const doVideosInPlaylistExistValidator = [
311 .customSanitizer(toIntArray)
312 .custom(v => isArrayOf(v, isIdValid)).withMessage('Should have a valid video ids array'),
314 (req: express.Request, res: express.Response, next: express.NextFunction) => {
315 logger.debug('Checking areVideosInPlaylistExistValidator parameters', { parameters: req.query })
317 if (areValidationErrors(req, res)) return
323 // ---------------------------------------------------------------------------
326 videoPlaylistsAddValidator,
327 videoPlaylistsUpdateValidator,
328 videoPlaylistsDeleteValidator,
329 videoPlaylistsGetValidator,
331 videoPlaylistsAddVideoValidator,
332 videoPlaylistsUpdateOrRemoveVideoValidator,
333 videoPlaylistsReorderVideosValidator,
335 videoPlaylistElementAPGetValidator,
337 commonVideoPlaylistFiltersValidator,
339 doVideosInPlaylistExistValidator
342 // ---------------------------------------------------------------------------
344 function getCommonPlaylistEditAttributes () {
346 body('thumbnailfile')
347 .custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
348 'This thumbnail file is not supported or too large. Please, make sure it is of the following type: '
349 + CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.IMAGE.EXTNAME.join(', ')
353 .custom(isVideoPlaylistNameValid).withMessage('Should have a valid display name'),
356 .customSanitizer(toValueOrNull)
357 .custom(isVideoPlaylistDescriptionValid).withMessage('Should have a valid description'),
361 .custom(isVideoPlaylistPrivacyValid).withMessage('Should have correct playlist privacy'),
362 body('videoChannelId')
364 .customSanitizer(toValueOrNull)
366 ] as (ValidationChain | express.Handler)[]
369 function checkUserCanManageVideoPlaylist (user: UserModel, videoPlaylist: VideoPlaylistModel, right: UserRight, res: express.Response) {
370 if (videoPlaylist.isOwned() === false) {
372 .json({ error: 'Cannot manage video playlist of another server.' })
378 // Check if the user can manage the video playlist
379 // The user can delete it if s/he is an admin
380 // Or if s/he is the video playlist's owner
381 if (user.hasRight(right) === false && videoPlaylist.ownerAccountId !== user.Account.id) {
383 .json({ error: 'Cannot manage video playlist of another user' })