diff options
author | Chocobozzz <me@florianbigard.com> | 2022-03-24 13:36:47 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2022-04-15 09:49:35 +0200 |
commit | b211106695bb82f6c32e53306081b5262c3d109d (patch) | |
tree | fa187de1c33b0956665f5362e29af6b0f6d8bb57 /server/controllers/api/videos/index.ts | |
parent | 69d48ee30c9d47cddf0c3c047dc99a99dcb6e894 (diff) | |
download | PeerTube-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/index.ts')
-rw-r--r-- | server/controllers/api/videos/index.ts | 31 |
1 files changed, 5 insertions, 26 deletions
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index c7617093c..be233722c 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts | |||
@@ -1,7 +1,6 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { pickCommonVideoQuery } from '@server/helpers/query' | 2 | import { pickCommonVideoQuery } from '@server/helpers/query' |
3 | import { doJSONRequest } from '@server/helpers/requests' | 3 | import { doJSONRequest } from '@server/helpers/requests' |
4 | import { VideoViews } from '@server/lib/video-views' | ||
5 | import { openapiOperationDoc } from '@server/middlewares/doc' | 4 | import { openapiOperationDoc } from '@server/middlewares/doc' |
6 | import { getServerActor } from '@server/models/application/application' | 5 | import { getServerActor } from '@server/models/application/application' |
7 | import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils' | 6 | import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils' |
@@ -13,7 +12,6 @@ import { logger } from '../../../helpers/logger' | |||
13 | import { getFormattedObjects } from '../../../helpers/utils' | 12 | import { getFormattedObjects } from '../../../helpers/utils' |
14 | import { REMOTE_SCHEME, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers/constants' | 13 | import { REMOTE_SCHEME, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers/constants' |
15 | import { sequelizeTypescript } from '../../../initializers/database' | 14 | import { sequelizeTypescript } from '../../../initializers/database' |
16 | import { sendView } from '../../../lib/activitypub/send/send-view' | ||
17 | import { JobQueue } from '../../../lib/job-queue' | 15 | import { JobQueue } from '../../../lib/job-queue' |
18 | import { Hooks } from '../../../lib/plugins/hooks' | 16 | import { Hooks } from '../../../lib/plugins/hooks' |
19 | import { | 17 | import { |
@@ -35,28 +33,30 @@ import { VideoModel } from '../../../models/video/video' | |||
35 | import { blacklistRouter } from './blacklist' | 33 | import { blacklistRouter } from './blacklist' |
36 | import { videoCaptionsRouter } from './captions' | 34 | import { videoCaptionsRouter } from './captions' |
37 | import { videoCommentRouter } from './comment' | 35 | import { videoCommentRouter } from './comment' |
38 | import { studioRouter } from './studio' | ||
39 | import { filesRouter } from './files' | 36 | import { filesRouter } from './files' |
40 | import { videoImportsRouter } from './import' | 37 | import { videoImportsRouter } from './import' |
41 | import { liveRouter } from './live' | 38 | import { liveRouter } from './live' |
42 | import { ownershipVideoRouter } from './ownership' | 39 | import { ownershipVideoRouter } from './ownership' |
43 | import { rateVideoRouter } from './rate' | 40 | import { rateVideoRouter } from './rate' |
41 | import { statsRouter } from './stats' | ||
42 | import { studioRouter } from './studio' | ||
44 | import { transcodingRouter } from './transcoding' | 43 | import { transcodingRouter } from './transcoding' |
45 | import { updateRouter } from './update' | 44 | import { updateRouter } from './update' |
46 | import { uploadRouter } from './upload' | 45 | import { uploadRouter } from './upload' |
47 | import { watchingRouter } from './watching' | 46 | import { viewRouter } from './view' |
48 | 47 | ||
49 | const auditLogger = auditLoggerFactory('videos') | 48 | const auditLogger = auditLoggerFactory('videos') |
50 | const videosRouter = express.Router() | 49 | const videosRouter = express.Router() |
51 | 50 | ||
52 | videosRouter.use('/', blacklistRouter) | 51 | videosRouter.use('/', blacklistRouter) |
52 | videosRouter.use('/', statsRouter) | ||
53 | videosRouter.use('/', rateVideoRouter) | 53 | videosRouter.use('/', rateVideoRouter) |
54 | videosRouter.use('/', videoCommentRouter) | 54 | videosRouter.use('/', videoCommentRouter) |
55 | videosRouter.use('/', studioRouter) | 55 | videosRouter.use('/', studioRouter) |
56 | videosRouter.use('/', videoCaptionsRouter) | 56 | videosRouter.use('/', videoCaptionsRouter) |
57 | videosRouter.use('/', videoImportsRouter) | 57 | videosRouter.use('/', videoImportsRouter) |
58 | videosRouter.use('/', ownershipVideoRouter) | 58 | videosRouter.use('/', ownershipVideoRouter) |
59 | videosRouter.use('/', watchingRouter) | 59 | videosRouter.use('/', viewRouter) |
60 | videosRouter.use('/', liveRouter) | 60 | videosRouter.use('/', liveRouter) |
61 | videosRouter.use('/', uploadRouter) | 61 | videosRouter.use('/', uploadRouter) |
62 | videosRouter.use('/', updateRouter) | 62 | videosRouter.use('/', updateRouter) |
@@ -103,11 +103,6 @@ videosRouter.get('/:id', | |||
103 | asyncMiddleware(checkVideoFollowConstraints), | 103 | asyncMiddleware(checkVideoFollowConstraints), |
104 | getVideo | 104 | getVideo |
105 | ) | 105 | ) |
106 | videosRouter.post('/:id/views', | ||
107 | openapiOperationDoc({ operationId: 'addView' }), | ||
108 | asyncMiddleware(videosCustomGetValidator('only-video')), | ||
109 | asyncMiddleware(viewVideo) | ||
110 | ) | ||
111 | 106 | ||
112 | videosRouter.delete('/:id', | 107 | videosRouter.delete('/:id', |
113 | openapiOperationDoc({ operationId: 'delVideo' }), | 108 | openapiOperationDoc({ operationId: 'delVideo' }), |
@@ -150,22 +145,6 @@ function getVideo (_req: express.Request, res: express.Response) { | |||
150 | return res.json(video.toFormattedDetailsJSON()) | 145 | return res.json(video.toFormattedDetailsJSON()) |
151 | } | 146 | } |
152 | 147 | ||
153 | async function viewVideo (req: express.Request, res: express.Response) { | ||
154 | const video = res.locals.onlyVideo | ||
155 | |||
156 | const ip = req.ip | ||
157 | const success = await VideoViews.Instance.processView({ video, ip }) | ||
158 | |||
159 | if (success) { | ||
160 | const serverActor = await getServerActor() | ||
161 | await sendView(serverActor, video, undefined) | ||
162 | |||
163 | Hooks.runAction('action:api.video.viewed', { video: video, ip, req, res }) | ||
164 | } | ||
165 | |||
166 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | ||
167 | } | ||
168 | |||
169 | async function getVideoDescription (req: express.Request, res: express.Response) { | 148 | async function getVideoDescription (req: express.Request, res: express.Response) { |
170 | const videoInstance = res.locals.videoAll | 149 | const videoInstance = res.locals.videoAll |
171 | 150 | ||