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'
7 import { VideoModel } from '../../../models/video/video'
8 import { getActorsInvolvedInVideo } from '../audience'
10 async function forwardVideoRelatedActivity (
13 followersException: ActorModel[] = [],
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)
20 return forwardActivity(activity, t, followersException, additionalFollowerUrls)
23 async function forwardActivity (
26 followersException: ActorModel[] = [],
27 additionalFollowerUrls: string[] = []
29 const to = activity.to || []
30 const cc = activity.cc || []
32 const followersUrls = additionalFollowerUrls
33 for (const dest of to.concat(cc)) {
34 if (dest.endsWith('/followers')) {
35 followersUrls.push(dest)
39 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
40 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
42 if (uris.length === 0) {
43 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
47 logger.debug('Creating forwarding job.', { uris })
53 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
56 async function broadcastToFollowers (
59 toActorFollowers: ActorModel[],
61 actorsException: ActorModel[] = []
63 const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
64 return broadcastTo(uris, data, byActor)
67 async function broadcastToActors (
70 toActors: ActorModel[],
71 actorsException: ActorModel[] = []
73 const uris = await computeUris(toActors, actorsException)
74 return broadcastTo(uris, data, byActor)
77 async function broadcastTo (uris: string[], data: any, byActor: ActorModel) {
78 if (uris.length === 0) return undefined
80 logger.debug('Creating broadcast job.', { uris })
84 signatureActorId: byActor.id,
88 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
91 async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) {
92 logger.debug('Creating unicast job.', { uri: toActorUrl })
96 signatureActorId: byActor.id,
100 return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
103 // ---------------------------------------------------------------------------
106 broadcastToFollowers,
110 forwardVideoRelatedActivity
113 // ---------------------------------------------------------------------------
115 async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
116 const toActorFollowerIds = toActorFollower.map(a => a.id)
118 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
119 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
120 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
123 async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
124 const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl))
126 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
127 return Array.from(toActorSharedInboxesSet)
128 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)