]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-watch.ts
correct error codes and backward compat
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-watch.ts
1 import { body, param } from 'express-validator'
2 import * as express from 'express'
3 import { isIdOrUUIDValid, toIntOrNull } from '../../../helpers/custom-validators/misc'
4 import { areValidationErrors } from '../utils'
5 import { logger } from '../../../helpers/logger'
6 import { doesVideoExist } from '../../../helpers/middlewares'
7 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
8
9 const videoWatchingValidator = [
10 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
11 body('currentTime')
12 .customSanitizer(toIntOrNull)
13 .isInt().withMessage('Should have correct current time'),
14
15 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
16 logger.debug('Checking videoWatching parameters', { parameters: req.body })
17
18 if (areValidationErrors(req, res)) return
19 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
20
21 const user = res.locals.oauth.token.User
22 if (user.videosHistoryEnabled === false) {
23 logger.warn('Cannot set videos to watch by user %d: videos history is disabled.', user.id)
24 return res.fail({
25 status: HttpStatusCode.CONFLICT_409,
26 message: 'Video history is disabled'
27 })
28 }
29
30 return next()
31 }
32 ]
33
34 // ---------------------------------------------------------------------------
35
36 export {
37 videoWatchingValidator
38 }