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'
9 import { getServerActor } from '../../../helpers/utils'
11 async function forwardVideoRelatedActivity (
14 followersException: ActorModel[] = [],
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)
21 return forwardActivity(activity, t, followersException, additionalFollowerUrls)
24 async function forwardActivity (
27 followersException: ActorModel[] = [],
28 additionalFollowerUrls: string[] = []
30 logger.info('Forwarding activity %s.', activity.id)
32 const to = activity.to || []
33 const cc = activity.cc || []
35 const followersUrls = additionalFollowerUrls
36 for (const dest of to.concat(cc)) {
37 if (dest.endsWith('/followers')) {
38 followersUrls.push(dest)
42 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
43 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
45 if (uris.length === 0) {
46 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
50 logger.debug('Creating forwarding job.', { uris })
56 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
59 async function broadcastToFollowers (
62 toActorFollowers: ActorModel[],
64 actorsException: ActorModel[] = []
66 const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
67 return broadcastTo(uris, data, byActor)
70 async function broadcastToActors (
73 toActors: ActorModel[],
74 actorsException: ActorModel[] = []
76 const uris = await computeUris(toActors, actorsException)
77 return broadcastTo(uris, data, byActor)
80 async function broadcastTo (uris: string[], data: any, byActor: ActorModel) {
81 if (uris.length === 0) return undefined
83 logger.debug('Creating broadcast job.', { uris })
87 signatureActorId: byActor.id,
91 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
94 async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) {
95 logger.debug('Creating unicast job.', { uri: toActorUrl })
99 signatureActorId: byActor.id,
103 return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
106 // ---------------------------------------------------------------------------
109 broadcastToFollowers,
113 forwardVideoRelatedActivity
116 // ---------------------------------------------------------------------------
118 async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
119 const toActorFollowerIds = toActorFollower.map(a => a.id)
121 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
122 const sharedInboxesException = await buildSharedInboxesException(actorsException)
124 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
127 async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
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)
133 const toActorSharedInboxesSet = new Set(targetUrls)
135 const sharedInboxesException = await buildSharedInboxesException(actorsException)
136 return Array.from(toActorSharedInboxesSet)
137 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
140 async function buildSharedInboxesException (actorsException: ActorModel[]) {
141 const serverActor = await getServerActor()
143 return actorsException
144 .map(f => f.sharedInboxUrl || f.inboxUrl)
145 .concat([ serverActor.sharedInboxUrl ])