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'
9 import { getCachedVideoDuration } from '@server/lib/video'
11 const getVideoLocalViewerValidator = [
12 param('localViewerId')
15 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
16 logger.debug('Checking getVideoLocalViewerValidator parameters', { parameters: req.params })
18 if (areValidationErrors(req, res)) return
20 const localViewer = await LocalVideoViewerModel.loadFullById(+req.params.localViewerId)
23 status: HttpStatusCode.NOT_FOUND_404,
24 message: 'Local viewer not found'
28 res.locals.localViewerFull = localViewer
34 const videoViewValidator = [
35 isValidVideoIdParam('videoId'),
38 .optional() // TODO: remove optional in a few versions, introduced in 4.2
39 .customSanitizer(toIntOrNull)
42 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
43 logger.debug('Checking videoView parameters', { parameters: req.body })
45 if (areValidationErrors(req, res)) return
46 if (!await doesVideoExist(req.params.videoId, res, 'only-immutable-attributes')) return
48 const video = res.locals.onlyImmutableVideo
49 const { duration } = await getCachedVideoDuration(video.id)
51 if (!exists(req.body.currentTime)) { // TODO: remove in a few versions, introduced in 4.2
52 req.body.currentTime = Math.min(duration ?? 0, 30)
55 const currentTime: number = req.body.currentTime
57 if (!isVideoTimeValid(currentTime, duration)) {
59 status: HttpStatusCode.BAD_REQUEST_400,
60 message: 'Current time is invalid'
68 // ---------------------------------------------------------------------------
72 getVideoLocalViewerValidator