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