2022-03-24 13:36:47 +01:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger'
|
2022-06-17 14:34:37 +02:00
|
|
|
import { MVideo, MVideoImmutable } from '@server/types/models'
|
2022-03-24 13:36:47 +01:00
|
|
|
import { VideoViewEvent } from '@shared/models'
|
2022-07-27 16:19:25 +02:00
|
|
|
import { VideoScope, VideoViewerCounters, VideoViewerStats, VideoViews, ViewerScope } from './shared'
|
2022-03-24 13:36:47 +01:00
|
|
|
|
2022-04-05 15:15:09 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
const lTags = loggerTagsFactory('views')
|
|
|
|
|
|
|
|
export class VideoViewsManager {
|
|
|
|
|
|
|
|
private static instance: VideoViewsManager
|
|
|
|
|
2022-04-06 08:50:43 +02:00
|
|
|
private videoViewerStats: VideoViewerStats
|
|
|
|
private videoViewerCounters: VideoViewerCounters
|
2022-03-24 13:36:47 +01:00
|
|
|
private videoViews: VideoViews
|
|
|
|
|
|
|
|
private constructor () {
|
|
|
|
}
|
|
|
|
|
|
|
|
init () {
|
2022-04-06 08:50:43 +02:00
|
|
|
this.videoViewerStats = new VideoViewerStats()
|
|
|
|
this.videoViewerCounters = new VideoViewerCounters()
|
2022-03-24 13:36:47 +01:00
|
|
|
this.videoViews = new VideoViews()
|
|
|
|
}
|
|
|
|
|
|
|
|
async processLocalView (options: {
|
2022-06-17 14:34:37 +02:00
|
|
|
video: MVideoImmutable
|
2022-03-24 13:36:47 +01:00
|
|
|
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())
|
|
|
|
|
2022-04-06 08:50:43 +02:00
|
|
|
await this.videoViewerStats.addLocalViewer({ video, ip, viewEvent, currentTime })
|
|
|
|
|
|
|
|
const successViewer = await this.videoViewerCounters.addLocalViewer({ video, ip })
|
2022-03-24 13:36:47 +01:00
|
|
|
|
|
|
|
// Do it after added local viewer to fetch updated information
|
2022-04-06 08:50:43 +02:00
|
|
|
const watchTime = await this.videoViewerStats.getWatchTime(video.id, ip)
|
2022-03-24 13:36:47 +01:00
|
|
|
|
|
|
|
const successView = await this.videoViews.addLocalView({ video, watchTime, ip })
|
|
|
|
|
|
|
|
return { successView, successViewer }
|
|
|
|
}
|
|
|
|
|
|
|
|
async processRemoteView (options: {
|
|
|
|
video: MVideo
|
2022-04-06 08:50:43 +02:00
|
|
|
viewerId: string | null
|
2022-03-24 13:36:47 +01:00
|
|
|
viewerExpires?: Date
|
|
|
|
}) {
|
2022-04-06 08:50:43 +02:00
|
|
|
const { video, viewerId, viewerExpires } = options
|
2022-03-24 13:36:47 +01:00
|
|
|
|
2022-04-06 08:50:43 +02:00
|
|
|
logger.debug('Processing remote view for %s.', video.url, { viewerExpires, viewerId, ...lTags() })
|
2022-03-24 13:36:47 +01:00
|
|
|
|
2022-04-06 08:50:43 +02:00
|
|
|
if (viewerExpires) await this.videoViewerCounters.addRemoteViewer({ video, viewerId, viewerExpires })
|
2022-03-24 13:36:47 +01:00
|
|
|
else await this.videoViews.addRemoteView({ video })
|
|
|
|
}
|
|
|
|
|
|
|
|
getViewers (video: MVideo) {
|
2022-04-06 08:50:43 +02:00
|
|
|
return this.videoViewerCounters.getViewers(video)
|
2022-03-24 13:36:47 +01:00
|
|
|
}
|
|
|
|
|
2022-07-27 16:19:25 +02:00
|
|
|
getTotalViewers (options: {
|
|
|
|
viewerScope: ViewerScope
|
|
|
|
videoScope: VideoScope
|
|
|
|
}) {
|
|
|
|
return this.videoViewerCounters.getTotalViewers(options)
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
buildViewerExpireTime () {
|
2022-04-06 08:50:43 +02:00
|
|
|
return this.videoViewerCounters.buildViewerExpireTime()
|
2022-03-24 13:36:47 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 08:50:43 +02:00
|
|
|
processViewerStats () {
|
|
|
|
return this.videoViewerStats.processViewerStats()
|
2022-03-24 13:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|