]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-view.ts
Improve views/viewers documentation
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-view.ts
CommitLineData
1e7eb25f 1import { Transaction } from 'sequelize'
b2111066
C
2import { VideoViewsManager } from '@server/lib/views/video-views-manager'
3import { MActorAudience, MActorLight, MVideoImmutable, MVideoUrl } from '@server/types/models'
57e4e1c1 4import { ActivityAudience, ActivityView } from '@shared/models'
de94ac86 5import { logger } from '../../../helpers/logger'
1e7eb25f 6import { audiencify, getAudience } from '../audience'
de94ac86 7import { getLocalVideoViewActivityPubUrl } from '../url'
57e4e1c1 8import { sendVideoRelatedActivity } from './shared/send-utils'
1e7eb25f 9
b2111066
C
10type ViewType = 'view' | 'viewer'
11
12async 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)
1e7eb25f
C
21
22 const activityBuilder = (audience: ActivityAudience) => {
de94ac86 23 const url = getLocalVideoViewActivityPubUrl(byActor, video)
1e7eb25f 24
b2111066 25 return buildViewActivity({ url, byActor, video, audience, type })
1e7eb25f
C
26 }
27
b2111066 28 return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction, contextType: 'View' })
1e7eb25f
C
29}
30
b2111066
C
31// ---------------------------------------------------------------------------
32
33export {
34 sendView
35}
36
37// ---------------------------------------------------------------------------
38
39function 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
1e7eb25f
C
47
48 return audiencify(
49 {
50 id: url,
51 type: 'View' as 'View',
52 actor: byActor.url,
51353d9a 53 object: video.url,
b2111066
C
54
55 expires: type === 'viewer'
56 ? new Date(VideoViewsManager.Instance.buildViewerExpireTime()).toISOString()
57 : undefined
1e7eb25f
C
58 },
59 audience
60 )
61}