2024-04-04 04:30:30 -05:00
|
|
|
import { buildUUID } from '@peertube/peertube-node-utils'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
|
2024-04-04 04:30:30 -05:00
|
|
|
import { VIEW_LIFETIME } from '@server/initializers/constants.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { sendView } from '@server/lib/activitypub/send/send-view.js'
|
|
|
|
import { getCachedVideoDuration } from '@server/lib/video.js'
|
|
|
|
import { getServerActor } from '@server/models/application/application.js'
|
|
|
|
import { MVideo, MVideoImmutable } from '@server/types/models/index.js'
|
2023-10-26 07:08:19 -05:00
|
|
|
import { LRUCache } from 'lru-cache'
|
2024-04-04 04:30:30 -05:00
|
|
|
import { Redis } from '../../redis.js'
|
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
2022-03-24 07:36:47 -05:00
|
|
|
|
|
|
|
const lTags = loggerTagsFactory('views')
|
|
|
|
|
|
|
|
export class VideoViews {
|
|
|
|
|
2023-10-26 07:08:19 -05:00
|
|
|
private readonly viewsCache = new LRUCache<string, boolean>({
|
|
|
|
max: 10_000,
|
|
|
|
ttl: VIEW_LIFETIME.VIEW
|
|
|
|
})
|
|
|
|
|
2022-03-24 07:36:47 -05:00
|
|
|
async addLocalView (options: {
|
2022-06-17 07:34:37 -05:00
|
|
|
video: MVideoImmutable
|
2024-04-04 04:30:30 -05:00
|
|
|
sessionId: string
|
2022-03-24 07:36:47 -05:00
|
|
|
watchTime: number
|
|
|
|
}) {
|
2024-04-04 04:30:30 -05:00
|
|
|
const { video, sessionId, watchTime } = options
|
2022-03-24 07:36:47 -05:00
|
|
|
|
|
|
|
logger.debug('Adding local view to video %s.', video.uuid, { watchTime, ...lTags(video.uuid) })
|
|
|
|
|
2022-06-17 07:34:37 -05:00
|
|
|
if (!await this.hasEnoughWatchTime(video, watchTime)) return false
|
2022-03-24 07:36:47 -05:00
|
|
|
|
2024-04-04 04:30:30 -05:00
|
|
|
const viewExists = await this.doesVideoSessionIdViewExist(sessionId, video.uuid)
|
2022-03-24 07:36:47 -05:00
|
|
|
if (viewExists) return false
|
|
|
|
|
2024-04-04 04:30:30 -05:00
|
|
|
await this.setSessionIdVideoView(sessionId, video.uuid)
|
2022-03-24 07:36:47 -05:00
|
|
|
|
|
|
|
await this.addView(video)
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
await sendView({ byActor: await getServerActor(), video, viewerIdentifier: buildUUID() })
|
2022-04-06 01:50:43 -05:00
|
|
|
|
2022-03-24 07:36:47 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
async addRemoteView (options: {
|
|
|
|
video: MVideo
|
|
|
|
}) {
|
|
|
|
const { video } = options
|
|
|
|
|
|
|
|
logger.debug('Adding remote view to video %s.', video.uuid, { ...lTags(video.uuid) })
|
|
|
|
|
|
|
|
await this.addView(video)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-04-06 01:50:43 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-06-17 07:34:37 -05:00
|
|
|
private async addView (video: MVideoImmutable) {
|
2022-03-24 07:36:47 -05:00
|
|
|
const promises: Promise<any>[] = []
|
|
|
|
|
|
|
|
if (video.isOwned()) {
|
|
|
|
promises.push(Redis.Instance.addLocalVideoView(video.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
promises.push(Redis.Instance.addVideoViewStats(video.id))
|
|
|
|
|
|
|
|
await Promise.all(promises)
|
|
|
|
}
|
|
|
|
|
2022-06-17 07:34:37 -05:00
|
|
|
private async hasEnoughWatchTime (video: MVideoImmutable, watchTime: number) {
|
|
|
|
const { duration, isLive } = await getCachedVideoDuration(video.id)
|
|
|
|
|
2024-04-04 04:30:30 -05:00
|
|
|
const countViewAfterSeconds = CONFIG.VIEWS.VIDEOS.COUNT_VIEW_AFTER / 1000 // Config is in ms
|
|
|
|
if (isLive || duration >= countViewAfterSeconds) return watchTime >= countViewAfterSeconds
|
2022-03-24 07:36:47 -05:00
|
|
|
|
|
|
|
// Check more than 50% of the video is watched
|
2022-06-17 07:34:37 -05:00
|
|
|
return duration / watchTime < 2
|
2022-03-24 07:36:47 -05:00
|
|
|
}
|
2023-10-26 07:08:19 -05:00
|
|
|
|
2024-04-04 04:30:30 -05:00
|
|
|
private doesVideoSessionIdViewExist (sessionId: string, videoUUID: string) {
|
|
|
|
const key = Redis.Instance.generateSessionIdViewKey(sessionId, videoUUID)
|
2023-10-26 07:08:19 -05:00
|
|
|
const value = this.viewsCache.has(key)
|
|
|
|
if (value === true) return Promise.resolve(true)
|
|
|
|
|
2024-04-04 04:30:30 -05:00
|
|
|
return Redis.Instance.doesVideoSessionIdViewExist(sessionId, videoUUID)
|
2023-10-26 07:08:19 -05:00
|
|
|
}
|
|
|
|
|
2024-04-04 04:30:30 -05:00
|
|
|
private setSessionIdVideoView (sessionId: string, videoUUID: string) {
|
|
|
|
const key = Redis.Instance.generateSessionIdViewKey(sessionId, videoUUID)
|
2023-10-26 07:08:19 -05:00
|
|
|
this.viewsCache.set(key, true)
|
|
|
|
|
2024-04-04 04:30:30 -05:00
|
|
|
return Redis.Instance.setSessionIdVideoView(sessionId, videoUUID)
|
2023-10-26 07:08:19 -05:00
|
|
|
}
|
2022-03-24 07:36:47 -05:00
|
|
|
}
|