aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/views/video-views-manager.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/views/video-views-manager.ts')
-rw-r--r--server/lib/views/video-views-manager.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/server/lib/views/video-views-manager.ts b/server/lib/views/video-views-manager.ts
new file mode 100644
index 000000000..e07af1ca9
--- /dev/null
+++ b/server/lib/views/video-views-manager.ts
@@ -0,0 +1,70 @@
1import { logger, loggerTagsFactory } from '@server/helpers/logger'
2import { MVideo } from '@server/types/models'
3import { VideoViewEvent } from '@shared/models'
4import { VideoViewers, VideoViews } from './shared'
5
6const lTags = loggerTagsFactory('views')
7
8export class VideoViewsManager {
9
10 private static instance: VideoViewsManager
11
12 private videoViewers: VideoViewers
13 private videoViews: VideoViews
14
15 private constructor () {
16 }
17
18 init () {
19 this.videoViewers = new VideoViewers()
20 this.videoViews = new VideoViews()
21 }
22
23 async processLocalView (options: {
24 video: MVideo
25 currentTime: number
26 ip: string | null
27 viewEvent?: VideoViewEvent
28 }) {
29 const { video, ip, viewEvent, currentTime } = options
30
31 logger.debug('Processing local view for %s and ip %s.', video.url, ip, lTags())
32
33 const successViewer = await this.videoViewers.addLocalViewer({ video, ip, viewEvent, currentTime })
34
35 // Do it after added local viewer to fetch updated information
36 const watchTime = await this.videoViewers.getWatchTime(video.id, ip)
37
38 const successView = await this.videoViews.addLocalView({ video, watchTime, ip })
39
40 return { successView, successViewer }
41 }
42
43 async processRemoteView (options: {
44 video: MVideo
45 viewerExpires?: Date
46 }) {
47 const { video, viewerExpires } = options
48
49 logger.debug('Processing remote view for %s.', video.url, { viewerExpires, ...lTags() })
50
51 if (viewerExpires) await this.videoViewers.addRemoteViewer({ video, viewerExpires })
52 else await this.videoViews.addRemoteView({ video })
53 }
54
55 getViewers (video: MVideo) {
56 return this.videoViewers.getViewers(video)
57 }
58
59 buildViewerExpireTime () {
60 return this.videoViewers.buildViewerExpireTime()
61 }
62
63 processViewers () {
64 return this.videoViewers.processViewerStats()
65 }
66
67 static get Instance () {
68 return this.instance || (this.instance = new this())
69 }
70}