From 51353d9a035fb6b81f903a8b5f391292841649fd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 9 Nov 2021 10:11:20 +0100 Subject: Refactor video views Introduce viewers attribute for live videos Count views for live videos Reduce delay to see the viewer update for lives Add ability to configure video views buffer interval and view ip expiration --- server/lib/activitypub/process/process-view.ts | 32 +++++++++++--------------- server/lib/activitypub/send/send-view.ts | 4 +++- server/lib/activitypub/videos/updater.ts | 1 - 3 files changed, 16 insertions(+), 21 deletions(-) (limited to 'server/lib/activitypub') diff --git a/server/lib/activitypub/process/process-view.ts b/server/lib/activitypub/process/process-view.ts index 5593ee257..720385f9b 100644 --- a/server/lib/activitypub/process/process-view.ts +++ b/server/lib/activitypub/process/process-view.ts @@ -1,13 +1,13 @@ -import { getOrCreateAPVideo } from '../videos' -import { forwardVideoRelatedActivity } from '../send/utils' -import { Redis } from '../../redis' -import { ActivityCreate, ActivityView, ViewObject } from '../../../../shared/models/activitypub' +import { VideoViews } from '@server/lib/video-views' +import { ActivityView } from '../../../../shared/models/activitypub' import { APProcessorOptions } from '../../../types/activitypub-processor.model' import { MActorSignature } from '../../../types/models' -import { LiveManager } from '@server/lib/live/live-manager' +import { forwardVideoRelatedActivity } from '../send/utils' +import { getOrCreateAPVideo } from '../videos' -async function processViewActivity (options: APProcessorOptions) { +async function processViewActivity (options: APProcessorOptions) { const { activity, byActor } = options + return processCreateView(activity, byActor) } @@ -19,10 +19,8 @@ export { // --------------------------------------------------------------------------- -async function processCreateView (activity: ActivityView | ActivityCreate, byActor: MActorSignature) { - const videoObject = activity.type === 'View' - ? activity.object - : (activity.object as ViewObject).object +async function processCreateView (activity: ActivityView, byActor: MActorSignature) { + const videoObject = activity.object const { video } = await getOrCreateAPVideo({ videoObject, @@ -30,17 +28,13 @@ async function processCreateView (activity: ActivityView | ActivityCreate, byAct allowRefresh: false }) - if (!video.isLive) { - await Redis.Instance.addVideoView(video.id) - } + const viewerExpires = activity.expires + ? new Date(activity.expires) + : undefined - if (video.isOwned()) { - // Our live manager will increment the counter and send the view to followers - if (video.isLive) { - LiveManager.Instance.addViewTo(video.id) - return - } + await VideoViews.Instance.processView({ video, ip: null, viewerExpires }) + if (video.isOwned()) { // Forward the view but don't resend the activity to the sender const exceptions = [ byActor ] await forwardVideoRelatedActivity(activity, undefined, exceptions, video) diff --git a/server/lib/activitypub/send/send-view.ts b/server/lib/activitypub/send/send-view.ts index 153e94295..b12583e26 100644 --- a/server/lib/activitypub/send/send-view.ts +++ b/server/lib/activitypub/send/send-view.ts @@ -1,4 +1,5 @@ import { Transaction } from 'sequelize' +import { VideoViews } from '@server/lib/video-views' import { MActorAudience, MVideoImmutable, MVideoUrl } from '@server/types/models' import { ActivityAudience, ActivityView } from '../../../../shared/models/activitypub' import { logger } from '../../../helpers/logger' @@ -27,7 +28,8 @@ function buildViewActivity (url: string, byActor: MActorAudience, video: MVideoU id: url, type: 'View' as 'View', actor: byActor.url, - object: video.url + object: video.url, + expires: new Date(VideoViews.Instance.buildViewerExpireTime()).toISOString() }, audience ) diff --git a/server/lib/activitypub/videos/updater.ts b/server/lib/activitypub/videos/updater.ts index 157569414..f786bb196 100644 --- a/server/lib/activitypub/videos/updater.ts +++ b/server/lib/activitypub/videos/updater.ts @@ -81,7 +81,6 @@ export class APVideoUpdater extends APVideoAbstractBuilder { if (videoUpdated.isLive) { PeerTubeSocket.Instance.sendVideoLiveNewState(videoUpdated) - PeerTubeSocket.Instance.sendVideoViewsUpdate(videoUpdated) } logger.info('Remote video with uuid %s updated', this.videoObject.uuid, this.lTags()) -- cgit v1.2.3