]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/utils.ts
Remove bad import
[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'
63c93323
C
7
8async function forwardActivity (
9 activity: Activity,
10 t: Transaction,
93ef8a9d
C
11 followersException: ActorModel[] = [],
12 additionalFollowerUrls: string[] = []
63c93323
C
13) {
14 const to = activity.to || []
15 const cc = activity.cc || []
16
93ef8a9d 17 const followersUrls = additionalFollowerUrls
63c93323
C
18 for (const dest of to.concat(cc)) {
19 if (dest.endsWith('/followers')) {
20 followersUrls.push(dest)
21 }
22 }
23
50d6de9c
C
24 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
25 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
63c93323
C
26
27 if (uris.length === 0) {
50d6de9c 28 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
df1966c9 29 return undefined
63c93323
C
30 }
31
32 logger.debug('Creating forwarding job.', { uris })
33
94a5ff8a 34 const payload = {
63c93323
C
35 uris,
36 body: activity
37 }
94a5ff8a 38 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
63c93323 39}
54141398 40
40ff5707
C
41async function broadcastToFollowers (
42 data: any,
50d6de9c
C
43 byActor: ActorModel,
44 toActorFollowers: ActorModel[],
40ff5707 45 t: Transaction,
93ef8a9d 46 actorsException: ActorModel[] = []
40ff5707 47) {
93ef8a9d 48 const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
94a5ff8a 49 return broadcastTo(uris, data, byActor)
93ef8a9d
C
50}
51
52async function broadcastToActors (
53 data: any,
54 byActor: ActorModel,
55 toActors: ActorModel[],
93ef8a9d
C
56 actorsException: ActorModel[] = []
57) {
58 const uris = await computeUris(toActors, actorsException)
94a5ff8a 59 return broadcastTo(uris, data, byActor)
93ef8a9d
C
60}
61
94a5ff8a 62async function broadcastTo (uris: string[], data: any, byActor: ActorModel) {
93ef8a9d 63 if (uris.length === 0) return undefined
54141398 64
63c93323 65 logger.debug('Creating broadcast job.', { uris })
40ff5707 66
94a5ff8a 67 const payload = {
40ff5707 68 uris,
50d6de9c 69 signatureActorId: byActor.id,
54141398
C
70 body: data
71 }
72
94a5ff8a 73 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
54141398
C
74}
75
94a5ff8a 76async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) {
50d6de9c 77 logger.debug('Creating unicast job.', { uri: toActorUrl })
63c93323 78
94a5ff8a
C
79 const payload = {
80 uri: toActorUrl,
50d6de9c 81 signatureActorId: byActor.id,
54141398
C
82 body: data
83 }
84
94a5ff8a 85 return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
54141398
C
86}
87
e251f170 88// ---------------------------------------------------------------------------
54141398 89
e251f170
C
90export {
91 broadcastToFollowers,
92 unicastTo,
93 forwardActivity,
94 broadcastToActors
54141398
C
95}
96
e251f170 97// ---------------------------------------------------------------------------
e12a0092 98
93ef8a9d 99async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
50d6de9c 100 const toActorFollowerIds = toActorFollower.map(a => a.id)
63c93323 101
50d6de9c 102 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
9a8cbd82 103 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
93ef8a9d
C
104 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
105}
106
107async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
9a8cbd82 108 const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl))
93ef8a9d 109
9a8cbd82 110 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
93ef8a9d 111 return Array.from(toActorSharedInboxesSet)
e251f170 112 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
54141398 113}