]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/utils.ts
Add more info logging
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / utils.ts
CommitLineData
54141398 1import { Transaction } from 'sequelize'
e251f170 2import { Activity } from '../../../../shared/models/activitypub'
da854ddd 3import { logger } from '../../../helpers/logger'
50d6de9c
C
4import { ActorModel } from '../../../models/activitypub/actor'
5import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
94a5ff8a 6import { JobQueue } from '../../job-queue'
9588d4f4
C
7import { VideoModel } from '../../../models/video/video'
8import { getActorsInvolvedInVideo } from '../audience'
9
10async function forwardVideoRelatedActivity (
11 activity: Activity,
12 t: Transaction,
13 followersException: ActorModel[] = [],
14 video: VideoModel
15) {
16 // Mastodon does not add our announces in audience, so we forward to them manually
17 const additionalActors = await getActorsInvolvedInVideo(video, t)
18 const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
19
20 return forwardActivity(activity, t, followersException, additionalFollowerUrls)
21}
63c93323
C
22
23async function forwardActivity (
24 activity: Activity,
25 t: Transaction,
93ef8a9d
C
26 followersException: ActorModel[] = [],
27 additionalFollowerUrls: string[] = []
63c93323 28) {
8e0fd45e
C
29 logger.info('Forwarding activity %s.', activity.id)
30
63c93323
C
31 const to = activity.to || []
32 const cc = activity.cc || []
33
93ef8a9d 34 const followersUrls = additionalFollowerUrls
63c93323
C
35 for (const dest of to.concat(cc)) {
36 if (dest.endsWith('/followers')) {
37 followersUrls.push(dest)
38 }
39 }
40
50d6de9c
C
41 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
42 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
63c93323
C
43
44 if (uris.length === 0) {
50d6de9c 45 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
df1966c9 46 return undefined
63c93323
C
47 }
48
49 logger.debug('Creating forwarding job.', { uris })
50
94a5ff8a 51 const payload = {
63c93323
C
52 uris,
53 body: activity
54 }
94a5ff8a 55 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
63c93323 56}
54141398 57
40ff5707
C
58async function broadcastToFollowers (
59 data: any,
50d6de9c
C
60 byActor: ActorModel,
61 toActorFollowers: ActorModel[],
40ff5707 62 t: Transaction,
93ef8a9d 63 actorsException: ActorModel[] = []
40ff5707 64) {
93ef8a9d 65 const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
94a5ff8a 66 return broadcastTo(uris, data, byActor)
93ef8a9d
C
67}
68
69async function broadcastToActors (
70 data: any,
71 byActor: ActorModel,
72 toActors: ActorModel[],
93ef8a9d
C
73 actorsException: ActorModel[] = []
74) {
75 const uris = await computeUris(toActors, actorsException)
94a5ff8a 76 return broadcastTo(uris, data, byActor)
93ef8a9d
C
77}
78
94a5ff8a 79async function broadcastTo (uris: string[], data: any, byActor: ActorModel) {
93ef8a9d 80 if (uris.length === 0) return undefined
54141398 81
63c93323 82 logger.debug('Creating broadcast job.', { uris })
40ff5707 83
94a5ff8a 84 const payload = {
40ff5707 85 uris,
50d6de9c 86 signatureActorId: byActor.id,
54141398
C
87 body: data
88 }
89
94a5ff8a 90 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
54141398
C
91}
92
94a5ff8a 93async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) {
50d6de9c 94 logger.debug('Creating unicast job.', { uri: toActorUrl })
63c93323 95
94a5ff8a
C
96 const payload = {
97 uri: toActorUrl,
50d6de9c 98 signatureActorId: byActor.id,
54141398
C
99 body: data
100 }
101
94a5ff8a 102 return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
54141398
C
103}
104
e251f170 105// ---------------------------------------------------------------------------
54141398 106
e251f170
C
107export {
108 broadcastToFollowers,
109 unicastTo,
110 forwardActivity,
9588d4f4
C
111 broadcastToActors,
112 forwardVideoRelatedActivity
54141398
C
113}
114
e251f170 115// ---------------------------------------------------------------------------
e12a0092 116
93ef8a9d 117async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
50d6de9c 118 const toActorFollowerIds = toActorFollower.map(a => a.id)
63c93323 119
50d6de9c 120 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
9a8cbd82 121 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
93ef8a9d
C
122 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
123}
124
125async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
9a8cbd82 126 const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl))
93ef8a9d 127
9a8cbd82 128 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
93ef8a9d 129 return Array.from(toActorSharedInboxesSet)
e251f170 130 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
54141398 131}