1 import { Transaction } from 'sequelize'
2 import { Activity, ActivityAudience } 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, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience'
9 import { getServerActor } from '../../../helpers/utils'
11 async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
14 transaction?: Transaction
16 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(options.video, options.transaction)
19 if (options.video.isOwned() === false) {
20 const audience = getRemoteVideoAudience(options.video, actorsInvolvedInVideo)
21 const activity = activityBuilder(audience)
23 return unicastTo(activity, options.byActor, options.video.VideoChannel.Account.Actor.sharedInboxUrl)
27 const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
28 const activity = activityBuilder(audience)
30 const actorsException = [ options.byActor ]
31 return broadcastToFollowers(activity, options.byActor, actorsInvolvedInVideo, options.transaction, actorsException)
34 async function forwardVideoRelatedActivity (
37 followersException: ActorModel[] = [],
40 // Mastodon does not add our announces in audience, so we forward to them manually
41 const additionalActors = await getActorsInvolvedInVideo(video, t)
42 const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
44 return forwardActivity(activity, t, followersException, additionalFollowerUrls)
47 async function forwardActivity (
50 followersException: ActorModel[] = [],
51 additionalFollowerUrls: string[] = []
53 logger.info('Forwarding activity %s.', activity.id)
55 const to = activity.to || []
56 const cc = activity.cc || []
58 const followersUrls = additionalFollowerUrls
59 for (const dest of to.concat(cc)) {
60 if (dest.endsWith('/followers')) {
61 followersUrls.push(dest)
65 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
66 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
68 if (uris.length === 0) {
69 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
73 logger.debug('Creating forwarding job.', { uris })
79 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
82 async function broadcastToFollowers (
85 toFollowersOf: ActorModel[],
87 actorsException: ActorModel[] = []
89 const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
90 return broadcastTo(uris, data, byActor)
93 async function broadcastToActors (
96 toActors: ActorModel[],
97 actorsException: ActorModel[] = []
99 const uris = await computeUris(toActors, actorsException)
100 return broadcastTo(uris, data, byActor)
103 async function broadcastTo (uris: string[], data: any, byActor: ActorModel) {
104 if (uris.length === 0) return undefined
106 logger.debug('Creating broadcast job.', { uris })
110 signatureActorId: byActor.id,
114 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
117 async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) {
118 logger.debug('Creating unicast job.', { uri: toActorUrl })
122 signatureActorId: byActor.id,
126 return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
129 // ---------------------------------------------------------------------------
132 broadcastToFollowers,
136 forwardVideoRelatedActivity,
137 sendVideoRelatedActivity
140 // ---------------------------------------------------------------------------
142 async function computeFollowerUris (toFollowersOf: ActorModel[], actorsException: ActorModel[], t: Transaction) {
143 const toActorFollowerIds = toFollowersOf.map(a => a.id)
145 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
146 const sharedInboxesException = await buildSharedInboxesException(actorsException)
148 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
151 async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
152 const serverActor = await getServerActor()
153 const targetUrls = toActors
154 .filter(a => a.id !== serverActor.id) // Don't send to ourselves
155 .map(a => a.sharedInboxUrl || a.inboxUrl)
157 const toActorSharedInboxesSet = new Set(targetUrls)
159 const sharedInboxesException = await buildSharedInboxesException(actorsException)
160 return Array.from(toActorSharedInboxesSet)
161 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
164 async function buildSharedInboxesException (actorsException: ActorModel[]) {
165 const serverActor = await getServerActor()
167 return actorsException
168 .map(f => f.sharedInboxUrl || f.inboxUrl)
169 .concat([ serverActor.sharedInboxUrl ])