From b211106695bb82f6c32e53306081b5262c3d109d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 24 Mar 2022 13:36:47 +0100 Subject: Support video views/viewers stats in server * Add "currentTime" and "event" body params to view endpoint * Merge watching and view endpoints * Introduce WatchAction AP activity * Add tables to store viewer information of local videos * Add endpoints to fetch video views/viewers stats of local videos * Refactor views/viewers handlers * Support "views" and "viewers" counters for both VOD and live videos --- server/middlewares/validators/videos/video-view.ts | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 server/middlewares/validators/videos/video-view.ts (limited to 'server/middlewares/validators/videos/video-view.ts') diff --git a/server/middlewares/validators/videos/video-view.ts b/server/middlewares/validators/videos/video-view.ts new file mode 100644 index 000000000..7a4994e8a --- /dev/null +++ b/server/middlewares/validators/videos/video-view.ts @@ -0,0 +1,74 @@ +import express from 'express' +import { body, param } from 'express-validator' +import { isVideoTimeValid } from '@server/helpers/custom-validators/video-view' +import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer' +import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' +import { exists, isIdValid, isIntOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' +import { logger } from '../../../helpers/logger' +import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' + +const getVideoLocalViewerValidator = [ + param('localViewerId') + .custom(isIdValid).withMessage('Should have a valid local viewer id'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking getVideoLocalViewerValidator parameters', { parameters: req.params }) + + if (areValidationErrors(req, res)) return + + const localViewer = await LocalVideoViewerModel.loadFullById(+req.params.localViewerId) + if (!localViewer) { + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Local viewer not found' + }) + } + + res.locals.localViewerFull = localViewer + + return next() + } +] + +const videoViewValidator = [ + isValidVideoIdParam('videoId'), + + body('currentTime') + .optional() // TODO: remove optional in a few versions, introduced in 4.2 + .customSanitizer(toIntOrNull) + .custom(isIntOrNull).withMessage('Should have correct current time'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoView parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return + + const video = res.locals.onlyVideo + const videoDuration = video.isLive + ? undefined + : video.duration + + if (!exists(req.body.currentTime)) { // TODO: remove in a few versions, introduced in 4.2 + req.body.currentTime = Math.min(videoDuration ?? 0, 30) + } + + const currentTime: number = req.body.currentTime + + if (!isVideoTimeValid(currentTime, videoDuration)) { + return res.fail({ + status: HttpStatusCode.BAD_REQUEST_400, + message: 'Current time is invalid' + }) + } + + return next() + } +] + +// --------------------------------------------------------------------------- + +export { + videoViewValidator, + getVideoLocalViewerValidator +} -- cgit v1.2.3