X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fviews%2Fvideo-views-manager.ts;h=86758e8d8a56b7eb616e16f7000e70eba1875c90;hb=3b0525106d8742b5ebd6962219eaf105435f6fb9;hp=e07af1ca940f39cca68a6fb4b0e43a204cd26886;hpb=b211106695bb82f6c32e53306081b5262c3d109d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/views/video-views-manager.ts b/server/lib/views/video-views-manager.ts index e07af1ca9..86758e8d8 100644 --- a/server/lib/views/video-views-manager.ts +++ b/server/lib/views/video-views-manager.ts @@ -1,7 +1,25 @@ import { logger, loggerTagsFactory } from '@server/helpers/logger' -import { MVideo } from '@server/types/models' +import { MVideo, MVideoImmutable } from '@server/types/models' import { VideoViewEvent } from '@shared/models' -import { VideoViewers, VideoViews } from './shared' +import { VideoViewerCounters, VideoViewerStats, VideoViews } 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') @@ -9,19 +27,21 @@ export class VideoViewsManager { private static instance: VideoViewsManager - private videoViewers: VideoViewers + private videoViewerStats: VideoViewerStats + private videoViewerCounters: VideoViewerCounters private videoViews: VideoViews private constructor () { } init () { - this.videoViewers = new VideoViewers() + this.videoViewerStats = new VideoViewerStats() + this.videoViewerCounters = new VideoViewerCounters() this.videoViews = new VideoViews() } async processLocalView (options: { - video: MVideo + video: MVideoImmutable currentTime: number ip: string | null viewEvent?: VideoViewEvent @@ -30,10 +50,12 @@ export class VideoViewsManager { logger.debug('Processing local view for %s and ip %s.', video.url, ip, lTags()) - const successViewer = await this.videoViewers.addLocalViewer({ video, ip, viewEvent, currentTime }) + 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.videoViewers.getWatchTime(video.id, ip) + const watchTime = await this.videoViewerStats.getWatchTime(video.id, ip) const successView = await this.videoViews.addLocalView({ video, watchTime, ip }) @@ -42,26 +64,27 @@ export class VideoViewsManager { async processRemoteView (options: { video: MVideo + viewerId: string | null viewerExpires?: Date }) { - const { video, viewerExpires } = options + const { video, viewerId, viewerExpires } = options - logger.debug('Processing remote view for %s.', video.url, { viewerExpires, ...lTags() }) + logger.debug('Processing remote view for %s.', video.url, { viewerExpires, viewerId, ...lTags() }) - if (viewerExpires) await this.videoViewers.addRemoteViewer({ video, viewerExpires }) + if (viewerExpires) await this.videoViewerCounters.addRemoteViewer({ video, viewerId, viewerExpires }) else await this.videoViews.addRemoteView({ video }) } getViewers (video: MVideo) { - return this.videoViewers.getViewers(video) + return this.videoViewerCounters.getViewers(video) } buildViewerExpireTime () { - return this.videoViewers.buildViewerExpireTime() + return this.videoViewerCounters.buildViewerExpireTime() } - processViewers () { - return this.videoViewers.processViewerStats() + processViewerStats () { + return this.videoViewerStats.processViewerStats() } static get Instance () {