PeerTube/server/middlewares/validators/videos/video-playlists.ts

439 lines
15 KiB
TypeScript
Raw Normal View History

2021-08-27 07:32:44 -05:00
import express from 'express'
2019-07-25 09:23:44 -05:00
import { body, param, query, ValidationChain } from 'express-validator'
import { ExpressPromiseHandler } from '@server/types/express-handler'
2021-02-03 02:33:05 -06:00
import { MUserAccountId } from '@server/types/models'
2021-12-24 03:14:47 -06:00
import {
HttpStatusCode,
UserRight,
VideoPlaylistCreate,
VideoPlaylistPrivacy,
VideoPlaylistType,
VideoPlaylistUpdate
} from '@shared/models'
2019-07-25 09:23:44 -05:00
import {
isArrayOf,
isIdOrUUIDValid,
isIdValid,
isUUIDValid,
toCompleteUUID,
2019-07-25 09:23:44 -05:00
toIntArray,
toIntOrNull,
toValueOrNull
} from '../../../helpers/custom-validators/misc'
2019-02-26 03:55:40 -06:00
import {
2019-03-19 08:13:53 -05:00
isVideoPlaylistDescriptionValid,
2019-02-26 03:55:40 -06:00
isVideoPlaylistNameValid,
2019-03-05 03:58:44 -06:00
isVideoPlaylistPrivacyValid,
isVideoPlaylistTimestampValid,
isVideoPlaylistTypeValid
2019-02-26 03:55:40 -06:00
} from '../../../helpers/custom-validators/video-playlists'
2021-02-03 02:33:05 -06:00
import { isVideoImage } from '../../../helpers/custom-validators/videos'
2019-02-26 03:55:40 -06:00
import { cleanUpReqFiles } from '../../../helpers/express-utils'
2021-02-03 02:33:05 -06:00
import { logger } from '../../../helpers/logger'
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
import { VideoPlaylistElementModel } from '../../../models/video/video-playlist-element'
2020-06-18 03:45:25 -05:00
import { MVideoPlaylist } from '../../../types/models/video/video-playlist'
import { authenticatePromiseIfNeeded } from '../../auth'
import {
areValidationErrors,
doesVideoChannelIdExist,
doesVideoExist,
doesVideoPlaylistExist,
isValidPlaylistIdParam,
VideoPlaylistFetchType
} from '../shared'
2019-02-26 03:55:40 -06:00
const videoPlaylistsAddValidator = getCommonPlaylistEditAttributes().concat([
body('displayName')
.custom(isVideoPlaylistNameValid).withMessage('Should have a valid display name'),
2019-02-26 03:55:40 -06:00
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 03:26:50 -05:00
if (body.videoChannelId && !await doesVideoChannelIdExist(body.videoChannelId, res)) return cleanUpReqFiles(req)
if (
!body.videoChannelId &&
(body.privacy === VideoPlaylistPrivacy.PUBLIC || body.privacy === VideoPlaylistPrivacy.UNLISTED)
) {
cleanUpReqFiles(req)
return res.fail({ message: 'Cannot set "public" or "unlisted" a playlist that is not assigned to a channel.' })
}
2019-02-26 03:55:40 -06:00
return next()
}
])
const videoPlaylistsUpdateValidator = getCommonPlaylistEditAttributes().concat([
isValidPlaylistIdParam('playlistId'),
2019-02-26 03:55:40 -06:00
body('displayName')
.optional()
.custom(isVideoPlaylistNameValid).withMessage('Should have a valid display name'),
2019-02-26 03:55:40 -06:00
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 03:26:50 -05:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return cleanUpReqFiles(req)
2019-02-28 04:14:26 -06:00
2019-08-15 04:53:26 -05:00
const videoPlaylist = getPlaylist(res)
2019-02-28 04:14:26 -06:00
2019-08-15 04:53:26 -05:00
if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) {
2019-02-26 03:55:40 -06:00
return cleanUpReqFiles(req)
}
const body: VideoPlaylistUpdate = req.body
const newPrivacy = body.privacy || videoPlaylist.privacy
if (newPrivacy === VideoPlaylistPrivacy.PUBLIC &&
(
(!videoPlaylist.videoChannelId && !body.videoChannelId) ||
body.videoChannelId === null
)
) {
cleanUpReqFiles(req)
return res.fail({ message: 'Cannot set "public" a playlist that is not assigned to a channel.' })
}
2019-03-05 03:58:44 -06:00
if (videoPlaylist.type === VideoPlaylistType.WATCH_LATER) {
cleanUpReqFiles(req)
return res.fail({ message: 'Cannot update a watch later playlist.' })
2019-03-05 03:58:44 -06:00
}
2019-03-19 03:26:50 -05:00
if (body.videoChannelId && !await doesVideoChannelIdExist(body.videoChannelId, res)) return cleanUpReqFiles(req)
2019-02-26 03:55:40 -06:00
return next()
}
])
const videoPlaylistsDeleteValidator = [
isValidPlaylistIdParam('playlistId'),
2019-02-26 03:55:40 -06:00
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 03:26:50 -05:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res)) return
2019-03-05 03:58:44 -06:00
2019-08-15 04:53:26 -05:00
const videoPlaylist = getPlaylist(res)
2019-03-05 03:58:44 -06:00
if (videoPlaylist.type === VideoPlaylistType.WATCH_LATER) {
return res.fail({ message: 'Cannot delete a watch later playlist.' })
2019-03-05 03:58:44 -06:00
}
2019-08-15 04:53:26 -05:00
if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) {
2019-02-26 03:55:40 -06:00
return
}
return next()
}
]
2019-08-15 04:53:26 -05:00
const videoPlaylistsGetValidator = (fetchType: VideoPlaylistFetchType) => {
return [
isValidPlaylistIdParam('playlistId'),
2019-02-26 03:55:40 -06:00
2019-08-15 04:53:26 -05:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoPlaylistsGetValidator parameters', { parameters: req.params })
2019-02-26 03:55:40 -06:00
2019-08-15 04:53:26 -05:00
if (areValidationErrors(req, res)) return
2019-02-26 03:55:40 -06:00
2019-08-15 04:53:26 -05:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res, fetchType)) return
2019-02-26 03:55:40 -06:00
2019-08-15 04:53:26 -05:00
const videoPlaylist = res.locals.videoPlaylistFull || res.locals.videoPlaylistSummary
2019-02-28 04:14:26 -06:00
2019-08-15 04:53: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()
2019-02-28 04:14:26 -06:00
return res.fail({
status: HttpStatusCode.NOT_FOUND_404,
message: 'Playlist not found'
})
2019-08-15 04:53:26 -05:00
}
2019-02-28 04:14:26 -06:00
2019-08-15 04:53:26 -05:00
if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
await authenticatePromiseIfNeeded(req, res)
2019-02-26 03:55:40 -06:00
2019-08-15 04:53:26 -05:00
const user = res.locals.oauth ? res.locals.oauth.token.User : null
2019-05-28 02:50:27 -05:00
2019-08-15 04:53:26 -05:00
if (
!user ||
(videoPlaylist.OwnerAccount.id !== user.Account.id && !user.hasRight(UserRight.UPDATE_ANY_VIDEO_PLAYLIST))
) {
return res.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: 'Cannot get this private video playlist.'
})
2019-08-15 04:53:26 -05:00
}
return next()
2019-02-26 03:55:40 -06:00
}
return next()
}
2019-08-15 04:53:26 -05:00
]
}
2019-02-26 03:55:40 -06:00
const videoPlaylistsSearchValidator = [
query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoPlaylists search query', { parameters: req.query })
if (areValidationErrors(req, res)) return
return next()
}
]
2019-02-26 03:55:40 -06:00
const videoPlaylistsAddVideoValidator = [
isValidPlaylistIdParam('playlistId'),
2019-02-26 03:55:40 -06:00
body('videoId')
.customSanitizer(toCompleteUUID)
2019-02-26 03:55:40 -06:00
.custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid'),
body('startTimestamp')
.optional()
2019-03-05 03:58:44 -06:00
.custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid start timestamp'),
2019-02-26 03:55:40 -06:00
body('stopTimestamp')
.optional()
2019-03-05 03:58:44 -06:00
.custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid stop timestamp'),
2019-02-26 03:55:40 -06: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 03:26:50 -05:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return
if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return
2019-02-26 03:55:40 -06:00
2019-08-15 04:53:26 -05:00
const videoPlaylist = getPlaylist(res)
2019-02-26 03:55:40 -06:00
2019-08-15 04:53:26 -05:00
if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) {
2019-02-26 03:55:40 -06:00
return
}
return next()
}
]
const videoPlaylistsUpdateOrRemoveVideoValidator = [
isValidPlaylistIdParam('playlistId'),
2019-07-31 08:57:32 -05:00
param('playlistElementId')
.customSanitizer(toCompleteUUID)
2019-07-31 08:57:32 -05:00
.custom(isIdValid).withMessage('Should have an element id/uuid'),
2019-02-26 03:55:40 -06:00
body('startTimestamp')
.optional()
2019-03-05 03:58:44 -06:00
.custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid start timestamp'),
2019-02-26 03:55:40 -06:00
body('stopTimestamp')
.optional()
2019-03-05 03:58:44 -06:00
.custom(isVideoPlaylistTimestampValid).withMessage('Should have a valid stop timestamp'),
2019-02-26 03:55:40 -06: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 03:26:50 -05:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return
2019-02-26 03:55:40 -06:00
2019-08-15 04:53:26 -05:00
const videoPlaylist = getPlaylist(res)
2019-02-26 03:55:40 -06:00
2019-07-31 08:57:32 -05:00
const videoPlaylistElement = await VideoPlaylistElementModel.loadById(req.params.playlistElementId)
2019-02-26 03:55:40 -06:00
if (!videoPlaylistElement) {
res.fail({
status: HttpStatusCode.NOT_FOUND_404,
message: 'Video playlist element not found'
})
2019-02-26 03:55:40 -06:00
return
}
res.locals.videoPlaylistElement = videoPlaylistElement
if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return
return next()
}
]
const videoPlaylistElementAPGetValidator = [
isValidPlaylistIdParam('playlistId'),
param('playlistElementId')
.custom(isIdValid).withMessage('Should have an playlist element id'),
2019-02-26 03:55:40 -06:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoPlaylistElementAPGetValidator parameters', { parameters: req.params })
if (areValidationErrors(req, res)) return
const playlistElementId = parseInt(req.params.playlistElementId + '', 10)
const playlistId = req.params.playlistId
const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndElementIdForAP(playlistId, playlistElementId)
2019-02-26 03:55:40 -06:00
if (!videoPlaylistElement) {
res.fail({
status: HttpStatusCode.NOT_FOUND_404,
message: 'Video playlist element not found'
})
2019-02-26 03:55:40 -06:00
return
}
if (videoPlaylistElement.VideoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
return res.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: 'Cannot get this private video playlist.'
})
2019-02-26 03:55:40 -06:00
}
2019-08-21 07:31:57 -05:00
res.locals.videoPlaylistElementAP = videoPlaylistElement
2019-02-26 03:55:40 -06:00
return next()
}
]
const videoPlaylistsReorderVideosValidator = [
isValidPlaylistIdParam('playlistId'),
2019-02-26 03:55:40 -06:00
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 03:26:50 -05:00
if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return
2019-02-26 03:55:40 -06:00
2019-08-15 04:53:26 -05:00
const videoPlaylist = getPlaylist(res)
2019-02-26 03:55:40 -06:00
if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return
2019-02-28 04:14:26 -06: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.fail({ message: `Start position or insert after position exceed the playlist limits (max: ${nextPosition - 1})` })
2019-02-28 04:14:26 -06:00
return
}
if (reorderLength && reorderLength + startPosition > nextPosition) {
res.fail({ message: `Reorder length with this start position exceeds the playlist limits (max: ${nextPosition - startPosition})` })
2019-02-28 04:14:26 -06:00
return
}
2019-02-26 03:55:40 -06:00
return next()
}
]
2019-03-05 03:58:44 -06: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 10:06:00 -06: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 03:55:40 -06:00
// ---------------------------------------------------------------------------
export {
videoPlaylistsAddValidator,
videoPlaylistsUpdateValidator,
videoPlaylistsDeleteValidator,
videoPlaylistsGetValidator,
videoPlaylistsSearchValidator,
2019-02-26 03:55:40 -06:00
videoPlaylistsAddVideoValidator,
videoPlaylistsUpdateOrRemoveVideoValidator,
videoPlaylistsReorderVideosValidator,
2019-03-05 03:58:44 -06:00
videoPlaylistElementAPGetValidator,
2019-03-07 10:06:00 -06:00
commonVideoPlaylistFiltersValidator,
doVideosInPlaylistExistValidator
2019-02-26 03:55:40 -06:00
}
// ---------------------------------------------------------------------------
function getCommonPlaylistEditAttributes () {
return [
body('thumbnailfile')
2020-01-31 09:56:52 -06:00
.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(', ')
),
2019-02-26 03:55:40 -06:00
body('description')
.optional()
.customSanitizer(toValueOrNull)
.custom(isVideoPlaylistDescriptionValid).withMessage('Should have a valid description'),
body('privacy')
.optional()
2019-07-25 09:23:44 -05:00
.customSanitizer(toIntOrNull)
2019-02-26 03:55:40 -06:00
.custom(isVideoPlaylistPrivacyValid).withMessage('Should have correct playlist privacy'),
body('videoChannelId')
.optional()
2019-07-25 09:23:44 -05:00
.customSanitizer(toIntOrNull)
2021-02-03 02:33:05 -06:00
] as (ValidationChain | ExpressPromiseHandler)[]
2019-02-26 03:55:40 -06:00
}
2019-08-15 04:53:26 -05:00
function checkUserCanManageVideoPlaylist (user: MUserAccountId, videoPlaylist: MVideoPlaylist, right: UserRight, res: express.Response) {
2019-02-26 03:55:40 -06:00
if (videoPlaylist.isOwned() === false) {
res.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: 'Cannot manage video playlist of another server.'
})
2019-02-26 03:55:40 -06:00
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.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: 'Cannot manage video playlist of another user'
})
2019-02-26 03:55:40 -06:00
return false
}
return true
}
2019-08-15 04:53:26 -05:00
function getPlaylist (res: express.Response) {
return res.locals.videoPlaylistFull || res.locals.videoPlaylistSummary
}