aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/view.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-24 13:36:47 +0100
committerChocobozzz <chocobozzz@cpy.re>2022-04-15 09:49:35 +0200
commitb211106695bb82f6c32e53306081b5262c3d109d (patch)
treefa187de1c33b0956665f5362e29af6b0f6d8bb57 /server/controllers/api/videos/view.ts
parent69d48ee30c9d47cddf0c3c047dc99a99dcb6e894 (diff)
downloadPeerTube-b211106695bb82f6c32e53306081b5262c3d109d.tar.gz
PeerTube-b211106695bb82f6c32e53306081b5262c3d109d.tar.zst
PeerTube-b211106695bb82f6c32e53306081b5262c3d109d.zip
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
Diffstat (limited to 'server/controllers/api/videos/view.ts')
-rw-r--r--server/controllers/api/videos/view.ts68
1 files changed, 68 insertions, 0 deletions
diff --git a/server/controllers/api/videos/view.ts b/server/controllers/api/videos/view.ts
new file mode 100644
index 000000000..e28cf371a
--- /dev/null
+++ b/server/controllers/api/videos/view.ts
@@ -0,0 +1,68 @@
1import express from 'express'
2import { sendView } from '@server/lib/activitypub/send/send-view'
3import { Hooks } from '@server/lib/plugins/hooks'
4import { VideoViewsManager } from '@server/lib/views/video-views-manager'
5import { getServerActor } from '@server/models/application/application'
6import { MVideoId } from '@server/types/models'
7import { HttpStatusCode, VideoView } from '@shared/models'
8import { asyncMiddleware, methodsValidator, openapiOperationDoc, optionalAuthenticate, videoViewValidator } from '../../../middlewares'
9import { UserVideoHistoryModel } from '../../../models/user/user-video-history'
10
11const viewRouter = express.Router()
12
13viewRouter.all(
14 [ '/:videoId/views', '/:videoId/watching' ],
15 openapiOperationDoc({ operationId: 'addView' }),
16 methodsValidator([ 'PUT', 'POST' ]),
17 optionalAuthenticate,
18 asyncMiddleware(videoViewValidator),
19 asyncMiddleware(viewVideo)
20)
21
22// ---------------------------------------------------------------------------
23
24export {
25 viewRouter
26}
27
28// ---------------------------------------------------------------------------
29
30async function viewVideo (req: express.Request, res: express.Response) {
31 const video = res.locals.onlyVideo
32
33 const body = req.body as VideoView
34
35 const ip = req.ip
36 const { successView, successViewer } = await VideoViewsManager.Instance.processLocalView({
37 video,
38 ip,
39 currentTime: body.currentTime,
40 viewEvent: body.viewEvent
41 })
42
43 if (successView) {
44 await sendView({ byActor: await getServerActor(), video, type: 'view' })
45
46 Hooks.runAction('action:api.video.viewed', { video: video, ip, req, res })
47 }
48
49 if (successViewer) {
50 await sendView({ byActor: await getServerActor(), video, type: 'viewer' })
51 }
52
53 await updateUserHistoryIfNeeded(body, video, res)
54
55 return res.status(HttpStatusCode.NO_CONTENT_204).end()
56}
57
58async function updateUserHistoryIfNeeded (body: VideoView, video: MVideoId, res: express.Response) {
59 const user = res.locals.oauth?.token.User
60 if (!user) return
61 if (user.videosHistoryEnabled !== true) return
62
63 await UserVideoHistoryModel.upsert({
64 videoId: video.id,
65 userId: user.id,
66 currentTime: body.currentTime
67 })
68}