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, isVideoImage } from '../../../helpers/custom-validators/videos'
8 import { CONSTRAINTS_FIELDS } from '../../../initializers'
9 import { isIdOrUUIDValid, isUUIDValid, toValueOrNull } 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'
27 const videoPlaylistsAddValidator = getCommonPlaylistEditAttributes().concat([
28 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
29 logger.debug('Checking videoPlaylistsAddValidator parameters', { parameters: req.body })
31 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
33 if (req.body.videoChannelId && !await isVideoChannelIdExist(req.body.videoChannelId, res)) return cleanUpReqFiles(req)
39 const videoPlaylistsUpdateValidator = getCommonPlaylistEditAttributes().concat([
41 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
43 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
44 logger.debug('Checking videoPlaylistsUpdateValidator parameters', { parameters: req.body })
46 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
48 if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return cleanUpReqFiles(req)
50 const videoPlaylist = res.locals.videoPlaylist
52 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) {
53 return cleanUpReqFiles(req)
56 if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PRIVATE && req.body.privacy === VideoPlaylistPrivacy.PRIVATE) {
58 return res.status(409)
59 .json({ error: 'Cannot set "private" a video playlist that was not private.' })
62 if (videoPlaylist.type === VideoPlaylistType.WATCH_LATER) {
64 return res.status(409)
65 .json({ error: 'Cannot update a watch later playlist.' })
68 if (req.body.videoChannelId && !await isVideoChannelIdExist(req.body.videoChannelId, res)) return cleanUpReqFiles(req)
74 const videoPlaylistsDeleteValidator = [
76 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
78 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
79 logger.debug('Checking videoPlaylistsDeleteValidator parameters', { parameters: req.params })
81 if (areValidationErrors(req, res)) return
83 if (!await isVideoPlaylistExist(req.params.playlistId, res)) return
85 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
86 if (videoPlaylist.type === VideoPlaylistType.WATCH_LATER) {
87 return res.status(409)
88 .json({ error: 'Cannot delete a watch later playlist.' })
91 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) {
99 const videoPlaylistsGetValidator = [
101 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
103 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
104 logger.debug('Checking videoPlaylistsGetValidator parameters', { parameters: req.params })
106 if (areValidationErrors(req, res)) return
108 if (!await isVideoPlaylistExist(req.params.playlistId, res)) return
110 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
112 // Video is unlisted, check we used the uuid to fetch it
113 if (videoPlaylist.privacy === VideoPlaylistPrivacy.UNLISTED) {
114 if (isUUIDValid(req.params.playlistId)) return next()
116 return res.status(404).end()
119 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
120 await authenticatePromiseIfNeeded(req, res)
122 const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : null
126 (videoPlaylist.OwnerAccount.userId !== user.id && !user.hasRight(UserRight.UPDATE_ANY_VIDEO_PLAYLIST))
128 return res.status(403)
129 .json({ error: 'Cannot get this private video playlist.' })
139 const videoPlaylistsAddVideoValidator = [
141 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
143 .custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid'),
144 body('startTimestamp')
146 .custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid start timestamp'),
147 body('stopTimestamp')
149 .custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid stop timestamp'),
151 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
152 logger.debug('Checking videoPlaylistsAddVideoValidator parameters', { parameters: req.params })
154 if (areValidationErrors(req, res)) return
156 if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return
157 if (!await isVideoExist(req.body.videoId, res, 'only-video')) return
159 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
160 const video: VideoModel = res.locals.video
162 const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideo(videoPlaylist.id, video.id)
163 if (videoPlaylistElement) {
165 .json({ error: 'This video in this playlist already exists' })
171 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) {
179 const videoPlaylistsUpdateOrRemoveVideoValidator = [
181 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
183 .custom(isIdOrUUIDValid).withMessage('Should have an video id/uuid'),
184 body('startTimestamp')
186 .custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid start timestamp'),
187 body('stopTimestamp')
189 .custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid stop timestamp'),
191 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
192 logger.debug('Checking videoPlaylistsRemoveVideoValidator parameters', { parameters: req.params })
194 if (areValidationErrors(req, res)) return
196 if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return
197 if (!await isVideoExist(req.params.videoId, res, 'id')) return
199 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
200 const video: VideoModel = res.locals.video
202 const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideo(videoPlaylist.id, video.id)
203 if (!videoPlaylistElement) {
205 .json({ error: 'Video playlist element not found' })
210 res.locals.videoPlaylistElement = videoPlaylistElement
212 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return
218 const videoPlaylistElementAPGetValidator = [
220 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
222 .custom(isIdOrUUIDValid).withMessage('Should have an video id/uuid'),
224 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
225 logger.debug('Checking videoPlaylistElementAPGetValidator parameters', { parameters: req.params })
227 if (areValidationErrors(req, res)) return
229 const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideoForAP(req.params.playlistId, req.params.videoId)
230 if (!videoPlaylistElement) {
232 .json({ error: 'Video playlist element not found' })
238 if (videoPlaylistElement.VideoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
239 return res.status(403).end()
242 res.locals.videoPlaylistElement = videoPlaylistElement
248 const videoPlaylistsReorderVideosValidator = [
250 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
251 body('startPosition')
252 .isInt({ min: 1 }).withMessage('Should have a valid start position'),
253 body('insertAfterPosition')
254 .isInt({ min: 0 }).withMessage('Should have a valid insert after position'),
255 body('reorderLength')
257 .isInt({ min: 1 }).withMessage('Should have a valid range length'),
259 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
260 logger.debug('Checking videoPlaylistsReorderVideosValidator parameters', { parameters: req.params })
262 if (areValidationErrors(req, res)) return
264 if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return
266 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
267 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return
269 const nextPosition = await VideoPlaylistElementModel.getNextPositionOf(videoPlaylist.id)
270 const startPosition: number = req.body.startPosition
271 const insertAfterPosition: number = req.body.insertAfterPosition
272 const reorderLength: number = req.body.reorderLength
274 if (startPosition >= nextPosition || insertAfterPosition >= nextPosition) {
276 .json({ error: `Start position or insert after position exceed the playlist limits (max: ${nextPosition - 1})` })
282 if (reorderLength && reorderLength + startPosition > nextPosition) {
284 .json({ error: `Reorder length with this start position exceeds the playlist limits (max: ${nextPosition - startPosition})` })
294 const commonVideoPlaylistFiltersValidator = [
295 query('playlistType')
297 .custom(isVideoPlaylistTypeValid).withMessage('Should have a valid playlist type'),
299 (req: express.Request, res: express.Response, next: express.NextFunction) => {
300 logger.debug('Checking commonVideoPlaylistFiltersValidator parameters', { parameters: req.params })
302 if (areValidationErrors(req, res)) return
308 // ---------------------------------------------------------------------------
311 videoPlaylistsAddValidator,
312 videoPlaylistsUpdateValidator,
313 videoPlaylistsDeleteValidator,
314 videoPlaylistsGetValidator,
316 videoPlaylistsAddVideoValidator,
317 videoPlaylistsUpdateOrRemoveVideoValidator,
318 videoPlaylistsReorderVideosValidator,
320 videoPlaylistElementAPGetValidator,
322 commonVideoPlaylistFiltersValidator
325 // ---------------------------------------------------------------------------
327 function getCommonPlaylistEditAttributes () {
329 body('thumbnailfile')
330 .custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
331 'This thumbnail file is not supported or too large. Please, make sure it is of the following type: '
332 + CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.IMAGE.EXTNAME.join(', ')
336 .custom(isVideoPlaylistNameValid).withMessage('Should have a valid display name'),
339 .customSanitizer(toValueOrNull)
340 .custom(isVideoPlaylistDescriptionValid).withMessage('Should have a valid description'),
344 .custom(isVideoPlaylistPrivacyValid).withMessage('Should have correct playlist privacy'),
345 body('videoChannelId')
348 ] as (ValidationChain | express.Handler)[]
351 function checkUserCanManageVideoPlaylist (user: UserModel, videoPlaylist: VideoPlaylistModel, right: UserRight, res: express.Response) {
352 if (videoPlaylist.isOwned() === false) {
354 .json({ error: 'Cannot manage video playlist of another server.' })
360 // Check if the user can manage the video playlist
361 // The user can delete it if s/he is an admin
362 // Or if s/he is the video playlist's owner
363 if (user.hasRight(right) === false && videoPlaylist.ownerAccountId !== user.Account.id) {
365 .json({ error: 'Cannot manage video playlist of another user' })