]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-watch.ts
Merge branch 'develop' into pr/1285
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-watch.ts
CommitLineData
6e46de09
C
1import { body, param } from 'express-validator/check'
2import * as express from 'express'
3import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
4import { isVideoExist } from '../../../helpers/custom-validators/videos'
5import { areValidationErrors } from '../utils'
6import { logger } from '../../../helpers/logger'
8b9a525a 7import { UserModel } from '../../../models/account/user'
6e46de09
C
8
9const videoWatchingValidator = [
10 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
11 body('currentTime')
12 .toInt()
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 isVideoExist(req.params.videoId, res, 'id')) return
20
8b9a525a
C
21 const user = res.locals.oauth.token.User as UserModel
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.status(409).end()
25 }
26
6e46de09
C
27 return next()
28 }
29]
30
31// ---------------------------------------------------------------------------
32
33export {
34 videoWatchingValidator
35}