diff options
author | Chocobozzz <me@florianbigard.com> | 2022-03-24 13:36:47 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2022-04-15 09:49:35 +0200 |
commit | b211106695bb82f6c32e53306081b5262c3d109d (patch) | |
tree | fa187de1c33b0956665f5362e29af6b0f6d8bb57 /server/lib/activitypub/send/send-view.ts | |
parent | 69d48ee30c9d47cddf0c3c047dc99a99dcb6e894 (diff) | |
download | PeerTube-b211106695bb82f6c32e53306081b5262c3d109d.tar.gz PeerTube-b211106695bb82f6c32e53306081b5262c3d109d.tar.zst PeerTube-b211106695bb82f6c32e53306081b5262c3d109d.zip |
Support video views/viewers stats in server
* Add "currentTime" and "event" body params to view endpoint
* Merge watching and view endpoints
* Introduce WatchAction AP activity
* Add tables to store viewer information of local videos
* Add endpoints to fetch video views/viewers stats of local videos
* Refactor views/viewers handlers
* Support "views" and "viewers" counters for both VOD and live videos
Diffstat (limited to 'server/lib/activitypub/send/send-view.ts')
-rw-r--r-- | server/lib/activitypub/send/send-view.ts | 51 |
1 files changed, 35 insertions, 16 deletions
diff --git a/server/lib/activitypub/send/send-view.ts b/server/lib/activitypub/send/send-view.ts index 1f97307b9..1088bf258 100644 --- a/server/lib/activitypub/send/send-view.ts +++ b/server/lib/activitypub/send/send-view.ts | |||
@@ -1,27 +1,49 @@ | |||
1 | import { Transaction } from 'sequelize' | 1 | import { Transaction } from 'sequelize' |
2 | import { VideoViews } from '@server/lib/video-views' | 2 | import { VideoViewsManager } from '@server/lib/views/video-views-manager' |
3 | import { MActorAudience, MVideoImmutable, MVideoUrl } from '@server/types/models' | 3 | import { MActorAudience, MActorLight, MVideoImmutable, MVideoUrl } from '@server/types/models' |
4 | import { ActivityAudience, ActivityView } from '@shared/models' | 4 | import { ActivityAudience, ActivityView } from '@shared/models' |
5 | import { logger } from '../../../helpers/logger' | 5 | import { logger } from '../../../helpers/logger' |
6 | import { ActorModel } from '../../../models/actor/actor' | ||
7 | import { audiencify, getAudience } from '../audience' | 6 | import { audiencify, getAudience } from '../audience' |
8 | import { getLocalVideoViewActivityPubUrl } from '../url' | 7 | import { getLocalVideoViewActivityPubUrl } from '../url' |
9 | import { sendVideoRelatedActivity } from './shared/send-utils' | 8 | import { sendVideoRelatedActivity } from './shared/send-utils' |
10 | 9 | ||
11 | async function sendView (byActor: ActorModel, video: MVideoImmutable, t: Transaction) { | 10 | type ViewType = 'view' | 'viewer' |
12 | logger.info('Creating job to send view of %s.', video.url) | 11 | |
12 | async function sendView (options: { | ||
13 | byActor: MActorLight | ||
14 | type: ViewType | ||
15 | video: MVideoImmutable | ||
16 | transaction?: Transaction | ||
17 | }) { | ||
18 | const { byActor, type, video, transaction } = options | ||
19 | |||
20 | logger.info('Creating job to send %s of %s.', type, video.url) | ||
13 | 21 | ||
14 | const activityBuilder = (audience: ActivityAudience) => { | 22 | const activityBuilder = (audience: ActivityAudience) => { |
15 | const url = getLocalVideoViewActivityPubUrl(byActor, video) | 23 | const url = getLocalVideoViewActivityPubUrl(byActor, video) |
16 | 24 | ||
17 | return buildViewActivity(url, byActor, video, audience) | 25 | return buildViewActivity({ url, byActor, video, audience, type }) |
18 | } | 26 | } |
19 | 27 | ||
20 | return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction: t, contextType: 'View' }) | 28 | return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction, contextType: 'View' }) |
21 | } | 29 | } |
22 | 30 | ||
23 | function buildViewActivity (url: string, byActor: MActorAudience, video: MVideoUrl, audience?: ActivityAudience): ActivityView { | 31 | // --------------------------------------------------------------------------- |
24 | if (!audience) audience = getAudience(byActor) | 32 | |
33 | export { | ||
34 | sendView | ||
35 | } | ||
36 | |||
37 | // --------------------------------------------------------------------------- | ||
38 | |||
39 | function buildViewActivity (options: { | ||
40 | url: string | ||
41 | byActor: MActorAudience | ||
42 | video: MVideoUrl | ||
43 | type: ViewType | ||
44 | audience?: ActivityAudience | ||
45 | }): ActivityView { | ||
46 | const { url, byActor, type, video, audience = getAudience(byActor) } = options | ||
25 | 47 | ||
26 | return audiencify( | 48 | return audiencify( |
27 | { | 49 | { |
@@ -29,14 +51,11 @@ function buildViewActivity (url: string, byActor: MActorAudience, video: MVideoU | |||
29 | type: 'View' as 'View', | 51 | type: 'View' as 'View', |
30 | actor: byActor.url, | 52 | actor: byActor.url, |
31 | object: video.url, | 53 | object: video.url, |
32 | expires: new Date(VideoViews.Instance.buildViewerExpireTime()).toISOString() | 54 | |
55 | expires: type === 'viewer' | ||
56 | ? new Date(VideoViewsManager.Instance.buildViewerExpireTime()).toISOString() | ||
57 | : undefined | ||
33 | }, | 58 | }, |
34 | audience | 59 | audience |
35 | ) | 60 | ) |
36 | } | 61 | } |
37 | |||
38 | // --------------------------------------------------------------------------- | ||
39 | |||
40 | export { | ||
41 | sendView | ||
42 | } | ||