1 import { Transaction } from 'sequelize'
2 import { getServerActor } from '@server/models/application/application'
3 import { ContextType } from '@shared/models/activitypub/context'
4 import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
5 import { afterCommitIfTransaction } from '../../../helpers/database-utils'
6 import { logger } from '../../../helpers/logger'
7 import { ActorModel } from '../../../models/actor/actor'
8 import { ActorFollowModel } from '../../../models/actor/actor-follow'
9 import { MActor, MActorId, MActorLight, MActorWithInboxes, MVideoAccountLight, MVideoId, MVideoImmutable } from '../../../types/models'
10 import { JobQueue } from '../../job-queue'
11 import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience'
13 async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
15 video: MVideoImmutable | MVideoAccountLight
16 transaction?: Transaction
17 contextType?: ContextType
19 const { byActor, video, transaction, contextType } = options
21 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, transaction)
24 if (video.isOwned() === false) {
25 let accountActor: MActorLight = (video as MVideoAccountLight).VideoChannel?.Account?.Actor
27 if (!accountActor) accountActor = await ActorModel.loadAccountActorByVideoId(video.id, transaction)
29 const audience = getRemoteVideoAudience(accountActor, actorsInvolvedInVideo)
30 const activity = activityBuilder(audience)
32 return afterCommitIfTransaction(transaction, () => {
33 return unicastTo(activity, byActor, accountActor.getSharedInbox(), contextType)
38 const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
39 const activity = activityBuilder(audience)
41 const actorsException = [ byActor ]
43 return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException, contextType)
46 async function forwardVideoRelatedActivity (
49 followersException: MActorWithInboxes[],
52 // Mastodon does not add our announces in audience, so we forward to them manually
53 const additionalActors = await getActorsInvolvedInVideo(video, t)
54 const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
56 return forwardActivity(activity, t, followersException, additionalFollowerUrls)
59 async function forwardActivity (
62 followersException: MActorWithInboxes[] = [],
63 additionalFollowerUrls: string[] = []
65 logger.info('Forwarding activity %s.', activity.id)
67 const to = activity.to || []
68 const cc = activity.cc || []
70 const followersUrls = additionalFollowerUrls
71 for (const dest of to.concat(cc)) {
72 if (dest.endsWith('/followers')) {
73 followersUrls.push(dest)
77 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
78 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
80 if (uris.length === 0) {
81 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
85 logger.debug('Creating forwarding job.', { uris })
91 return afterCommitIfTransaction(t, () => JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }))
94 async function broadcastToFollowers (
97 toFollowersOf: MActorId[],
99 actorsException: MActorWithInboxes[] = [],
100 contextType?: ContextType
102 const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
104 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
107 async function broadcastToActors (
112 actorsException: MActorWithInboxes[] = [],
113 contextType?: ContextType
115 const uris = await computeUris(toActors, actorsException)
116 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
119 function broadcastTo (uris: string[], data: any, byActor: MActorId, contextType?: ContextType) {
120 if (uris.length === 0) return undefined
122 logger.debug('Creating broadcast job.', { uris })
126 signatureActorId: byActor.id,
131 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
134 function unicastTo (data: any, byActor: MActorId, toActorUrl: string, contextType?: ContextType) {
135 logger.debug('Creating unicast job.', { uri: toActorUrl })
139 signatureActorId: byActor.id,
144 JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
147 // ---------------------------------------------------------------------------
150 broadcastToFollowers,
154 forwardVideoRelatedActivity,
155 sendVideoRelatedActivity
158 // ---------------------------------------------------------------------------
160 async function computeFollowerUris (toFollowersOf: MActorId[], actorsException: MActorWithInboxes[], t: Transaction) {
161 const toActorFollowerIds = toFollowersOf.map(a => a.id)
163 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
164 const sharedInboxesException = await buildSharedInboxesException(actorsException)
166 return result.data.filter(sharedInbox => sharedInboxesException.includes(sharedInbox) === false)
169 async function computeUris (toActors: MActor[], actorsException: MActorWithInboxes[] = []) {
170 const serverActor = await getServerActor()
171 const targetUrls = toActors
172 .filter(a => a.id !== serverActor.id) // Don't send to ourselves
173 .map(a => a.getSharedInbox())
175 const toActorSharedInboxesSet = new Set(targetUrls)
177 const sharedInboxesException = await buildSharedInboxesException(actorsException)
178 return Array.from(toActorSharedInboxesSet)
179 .filter(sharedInbox => sharedInboxesException.includes(sharedInbox) === false)
182 async function buildSharedInboxesException (actorsException: MActorWithInboxes[]) {
183 const serverActor = await getServerActor()
185 return actorsException
186 .map(f => f.getSharedInbox())
187 .concat([ serverActor.sharedInboxUrl ])