1
0
Fork 0
peertube/server/middlewares/validators/videos/video-playlists.ts

409 lines
15 KiB
TypeScript
Raw Normal View History

2019-02-26 04:55:40 -05:00
import * as express from 'express'
2019-03-05 04:58:44 -05:00
import { body, param, query, ValidationChain } from 'express-validator/check'
import { UserRight, VideoPlaylistCreate, VideoPlaylistUpdate } from '../../../../shared'
2019-02-26 04:55:40 -05:00
import { logger } from '../../../helpers/logger'
import { UserModel } from '../../../models/account/user'
import { areValidationErrors } from '../utils'
2019-03-19 04:26:50 -04:00
import { doesVideoExist, isVideoImage } from '../../../helpers/custom-validators/videos'
2019-02-26 04:55:40 -05:00
import { CONSTRAINTS_FIELDS } from '../../../initializers'
2019-03-13 11:03:03 -04:00
import { isArrayOf, isIdOrUUIDValid, isIdValid, isUUIDValid, toIntArray, toValueOrNull } from '../../../helpers/custom-validators/misc'
2019-02-26 04:55:40 -05:00
import {
2019-03-19 04:26:50 -04:00
doesVideoPlaylistExist,
2019-03-19 09:13:53 -04:00
isVideoPlaylistDescriptionValid,
2019-02-26 04:55:40 -05:00
isVideoPlaylistNameValid,
2019-03-05 04:58:44 -05:00
isVideoPlaylistPrivacyValid,
isVideoPlaylistTimestampValid,
isVideoPlaylistTypeValid
2019-02-26 04:55:40 -05:00
} from '../../../helpers/custom-validators/video-playlists'
import { VideoPlaylistModel } from '../../../models/video/video-playlist'
import { cleanUpReqFiles } from '../../../helpers/express-utils'
2019-03-19 04:26:50 -04:00
import { doesVideoChannelIdExist } from '../../../helpers/custom-validators/video-channels'
2019-02-26 04:55:40 -05:00
import { VideoPlaylistElementModel } from '../../../models/video/video-playlist-element'
import { authenticatePromiseIfNeeded } from '../../oauth'
import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
2019-03-05 04:58:44 -05:00
import { VideoPlaylistType } from '../../../../shared/models/videos/playlist/video-playlist-type.model'
2019-02-26 04:55:40 -05:00
const videoPlaylistsAddValidator = getCommonPlaylistEditAttributes().concat([
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoPlaylistsAddValidator parameters', { parameters: req.body })
if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
const body: VideoPlaylistCreate = req.body
2019-03-19 04:26:50 -04:00
if (body.videoChannelId && !await doesVideoChannelIdExist(body.videoChannelId, res)) return cleanUpReqFiles(req)
if (body.privacy === VideoPlaylistPrivacy.PUBLIC && !body.videoChannelId) {
cleanUpReqFiles(req)
return res.status(400)
.json({ error: 'Cannot set "public" a playlist that is not assigned to a channel.' })
}
2019-02-26 04:55:40 -05:00
return next()
}
])
const videoPlaylistsUpdateValidator = getCommonPlaylistEditAttributes().concat([
param('playlistId')
.custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoPlaylistsUpdateValidator parameters', { parameters: req.body })
if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
2019-03-19 04:26:50 -04:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return cleanUpReqFiles(req)
2019-02-28 05:14:26 -05:00
const videoPlaylist = res.locals.videoPlaylist
2019-02-26 04:55:40 -05:00
if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) {
return cleanUpReqFiles(req)
}
const body: VideoPlaylistUpdate = req.body
if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PRIVATE && body.privacy === VideoPlaylistPrivacy.PRIVATE) {
2019-02-28 05:14:26 -05:00
cleanUpReqFiles(req)
return res.status(400)
2019-02-28 05:14:26 -05:00
.json({ error: 'Cannot set "private" a video playlist that was not private.' })
}
const newPrivacy = body.privacy || videoPlaylist.privacy
if (newPrivacy === VideoPlaylistPrivacy.PUBLIC &&
(
(!videoPlaylist.videoChannelId && !body.videoChannelId) ||
body.videoChannelId === null
)
) {
cleanUpReqFiles(req)
return res.status(400)
.json({ error: 'Cannot set "public" a playlist that is not assigned to a channel.' })
}
2019-03-05 04:58:44 -05:00
if (videoPlaylist.type === VideoPlaylistType.WATCH_LATER) {
cleanUpReqFiles(req)
return res.status(400)
2019-03-05 04:58:44 -05:00
.json({ error: 'Cannot update a watch later playlist.' })
}
2019-03-19 04:26:50 -04:00
if (body.videoChannelId && !await doesVideoChannelIdExist(body.videoChannelId, res)) return cleanUpReqFiles(req)
2019-02-26 04:55:40 -05:00
return next()
}
])
const videoPlaylistsDeleteValidator = [
param('playlistId')
.custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoPlaylistsDeleteValidator parameters', { parameters: req.params })
if (areValidationErrors(req, res)) return
2019-03-19 04:26:50 -04:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res)) return
2019-03-05 04:58:44 -05:00
2019-03-19 05:35:15 -04:00
const videoPlaylist = res.locals.videoPlaylist
2019-03-05 04:58:44 -05:00
if (videoPlaylist.type === VideoPlaylistType.WATCH_LATER) {
return res.status(400)
2019-03-05 04:58:44 -05:00
.json({ error: 'Cannot delete a watch later playlist.' })
}
2019-02-26 04:55:40 -05:00
if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) {
return
}
return next()
}
]
const videoPlaylistsGetValidator = [
param('playlistId')
.custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoPlaylistsGetValidator parameters', { parameters: req.params })
if (areValidationErrors(req, res)) return
2019-03-19 04:26:50 -04:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res)) return
2019-02-26 04:55:40 -05:00
2019-03-19 05:35:15 -04:00
const videoPlaylist = res.locals.videoPlaylist
2019-02-28 05:14:26 -05:00
// Video is unlisted, check we used the uuid to fetch it
if (videoPlaylist.privacy === VideoPlaylistPrivacy.UNLISTED) {
if (isUUIDValid(req.params.playlistId)) return next()
return res.status(404).end()
}
2019-02-26 04:55:40 -05:00
if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
await authenticatePromiseIfNeeded(req, res)
2019-03-19 05:35:15 -04:00
const user = res.locals.oauth ? res.locals.oauth.token.User : null
2019-02-26 04:55:40 -05:00
if (
!user ||
(videoPlaylist.OwnerAccount.userId !== user.id && !user.hasRight(UserRight.UPDATE_ANY_VIDEO_PLAYLIST))
) {
return res.status(403)
.json({ error: 'Cannot get this private video playlist.' })
}
return next()
}
return next()
}
]
const videoPlaylistsAddVideoValidator = [
param('playlistId')
.custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
body('videoId')
.custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid'),
body('startTimestamp')
.optional()
2019-03-05 04:58:44 -05:00
.custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid start timestamp'),
2019-02-26 04:55:40 -05:00
body('stopTimestamp')
.optional()
2019-03-05 04:58:44 -05:00
.custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid stop timestamp'),
2019-02-26 04:55:40 -05:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoPlaylistsAddVideoValidator parameters', { parameters: req.params })
if (areValidationErrors(req, res)) return
2019-03-19 04:26:50 -04:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return
if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return
2019-02-26 04:55:40 -05:00
2019-03-19 05:35:15 -04:00
const videoPlaylist = res.locals.videoPlaylist
const video = res.locals.video
2019-02-26 04:55:40 -05:00
const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideo(videoPlaylist.id, video.id)
if (videoPlaylistElement) {
res.status(409)
.json({ error: 'This video in this playlist already exists' })
.end()
return
}
if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) {
return
}
return next()
}
]
const videoPlaylistsUpdateOrRemoveVideoValidator = [
param('playlistId')
.custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
param('videoId')
.custom(isIdOrUUIDValid).withMessage('Should have an video id/uuid'),
body('startTimestamp')
.optional()
2019-03-05 04:58:44 -05:00
.custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid start timestamp'),
2019-02-26 04:55:40 -05:00
body('stopTimestamp')
.optional()
2019-03-05 04:58:44 -05:00
.custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid stop timestamp'),
2019-02-26 04:55:40 -05:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoPlaylistsRemoveVideoValidator parameters', { parameters: req.params })
if (areValidationErrors(req, res)) return
2019-03-19 04:26:50 -04:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return
if (!await doesVideoExist(req.params.videoId, res, 'id')) return
2019-02-26 04:55:40 -05:00
2019-03-19 05:35:15 -04:00
const videoPlaylist = res.locals.videoPlaylist
const video = res.locals.video
2019-02-26 04:55:40 -05:00
const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideo(videoPlaylist.id, video.id)
if (!videoPlaylistElement) {
res.status(404)
.json({ error: 'Video playlist element not found' })
.end()
return
}
res.locals.videoPlaylistElement = videoPlaylistElement
if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return
return next()
}
]
const videoPlaylistElementAPGetValidator = [
param('playlistId')
.custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
param('videoId')
.custom(isIdOrUUIDValid).withMessage('Should have an video id/uuid'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoPlaylistElementAPGetValidator parameters', { parameters: req.params })
if (areValidationErrors(req, res)) return
const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideoForAP(req.params.playlistId, req.params.videoId)
if (!videoPlaylistElement) {
res.status(404)
.json({ error: 'Video playlist element not found' })
.end()
return
}
if (videoPlaylistElement.VideoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
return res.status(403).end()
}
res.locals.videoPlaylistElement = videoPlaylistElement
return next()
}
]
const videoPlaylistsReorderVideosValidator = [
param('playlistId')
.custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
body('startPosition')
.isInt({ min: 1 }).withMessage('Should have a valid start position'),
body('insertAfterPosition')
.isInt({ min: 0 }).withMessage('Should have a valid insert after position'),
body('reorderLength')
.optional()
.isInt({ min: 1 }).withMessage('Should have a valid range length'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoPlaylistsReorderVideosValidator parameters', { parameters: req.params })
if (areValidationErrors(req, res)) return
2019-03-19 04:26:50 -04:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return
2019-02-26 04:55:40 -05:00
2019-03-19 05:35:15 -04:00
const videoPlaylist = res.locals.videoPlaylist
2019-02-26 04:55:40 -05:00
if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return
2019-02-28 05:14:26 -05:00
const nextPosition = await VideoPlaylistElementModel.getNextPositionOf(videoPlaylist.id)
const startPosition: number = req.body.startPosition
const insertAfterPosition: number = req.body.insertAfterPosition
const reorderLength: number = req.body.reorderLength
if (startPosition >= nextPosition || insertAfterPosition >= nextPosition) {
res.status(400)
.json({ error: `Start position or insert after position exceed the playlist limits (max: ${nextPosition - 1})` })
.end()
return
}
if (reorderLength && reorderLength + startPosition > nextPosition) {
res.status(400)
.json({ error: `Reorder length with this start position exceeds the playlist limits (max: ${nextPosition - startPosition})` })
.end()
return
}
2019-02-26 04:55:40 -05:00
return next()
}
]
2019-03-05 04:58:44 -05:00
const commonVideoPlaylistFiltersValidator = [
query('playlistType')
.optional()
.custom(isVideoPlaylistTypeValid).withMessage('Should have a valid playlist type'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking commonVideoPlaylistFiltersValidator parameters', { parameters: req.params })
if (areValidationErrors(req, res)) return
return next()
}
]
2019-03-07 11:06:00 -05:00
const doVideosInPlaylistExistValidator = [
query('videoIds')
.customSanitizer(toIntArray)
.custom(v => isArrayOf(v, isIdValid)).withMessage('Should have a valid video ids array'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking areVideosInPlaylistExistValidator parameters', { parameters: req.query })
if (areValidationErrors(req, res)) return
return next()
}
]
2019-02-26 04:55:40 -05:00
// ---------------------------------------------------------------------------
export {
videoPlaylistsAddValidator,
videoPlaylistsUpdateValidator,
videoPlaylistsDeleteValidator,
videoPlaylistsGetValidator,
videoPlaylistsAddVideoValidator,
videoPlaylistsUpdateOrRemoveVideoValidator,
videoPlaylistsReorderVideosValidator,
2019-03-05 04:58:44 -05:00
videoPlaylistElementAPGetValidator,
2019-03-07 11:06:00 -05:00
commonVideoPlaylistFiltersValidator,
doVideosInPlaylistExistValidator
2019-02-26 04:55:40 -05:00
}
// ---------------------------------------------------------------------------
function getCommonPlaylistEditAttributes () {
return [
body('thumbnailfile')
.custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
'This thumbnail file is not supported or too large. Please, make sure it is of the following type: '
+ CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.IMAGE.EXTNAME.join(', ')
),
body('displayName')
.custom(isVideoPlaylistNameValid).withMessage('Should have a valid display name'),
body('description')
.optional()
.customSanitizer(toValueOrNull)
.custom(isVideoPlaylistDescriptionValid).withMessage('Should have a valid description'),
body('privacy')
.optional()
.toInt()
.custom(isVideoPlaylistPrivacyValid).withMessage('Should have correct playlist privacy'),
body('videoChannelId')
.optional()
2019-03-06 09:36:44 -05:00
.customSanitizer(toValueOrNull)
2019-02-26 04:55:40 -05:00
.toInt()
] as (ValidationChain | express.Handler)[]
}
function checkUserCanManageVideoPlaylist (user: UserModel, videoPlaylist: VideoPlaylistModel, right: UserRight, res: express.Response) {
if (videoPlaylist.isOwned() === false) {
res.status(403)
.json({ error: 'Cannot manage video playlist of another server.' })
.end()
return false
}
// Check if the user can manage the video playlist
// The user can delete it if s/he is an admin
// Or if s/he is the video playlist's owner
if (user.hasRight(right) === false && videoPlaylist.ownerAccountId !== user.Account.id) {
res.status(403)
.json({ error: 'Cannot manage video playlist of another user' })
.end()
return false
}
return true
}