X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fviews%2Fshared%2Fvideo-views.ts;h=e563287e18fc5785ff1e87a78ab4f7844739779f;hb=e722fb5923ddf11d72e48cec9788abc64327c22f;hp=19250f99360202c91bddefbdb4577bccc54274bc;hpb=b211106695bb82f6c32e53306081b5262c3d109d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/views/shared/video-views.ts b/server/lib/views/shared/video-views.ts index 19250f993..e563287e1 100644 --- a/server/lib/views/shared/video-views.ts +++ b/server/lib/views/shared/video-views.ts @@ -1,5 +1,9 @@ import { logger, loggerTagsFactory } from '@server/helpers/logger' -import { MVideo } from '@server/types/models' +import { sendView } from '@server/lib/activitypub/send/send-view' +import { getCachedVideoDuration } from '@server/lib/video' +import { getServerActor } from '@server/models/application/application' +import { MVideo, MVideoImmutable } from '@server/types/models' +import { buildUUID } from '@shared/extra-utils' import { Redis } from '../../redis' const lTags = loggerTagsFactory('views') @@ -7,7 +11,7 @@ const lTags = loggerTagsFactory('views') export class VideoViews { async addLocalView (options: { - video: MVideo + video: MVideoImmutable ip: string watchTime: number }) { @@ -15,7 +19,7 @@ export class VideoViews { logger.debug('Adding local view to video %s.', video.uuid, { watchTime, ...lTags(video.uuid) }) - if (!this.hasEnoughWatchTime(video, watchTime)) return false + if (!await this.hasEnoughWatchTime(video, watchTime)) return false const viewExists = await Redis.Instance.doesVideoIPViewExist(ip, video.uuid) if (viewExists) return false @@ -24,6 +28,8 @@ export class VideoViews { await this.addView(video) + await sendView({ byActor: await getServerActor(), video, type: 'view', viewerIdentifier: buildUUID() }) + return true } @@ -39,7 +45,9 @@ export class VideoViews { return true } - private async addView (video: MVideo) { + // --------------------------------------------------------------------------- + + private async addView (video: MVideoImmutable) { const promises: Promise[] = [] if (video.isOwned()) { @@ -51,10 +59,12 @@ export class VideoViews { await Promise.all(promises) } - private hasEnoughWatchTime (video: MVideo, watchTime: number) { - if (video.isLive || video.duration >= 30) return watchTime >= 30 + private async hasEnoughWatchTime (video: MVideoImmutable, watchTime: number) { + const { duration, isLive } = await getCachedVideoDuration(video.id) + + if (isLive || duration >= 30) return watchTime >= 30 // Check more than 50% of the video is watched - return video.duration / watchTime < 2 + return duration / watchTime < 2 } }