aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/views/video-views-manager.ts
blob: c088dad5e94bf4c62741e86248a755135a81391a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { MVideo, MVideoImmutable } from '@server/types/models'
import { VideoViewEvent } from '@shared/models'
import { VideoScope, VideoViewerCounters, VideoViewerStats, VideoViews, ViewerScope } from './shared'

/**
 * If processing a local view:
 *  - We update viewer information (segments watched, watch time etc)
 *  - We add +1 to video viewers counter if this is a new viewer
 *  - We add +1 to video views counter if this is a new view and if the user watched enough seconds
 *  - We send AP message to notify about this viewer and this view
 *  - We update last video time for the user if authenticated
 *
 * If processing a remote view:
 *  - We add +1 to video viewers counter
 *  - We add +1 to video views counter
 *
 * A viewer is a someone that watched one or multiple sections of a video
 * A viewer that watched only a few seconds of a video may not increment the video views counter
 * Viewers statistics are sent to origin instance using the `WatchAction` ActivityPub object
 *
 */

const lTags = loggerTagsFactory('views')

export class VideoViewsManager {

  private static instance: VideoViewsManager

  private videoViewerStats: VideoViewerStats
  private videoViewerCounters: VideoViewerCounters
  private videoViews: VideoViews

  private constructor () {
  }

  init () {
    this.videoViewerStats = new VideoViewerStats()
    this.videoViewerCounters = new VideoViewerCounters()
    this.videoViews = new VideoViews()
  }

  async processLocalView (options: {
    video: MVideoImmutable
    currentTime: number
    ip: string | null
    viewEvent?: VideoViewEvent
  }) {
    const { video, ip, viewEvent, currentTime } = options

    logger.debug('Processing local view for %s and ip %s.', video.url, ip, lTags())

    await this.videoViewerStats.addLocalViewer({ video, ip, viewEvent, currentTime })

    const successViewer = await this.videoViewerCounters.addLocalViewer({ video, ip })

    // Do it after added local viewer to fetch updated information
    const watchTime = await this.videoViewerStats.getWatchTime(video.id, ip)

    const successView = await this.videoViews.addLocalView({ video, watchTime, ip })

    return { successView, successViewer }
  }

  async processRemoteView (options: {
    video: MVideo
    viewerId: string | null
    viewerExpires?: Date
  }) {
    const { video, viewerId, viewerExpires } = options

    logger.debug('Processing remote view for %s.', video.url, { viewerExpires, viewerId, ...lTags() })

    if (viewerExpires) await this.videoViewerCounters.addRemoteViewer({ video, viewerId, viewerExpires })
    else await this.videoViews.addRemoteView({ video })
  }

  getViewers (video: MVideo) {
    return this.videoViewerCounters.getViewers(video)
  }

  getTotalViewers (options: {
    viewerScope: ViewerScope
    videoScope: VideoScope
  }) {
    return this.videoViewerCounters.getTotalViewers(options)
  }

  buildViewerExpireTime () {
    return this.videoViewerCounters.buildViewerExpireTime()
  }

  processViewerStats () {
    return this.videoViewerStats.processViewerStats()
  }

  static get Instance () {
    return this.instance || (this.instance = new this())
  }
}