mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-03 09:49:20 +02:00

* add user agent video stats closes #6832 * Disable indexes, support start/end dates * move ua parsing to client * Openapi, inline body request, update tests --------- Co-authored-by: Chocobozzz <me@florianbigard.com>
70 lines
2 KiB
TypeScript
70 lines
2 KiB
TypeScript
import { HttpStatusCode, VideoView } from '@peertube/peertube-models'
|
|
import { Hooks } from '@server/lib/plugins/hooks.js'
|
|
import { VideoViewsManager } from '@server/lib/views/video-views-manager.js'
|
|
import { MVideoId } from '@server/types/models/index.js'
|
|
import express from 'express'
|
|
import {
|
|
asyncMiddleware,
|
|
methodsValidator,
|
|
openapiOperationDoc,
|
|
optionalAuthenticate,
|
|
videoViewValidator
|
|
} from '../../../middlewares/index.js'
|
|
import { UserVideoHistoryModel } from '../../../models/user/user-video-history.js'
|
|
|
|
const viewRouter = express.Router()
|
|
|
|
viewRouter.all(
|
|
[ '/:videoId/views', '/:videoId/watching' ],
|
|
openapiOperationDoc({ operationId: 'addView' }),
|
|
methodsValidator([ 'PUT', 'POST' ]),
|
|
optionalAuthenticate,
|
|
asyncMiddleware(videoViewValidator),
|
|
asyncMiddleware(viewVideo)
|
|
)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export {
|
|
viewRouter
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
async function viewVideo (req: express.Request, res: express.Response) {
|
|
const video = res.locals.onlyImmutableVideo
|
|
|
|
const body = req.body as VideoView
|
|
const ip = req.ip
|
|
|
|
const { successView } = await VideoViewsManager.Instance.processLocalView({
|
|
video,
|
|
ip,
|
|
currentTime: body.currentTime,
|
|
viewEvent: body.viewEvent,
|
|
sessionId: body.sessionId,
|
|
client: body.client,
|
|
operatingSystem: body.operatingSystem,
|
|
device: body.device
|
|
})
|
|
|
|
if (successView) {
|
|
Hooks.runAction('action:api.video.viewed', { video, ip, req, res })
|
|
}
|
|
|
|
await updateUserHistoryIfNeeded(body, video, res)
|
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
|
}
|
|
|
|
async function updateUserHistoryIfNeeded (body: VideoView, video: MVideoId, res: express.Response) {
|
|
const user = res.locals.oauth?.token.User
|
|
if (!user) return
|
|
if (user.videosHistoryEnabled !== true) return
|
|
|
|
await UserVideoHistoryModel.upsert({
|
|
videoId: video.id,
|
|
userId: user.id,
|
|
currentTime: body.currentTime
|
|
})
|
|
}
|