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