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 { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience'
8 import { getServerActor } from '../../../helpers/utils'
9 import { afterCommitIfTransaction } from '../../../helpers/database-utils'
10 import { MActorWithInboxes, MActor, MActorId, MActorLight, MVideo, MVideoAccountLight } from '../../../typings/models'
11 import { ContextType } from '@server/helpers/activitypub'
13 async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
15 video: 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 const audience = getRemoteVideoAudience(video, actorsInvolvedInVideo)
26 const activity = activityBuilder(audience)
28 return afterCommitIfTransaction(transaction, () => {
29 return unicastTo(activity, byActor, video.VideoChannel.Account.Actor.getSharedInbox(), contextType)
34 const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
35 const activity = activityBuilder(audience)
37 const actorsException = [ byActor ]
39 return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException, contextType)
42 async function forwardVideoRelatedActivity (
45 followersException: MActorWithInboxes[] = [],
48 // Mastodon does not add our announces in audience, so we forward to them manually
49 const additionalActors = await getActorsInvolvedInVideo(video, t)
50 const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
52 return forwardActivity(activity, t, followersException, additionalFollowerUrls)
55 async function forwardActivity (
58 followersException: MActorWithInboxes[] = [],
59 additionalFollowerUrls: string[] = []
61 logger.info('Forwarding activity %s.', activity.id)
63 const to = activity.to || []
64 const cc = activity.cc || []
66 const followersUrls = additionalFollowerUrls
67 for (const dest of to.concat(cc)) {
68 if (dest.endsWith('/followers')) {
69 followersUrls.push(dest)
73 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
74 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
76 if (uris.length === 0) {
77 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
81 logger.debug('Creating forwarding job.', { uris })
87 return afterCommitIfTransaction(t, () => JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }))
90 async function broadcastToFollowers (
93 toFollowersOf: MActorId[],
95 actorsException: MActorWithInboxes[] = [],
96 contextType?: ContextType
98 const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
100 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
103 async function broadcastToActors (
108 actorsException: MActorWithInboxes[] = [],
109 contextType?: ContextType
111 const uris = await computeUris(toActors, actorsException)
112 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
115 function broadcastTo (uris: string[], data: any, byActor: MActorId, contextType?: ContextType) {
116 if (uris.length === 0) return undefined
118 logger.debug('Creating broadcast job.', { uris })
122 signatureActorId: byActor.id,
127 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
130 function unicastTo (data: any, byActor: MActorId, toActorUrl: string, contextType?: ContextType) {
131 logger.debug('Creating unicast job.', { uri: toActorUrl })
135 signatureActorId: byActor.id,
140 JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
143 // ---------------------------------------------------------------------------
146 broadcastToFollowers,
150 forwardVideoRelatedActivity,
151 sendVideoRelatedActivity
154 // ---------------------------------------------------------------------------
156 async function computeFollowerUris (toFollowersOf: MActorId[], actorsException: MActorWithInboxes[], t: Transaction) {
157 const toActorFollowerIds = toFollowersOf.map(a => a.id)
159 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
160 const sharedInboxesException = await buildSharedInboxesException(actorsException)
162 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
165 async function computeUris (toActors: MActor[], actorsException: MActorWithInboxes[] = []) {
166 const serverActor = await getServerActor()
167 const targetUrls = toActors
168 .filter(a => a.id !== serverActor.id) // Don't send to ourselves
169 .map(a => a.getSharedInbox())
171 const toActorSharedInboxesSet = new Set(targetUrls)
173 const sharedInboxesException = await buildSharedInboxesException(actorsException)
174 return Array.from(toActorSharedInboxesSet)
175 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
178 async function buildSharedInboxesException (actorsException: MActorWithInboxes[]) {
179 const serverActor = await getServerActor()
181 return actorsException
182 .map(f => f.getSharedInbox())
183 .concat([ serverActor.sharedInboxUrl ])