1 import express from 'express'
2 import { body, param } from 'express-validator'
3 import { isVideoTimeValid } from '@server/helpers/custom-validators/video-view'
4 import { getCachedVideoDuration } from '@server/lib/video'
5 import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer'
6 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
7 import { exists, isIdValid, isIntOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
8 import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
10 const getVideoLocalViewerValidator = [
11 param('localViewerId')
14 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 if (areValidationErrors(req, res)) return
17 const localViewer = await LocalVideoViewerModel.loadFullById(+req.params.localViewerId)
20 status: HttpStatusCode.NOT_FOUND_404,
21 message: 'Local viewer not found'
25 res.locals.localViewerFull = localViewer
31 const videoViewValidator = [
32 isValidVideoIdParam('videoId'),
35 .optional() // TODO: remove optional in a few versions, introduced in 4.2
36 .customSanitizer(toIntOrNull)
39 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
40 if (areValidationErrors(req, res)) return
41 if (!await doesVideoExist(req.params.videoId, res, 'only-immutable-attributes')) return
43 const video = res.locals.onlyImmutableVideo
44 const { duration } = await getCachedVideoDuration(video.id)
46 if (!exists(req.body.currentTime)) { // TODO: remove in a few versions, introduced in 4.2
47 req.body.currentTime = Math.min(duration ?? 0, 30)
50 const currentTime: number = req.body.currentTime
52 if (!isVideoTimeValid(currentTime, duration)) {
54 status: HttpStatusCode.BAD_REQUEST_400,
55 message: 'Current time is invalid'
63 // ---------------------------------------------------------------------------
67 getVideoLocalViewerValidator