]>
Commit | Line | Data |
---|---|---|
b2111066 C |
1 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
2 | import { MVideo } from '@server/types/models' | |
3 | import { VideoViewEvent } from '@shared/models' | |
4 | import { VideoViewers, VideoViews } from './shared' | |
5 | ||
6 | const lTags = loggerTagsFactory('views') | |
7 | ||
8 | export 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 | } |