1 import { logger, loggerTagsFactory } from '@server/helpers/logger'
2 import { MVideo } from '@server/types/models'
3 import { Redis } from '../../redis'
5 const lTags = loggerTagsFactory('views')
7 export class VideoViews {
9 async addLocalView (options: {
14 const { video, ip, watchTime } = options
16 logger.debug('Adding local view to video %s.', video.uuid, { watchTime, ...lTags(video.uuid) })
18 if (!this.hasEnoughWatchTime(video, watchTime)) return false
20 const viewExists = await Redis.Instance.doesVideoIPViewExist(ip, video.uuid)
21 if (viewExists) return false
23 await Redis.Instance.setIPVideoView(ip, video.uuid)
25 await this.addView(video)
30 async addRemoteView (options: {
33 const { video } = options
35 logger.debug('Adding remote view to video %s.', video.uuid, { ...lTags(video.uuid) })
37 await this.addView(video)
42 private async addView (video: MVideo) {
43 const promises: Promise<any>[] = []
45 if (video.isOwned()) {
46 promises.push(Redis.Instance.addLocalVideoView(video.id))
49 promises.push(Redis.Instance.addVideoViewStats(video.id))
51 await Promise.all(promises)
54 private hasEnoughWatchTime (video: MVideo, watchTime: number) {
55 if (video.isLive || video.duration >= 30) return watchTime >= 30
57 // Check more than 50% of the video is watched
58 return video.duration / watchTime < 2