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/cache/shared/api-cache.ts | 4 +- server/middlewares/validators/express.ts | 15 +++++ server/middlewares/validators/index.ts | 23 +++++-- server/middlewares/validators/videos/index.ts | 3 +- .../middlewares/validators/videos/video-stats.ts | 73 +++++++++++++++++++++ server/middlewares/validators/videos/video-view.ts | 74 ++++++++++++++++++++++ .../middlewares/validators/videos/video-watch.ts | 38 ----------- 7 files changed, 182 insertions(+), 48 deletions(-) create mode 100644 server/middlewares/validators/express.ts create mode 100644 server/middlewares/validators/videos/video-stats.ts create mode 100644 server/middlewares/validators/videos/video-view.ts delete mode 100644 server/middlewares/validators/videos/video-watch.ts (limited to 'server/middlewares') diff --git a/server/middlewares/cache/shared/api-cache.ts b/server/middlewares/cache/shared/api-cache.ts index 86c5095b5..abc919339 100644 --- a/server/middlewares/cache/shared/api-cache.ts +++ b/server/middlewares/cache/shared/api-cache.ts @@ -6,8 +6,8 @@ import { OutgoingHttpHeaders } from 'http' import { isTestInstance, parseDurationToMs } from '@server/helpers/core-utils' import { logger } from '@server/helpers/logger' import { Redis } from '@server/lib/redis' -import { HttpStatusCode } from '@shared/models' import { asyncMiddleware } from '@server/middlewares' +import { HttpStatusCode } from '@shared/models' export interface APICacheOptions { headerBlacklist?: string[] @@ -152,7 +152,7 @@ export class ApiCache { end: res.end, cacheable: true, content: undefined, - headers: {} + headers: undefined } // Patch express diff --git a/server/middlewares/validators/express.ts b/server/middlewares/validators/express.ts new file mode 100644 index 000000000..718aec55b --- /dev/null +++ b/server/middlewares/validators/express.ts @@ -0,0 +1,15 @@ +import * as express from 'express' + +const methodsValidator = (methods: string[]) => { + return (req: express.Request, res: express.Response, next: express.NextFunction) => { + if (methods.includes(req.method) !== true) { + return res.sendStatus(405) + } + + return next() + } +} + +export { + methodsValidator +} diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts index 94a3c2dea..b0ad04819 100644 --- a/server/middlewares/validators/index.ts +++ b/server/middlewares/validators/index.ts @@ -1,17 +1,26 @@ +export * from './activitypub' +export * from './videos' export * from './abuse' export * from './account' export * from './actor-image' export * from './blocklist' +export * from './bulk' +export * from './config' +export * from './express' +export * from './feeds' +export * from './follows' +export * from './jobs' +export * from './logs' export * from './oembed' -export * from './activitypub' export * from './pagination' -export * from './follows' -export * from './feeds' -export * from './sort' -export * from './users' -export * from './user-subscriptions' -export * from './videos' +export * from './plugins' +export * from './redundancy' export * from './search' export * from './server' +export * from './sort' +export * from './themes' export * from './user-history' +export * from './user-notifications' +export * from './user-subscriptions' +export * from './users' export * from './webfinger' diff --git a/server/middlewares/validators/videos/index.ts b/server/middlewares/validators/videos/index.ts index c7dea4b3d..bd2590bc5 100644 --- a/server/middlewares/validators/videos/index.ts +++ b/server/middlewares/validators/videos/index.ts @@ -6,9 +6,10 @@ export * from './video-files' export * from './video-imports' export * from './video-live' export * from './video-ownership-changes' -export * from './video-watch' +export * from './video-view' export * from './video-rates' export * from './video-shares' +export * from './video-stats' export * from './video-studio' export * from './video-transcoding' export * from './videos' diff --git a/server/middlewares/validators/videos/video-stats.ts b/server/middlewares/validators/videos/video-stats.ts new file mode 100644 index 000000000..358b6b473 --- /dev/null +++ b/server/middlewares/validators/videos/video-stats.ts @@ -0,0 +1,73 @@ +import express from 'express' +import { param } from 'express-validator' +import { isValidStatTimeserieMetric } from '@server/helpers/custom-validators/video-stats' +import { HttpStatusCode, UserRight } from '@shared/models' +import { logger } from '../../../helpers/logger' +import { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared' + +const videoOverallStatsValidator = [ + isValidVideoIdParam('videoId'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoOverallStatsValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + if (!await commonStatsCheck(req, res)) return + + return next() + } +] + +const videoRetentionStatsValidator = [ + isValidVideoIdParam('videoId'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoRetentionStatsValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + if (!await commonStatsCheck(req, res)) return + + if (res.locals.videoAll.isLive) { + return res.fail({ + status: HttpStatusCode.BAD_REQUEST_400, + message: 'Cannot get retention stats of live video' + }) + } + + return next() + } +] + +const videoTimeserieStatsValidator = [ + isValidVideoIdParam('videoId'), + + param('metric') + .custom(isValidStatTimeserieMetric) + .withMessage('Should have a valid timeserie metric'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoTimeserieStatsValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + if (!await commonStatsCheck(req, res)) return + + return next() + } +] + +// --------------------------------------------------------------------------- + +export { + videoOverallStatsValidator, + videoTimeserieStatsValidator, + videoRetentionStatsValidator +} + +// --------------------------------------------------------------------------- + +async function commonStatsCheck (req: express.Request, res: express.Response) { + if (!await doesVideoExist(req.params.videoId, res, 'all')) return false + if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.SEE_ALL_VIDEOS, res)) return false + + return true +} 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 +} diff --git a/server/middlewares/validators/videos/video-watch.ts b/server/middlewares/validators/videos/video-watch.ts deleted file mode 100644 index d83710a64..000000000 --- a/server/middlewares/validators/videos/video-watch.ts +++ /dev/null @@ -1,38 +0,0 @@ -import express from 'express' -import { body } from 'express-validator' -import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' -import { toIntOrNull } from '../../../helpers/custom-validators/misc' -import { logger } from '../../../helpers/logger' -import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' - -const videoWatchingValidator = [ - isValidVideoIdParam('videoId'), - - body('currentTime') - .customSanitizer(toIntOrNull) - .isInt().withMessage('Should have correct current time'), - - async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videoWatching parameters', { parameters: req.body }) - - if (areValidationErrors(req, res)) return - if (!await doesVideoExist(req.params.videoId, res, 'id')) return - - const user = res.locals.oauth.token.User - if (user.videosHistoryEnabled === false) { - logger.warn('Cannot set videos to watch by user %d: videos history is disabled.', user.id) - return res.fail({ - status: HttpStatusCode.CONFLICT_409, - message: 'Video history is disabled' - }) - } - - return next() - } -] - -// --------------------------------------------------------------------------- - -export { - videoWatchingValidator -} -- cgit v1.2.3