1 import express from 'express'
2 import { body, param } from 'express-validator'
3 import { isVideoTimeValid } from '@server/helpers/custom-validators/video-view'
4 import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer'
5 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
6 import { exists, isIdValid, isIntOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
7 import { logger } from '../../../helpers/logger'
8 import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
10 const getVideoLocalViewerValidator = [
11 param('localViewerId')
12 .custom(isIdValid).withMessage('Should have a valid local viewer id'),
14 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 logger.debug('Checking getVideoLocalViewerValidator parameters', { parameters: req.params })
17 if (areValidationErrors(req, res)) return
19 const localViewer = await LocalVideoViewerModel.loadFullById(+req.params.localViewerId)
22 status: HttpStatusCode.NOT_FOUND_404,
23 message: 'Local viewer not found'
27 res.locals.localViewerFull = localViewer
33 const videoViewValidator = [
34 isValidVideoIdParam('videoId'),
37 .optional() // TODO: remove optional in a few versions, introduced in 4.2
38 .customSanitizer(toIntOrNull)
39 .custom(isIntOrNull).withMessage('Should have correct current time'),
41 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
42 logger.debug('Checking videoView parameters', { parameters: req.body })
44 if (areValidationErrors(req, res)) return
45 if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return
47 const video = res.locals.onlyVideo
48 const videoDuration = video.isLive
52 if (!exists(req.body.currentTime)) { // TODO: remove in a few versions, introduced in 4.2
53 req.body.currentTime = Math.min(videoDuration ?? 0, 30)
56 const currentTime: number = req.body.currentTime
58 if (!isVideoTimeValid(currentTime, videoDuration)) {
60 status: HttpStatusCode.BAD_REQUEST_400,
61 message: 'Current time is invalid'
69 // ---------------------------------------------------------------------------
73 getVideoLocalViewerValidator