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 +++++++++++--------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'server/lib/activitypub/process/process-view.ts') 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) -- cgit v1.2.3