1 import { Transaction } from 'sequelize'
2 import { ActorFollowHealthCache } from '@server/lib/actor-follow-health-cache'
3 import { getServerActor } from '@server/models/application/application'
4 import { Activity, ActivityAudience } from '@shared/models'
5 import { ContextType } from '@shared/models/activitypub/context'
6 import { afterCommitIfTransaction } from '../../../../helpers/database-utils'
7 import { logger } from '../../../../helpers/logger'
8 import { ActorModel } from '../../../../models/actor/actor'
9 import { ActorFollowModel } from '../../../../models/actor/actor-follow'
10 import { MActor, MActorId, MActorLight, MActorWithInboxes, MVideoAccountLight, MVideoId, MVideoImmutable } from '../../../../types/models'
11 import { JobQueue } from '../../../job-queue'
12 import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getOriginVideoAudience } from './audience-utils'
14 async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
16 video: MVideoImmutable | MVideoAccountLight
17 transaction?: Transaction
18 contextType?: ContextType
20 const { byActor, video, transaction, contextType } = options
22 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, transaction)
25 if (video.isOwned() === false) {
26 return sendVideoActivityToOrigin(activityBuilder, options)
30 const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
31 const activity = activityBuilder(audience)
33 const actorsException = [ byActor ]
35 return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException, contextType)
38 async function sendVideoActivityToOrigin (activityBuilder: (audience: ActivityAudience) => Activity, options: {
40 video: MVideoImmutable | MVideoAccountLight
41 actorsInvolvedInVideo?: MActorLight[]
42 transaction?: Transaction
43 contextType?: ContextType
45 const { byActor, video, actorsInvolvedInVideo, transaction, contextType } = options
47 if (video.isOwned()) throw new Error('Cannot send activity to owned video origin ' + video.url)
49 let accountActor: MActorLight = (video as MVideoAccountLight).VideoChannel?.Account?.Actor
50 if (!accountActor) accountActor = await ActorModel.loadAccountActorByVideoId(video.id, transaction)
52 const audience = getOriginVideoAudience(accountActor, actorsInvolvedInVideo)
53 const activity = activityBuilder(audience)
55 return afterCommitIfTransaction(transaction, () => {
56 return unicastTo(activity, byActor, accountActor.getSharedInbox(), contextType)
60 // ---------------------------------------------------------------------------
62 async function forwardVideoRelatedActivity (
65 followersException: MActorWithInboxes[],
68 // Mastodon does not add our announces in audience, so we forward to them manually
69 const additionalActors = await getActorsInvolvedInVideo(video, t)
70 const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
72 return forwardActivity(activity, t, followersException, additionalFollowerUrls)
75 async function forwardActivity (
78 followersException: MActorWithInboxes[] = [],
79 additionalFollowerUrls: string[] = []
81 logger.info('Forwarding activity %s.', activity.id)
83 const to = activity.to || []
84 const cc = activity.cc || []
86 const followersUrls = additionalFollowerUrls
87 for (const dest of to.concat(cc)) {
88 if (dest.endsWith('/followers')) {
89 followersUrls.push(dest)
93 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
94 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
96 if (uris.length === 0) {
97 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
101 logger.debug('Creating forwarding job.', { uris })
107 return afterCommitIfTransaction(t, () => JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }))
110 // ---------------------------------------------------------------------------
112 async function broadcastToFollowers (
115 toFollowersOf: MActorId[],
117 actorsException: MActorWithInboxes[] = [],
118 contextType?: ContextType
120 const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
122 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
125 async function broadcastToActors (
130 actorsException: MActorWithInboxes[] = [],
131 contextType?: ContextType
133 const uris = await computeUris(toActors, actorsException)
134 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
137 function broadcastTo (uris: string[], data: any, byActor: MActorId, contextType?: ContextType) {
138 if (uris.length === 0) return undefined
140 const broadcastUris: string[] = []
141 const unicastUris: string[] = []
143 // Bad URIs could be slow to respond, prefer to process them in a dedicated queue
144 for (const uri of uris) {
145 if (ActorFollowHealthCache.Instance.isBadInbox(uri)) {
146 unicastUris.push(uri)
148 broadcastUris.push(uri)
152 logger.debug('Creating broadcast job.', { broadcastUris, unicastUris })
154 if (broadcastUris.length !== 0) {
157 signatureActorId: byActor.id,
162 JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
165 for (const unicastUri of unicastUris) {
168 signatureActorId: byActor.id,
173 JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
177 function unicastTo (data: any, byActor: MActorId, toActorUrl: string, contextType?: ContextType) {
178 logger.debug('Creating unicast job.', { uri: toActorUrl })
182 signatureActorId: byActor.id,
187 JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
190 // ---------------------------------------------------------------------------
193 broadcastToFollowers,
197 sendVideoActivityToOrigin,
198 forwardVideoRelatedActivity,
199 sendVideoRelatedActivity
202 // ---------------------------------------------------------------------------
204 async function computeFollowerUris (toFollowersOf: MActorId[], actorsException: MActorWithInboxes[], t: Transaction) {
205 const toActorFollowerIds = toFollowersOf.map(a => a.id)
207 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
208 const sharedInboxesException = await buildSharedInboxesException(actorsException)
210 return result.data.filter(sharedInbox => sharedInboxesException.includes(sharedInbox) === false)
213 async function computeUris (toActors: MActor[], actorsException: MActorWithInboxes[] = []) {
214 const serverActor = await getServerActor()
215 const targetUrls = toActors
216 .filter(a => a.id !== serverActor.id) // Don't send to ourselves
217 .map(a => a.getSharedInbox())
219 const toActorSharedInboxesSet = new Set(targetUrls)
221 const sharedInboxesException = await buildSharedInboxesException(actorsException)
222 return Array.from(toActorSharedInboxesSet)
223 .filter(sharedInbox => sharedInboxesException.includes(sharedInbox) === false)
226 async function buildSharedInboxesException (actorsException: MActorWithInboxes[]) {
227 const serverActor = await getServerActor()
229 return actorsException
230 .map(f => f.getSharedInbox())
231 .concat([ serverActor.sharedInboxUrl ])