diff options
author | Chocobozzz <me@florianbigard.com> | 2021-11-09 10:11:20 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-11-09 15:00:31 +0100 |
commit | 51353d9a035fb6b81f903a8b5f391292841649fd (patch) | |
tree | 75acb6eea5e043bf2e15a6a5a92e9a3c5967b156 /server/lib/activitypub | |
parent | 221ee1adc916684d4881d2a9c4c01954dcde986e (diff) | |
download | PeerTube-51353d9a035fb6b81f903a8b5f391292841649fd.tar.gz PeerTube-51353d9a035fb6b81f903a8b5f391292841649fd.tar.zst PeerTube-51353d9a035fb6b81f903a8b5f391292841649fd.zip |
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
Diffstat (limited to 'server/lib/activitypub')
-rw-r--r-- | server/lib/activitypub/process/process-view.ts | 32 | ||||
-rw-r--r-- | server/lib/activitypub/send/send-view.ts | 4 | ||||
-rw-r--r-- | server/lib/activitypub/videos/updater.ts | 1 |
3 files changed, 16 insertions, 21 deletions
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 @@ | |||
1 | import { getOrCreateAPVideo } from '../videos' | 1 | import { VideoViews } from '@server/lib/video-views' |
2 | import { forwardVideoRelatedActivity } from '../send/utils' | 2 | import { ActivityView } from '../../../../shared/models/activitypub' |
3 | import { Redis } from '../../redis' | ||
4 | import { ActivityCreate, ActivityView, ViewObject } from '../../../../shared/models/activitypub' | ||
5 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' | 3 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' |
6 | import { MActorSignature } from '../../../types/models' | 4 | import { MActorSignature } from '../../../types/models' |
7 | import { LiveManager } from '@server/lib/live/live-manager' | 5 | import { forwardVideoRelatedActivity } from '../send/utils' |
6 | import { getOrCreateAPVideo } from '../videos' | ||
8 | 7 | ||
9 | async function processViewActivity (options: APProcessorOptions<ActivityCreate | ActivityView>) { | 8 | async function processViewActivity (options: APProcessorOptions<ActivityView>) { |
10 | const { activity, byActor } = options | 9 | const { activity, byActor } = options |
10 | |||
11 | return processCreateView(activity, byActor) | 11 | return processCreateView(activity, byActor) |
12 | } | 12 | } |
13 | 13 | ||
@@ -19,10 +19,8 @@ export { | |||
19 | 19 | ||
20 | // --------------------------------------------------------------------------- | 20 | // --------------------------------------------------------------------------- |
21 | 21 | ||
22 | async function processCreateView (activity: ActivityView | ActivityCreate, byActor: MActorSignature) { | 22 | async function processCreateView (activity: ActivityView, byActor: MActorSignature) { |
23 | const videoObject = activity.type === 'View' | 23 | const videoObject = activity.object |
24 | ? activity.object | ||
25 | : (activity.object as ViewObject).object | ||
26 | 24 | ||
27 | const { video } = await getOrCreateAPVideo({ | 25 | const { video } = await getOrCreateAPVideo({ |
28 | videoObject, | 26 | videoObject, |
@@ -30,17 +28,13 @@ async function processCreateView (activity: ActivityView | ActivityCreate, byAct | |||
30 | allowRefresh: false | 28 | allowRefresh: false |
31 | }) | 29 | }) |
32 | 30 | ||
33 | if (!video.isLive) { | 31 | const viewerExpires = activity.expires |
34 | await Redis.Instance.addVideoView(video.id) | 32 | ? new Date(activity.expires) |
35 | } | 33 | : undefined |
36 | 34 | ||
37 | if (video.isOwned()) { | 35 | await VideoViews.Instance.processView({ video, ip: null, viewerExpires }) |
38 | // Our live manager will increment the counter and send the view to followers | ||
39 | if (video.isLive) { | ||
40 | LiveManager.Instance.addViewTo(video.id) | ||
41 | return | ||
42 | } | ||
43 | 36 | ||
37 | if (video.isOwned()) { | ||
44 | // Forward the view but don't resend the activity to the sender | 38 | // Forward the view but don't resend the activity to the sender |
45 | const exceptions = [ byActor ] | 39 | const exceptions = [ byActor ] |
46 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | 40 | 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 @@ | |||
1 | import { Transaction } from 'sequelize' | 1 | import { Transaction } from 'sequelize' |
2 | import { VideoViews } from '@server/lib/video-views' | ||
2 | import { MActorAudience, MVideoImmutable, MVideoUrl } from '@server/types/models' | 3 | import { MActorAudience, MVideoImmutable, MVideoUrl } from '@server/types/models' |
3 | import { ActivityAudience, ActivityView } from '../../../../shared/models/activitypub' | 4 | import { ActivityAudience, ActivityView } from '../../../../shared/models/activitypub' |
4 | import { logger } from '../../../helpers/logger' | 5 | import { logger } from '../../../helpers/logger' |
@@ -27,7 +28,8 @@ function buildViewActivity (url: string, byActor: MActorAudience, video: MVideoU | |||
27 | id: url, | 28 | id: url, |
28 | type: 'View' as 'View', | 29 | type: 'View' as 'View', |
29 | actor: byActor.url, | 30 | actor: byActor.url, |
30 | object: video.url | 31 | object: video.url, |
32 | expires: new Date(VideoViews.Instance.buildViewerExpireTime()).toISOString() | ||
31 | }, | 33 | }, |
32 | audience | 34 | audience |
33 | ) | 35 | ) |
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 { | |||
81 | 81 | ||
82 | if (videoUpdated.isLive) { | 82 | if (videoUpdated.isLive) { |
83 | PeerTubeSocket.Instance.sendVideoLiveNewState(videoUpdated) | 83 | PeerTubeSocket.Instance.sendVideoLiveNewState(videoUpdated) |
84 | PeerTubeSocket.Instance.sendVideoViewsUpdate(videoUpdated) | ||
85 | } | 84 | } |
86 | 85 | ||
87 | logger.info('Remote video with uuid %s updated', this.videoObject.uuid, this.lTags()) | 86 | logger.info('Remote video with uuid %s updated', this.videoObject.uuid, this.lTags()) |