1 import { Transaction } from 'sequelize'
2 import { Activity } from '../../../../shared/models/activitypub'
3 import { logger } from '../../../helpers/logger'
4 import { ActorModel } from '../../../models/activitypub/actor'
5 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
6 import { JobQueue } from '../../job-queue'
8 async function forwardActivity (
11 followersException: ActorModel[] = [],
12 additionalFollowerUrls: string[] = []
14 const to = activity.to || []
15 const cc = activity.cc || []
17 const followersUrls = additionalFollowerUrls
18 for (const dest of to.concat(cc)) {
19 if (dest.endsWith('/followers')) {
20 followersUrls.push(dest)
24 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
25 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
27 if (uris.length === 0) {
28 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
32 logger.debug('Creating forwarding job.', { uris })
38 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
41 async function broadcastToFollowers (
44 toActorFollowers: ActorModel[],
46 actorsException: ActorModel[] = []
48 const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
49 return broadcastTo(uris, data, byActor)
52 async function broadcastToActors (
55 toActors: ActorModel[],
56 actorsException: ActorModel[] = []
58 const uris = await computeUris(toActors, actorsException)
59 return broadcastTo(uris, data, byActor)
62 async function broadcastTo (uris: string[], data: any, byActor: ActorModel) {
63 if (uris.length === 0) return undefined
65 logger.debug('Creating broadcast job.', { uris })
69 signatureActorId: byActor.id,
73 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
76 async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) {
77 logger.debug('Creating unicast job.', { uri: toActorUrl })
81 signatureActorId: byActor.id,
85 return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
88 // ---------------------------------------------------------------------------
97 // ---------------------------------------------------------------------------
99 async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
100 const toActorFollowerIds = toActorFollower.map(a => a.id)
102 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
103 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
104 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
107 async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
108 const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl))
110 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
111 return Array.from(toActorSharedInboxesSet)
112 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)