2017-11-20 02:43:39 -06:00
|
|
|
import { Transaction } from 'sequelize'
|
2021-10-13 04:47:32 -05:00
|
|
|
import { ActorFollowHealthCache } from '@server/lib/actor-follow-health-cache'
|
2021-05-11 04:15:29 -05:00
|
|
|
import { getServerActor } from '@server/models/application/application'
|
2022-03-23 10:14:33 -05:00
|
|
|
import { Activity, ActivityAudience, ActivitypubHttpBroadcastPayload } from '@shared/models'
|
2021-05-11 04:15:29 -05:00
|
|
|
import { ContextType } from '@shared/models/activitypub/context'
|
2022-03-18 05:17:35 -05:00
|
|
|
import { afterCommitIfTransaction } from '../../../../helpers/database-utils'
|
|
|
|
import { logger } from '../../../../helpers/logger'
|
|
|
|
import { ActorModel } from '../../../../models/actor/actor'
|
|
|
|
import { ActorFollowModel } from '../../../../models/actor/actor-follow'
|
|
|
|
import { MActor, MActorId, MActorLight, MActorWithInboxes, MVideoAccountLight, MVideoId, MVideoImmutable } from '../../../../types/models'
|
|
|
|
import { JobQueue } from '../../../job-queue'
|
|
|
|
import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getOriginVideoAudience } from './audience-utils'
|
2018-05-31 03:23:56 -05:00
|
|
|
|
2018-09-14 09:51:35 -05:00
|
|
|
async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
|
2020-01-31 09:56:52 -06:00
|
|
|
byActor: MActorLight
|
2020-02-04 09:14:33 -06:00
|
|
|
video: MVideoImmutable | MVideoAccountLight
|
2022-03-23 10:14:33 -05:00
|
|
|
contextType: ContextType
|
2022-06-17 07:08:13 -05:00
|
|
|
parallelizable?: boolean
|
2020-02-04 02:12:08 -06:00
|
|
|
transaction?: Transaction
|
2018-09-14 09:51:35 -05:00
|
|
|
}) {
|
2022-06-17 07:08:13 -05:00
|
|
|
const { byActor, video, transaction, contextType, parallelizable } = options
|
2019-08-09 01:17:16 -05:00
|
|
|
|
2018-09-14 09:51:35 -05:00
|
|
|
// Send to origin
|
2019-08-09 01:17:16 -05:00
|
|
|
if (video.isOwned() === false) {
|
2022-03-18 05:17:35 -05:00
|
|
|
return sendVideoActivityToOrigin(activityBuilder, options)
|
2018-09-14 09:51:35 -05:00
|
|
|
}
|
|
|
|
|
2022-06-17 03:49:37 -05:00
|
|
|
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, transaction)
|
|
|
|
|
2018-09-14 09:51:35 -05:00
|
|
|
// Send to followers
|
|
|
|
const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
|
|
|
|
const activity = activityBuilder(audience)
|
|
|
|
|
2019-08-09 01:17:16 -05:00
|
|
|
const actorsException = [ byActor ]
|
2019-07-29 04:59:29 -05:00
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
return broadcastToFollowers({
|
|
|
|
data: activity,
|
|
|
|
byActor,
|
|
|
|
toFollowersOf: actorsInvolvedInVideo,
|
|
|
|
transaction,
|
|
|
|
actorsException,
|
2022-06-17 07:08:13 -05:00
|
|
|
parallelizable,
|
2022-03-23 10:14:33 -05:00
|
|
|
contextType
|
|
|
|
})
|
2018-09-14 09:51:35 -05:00
|
|
|
}
|
|
|
|
|
2022-03-18 05:17:35 -05:00
|
|
|
async function sendVideoActivityToOrigin (activityBuilder: (audience: ActivityAudience) => Activity, options: {
|
|
|
|
byActor: MActorLight
|
|
|
|
video: MVideoImmutable | MVideoAccountLight
|
2022-03-23 10:14:33 -05:00
|
|
|
contextType: ContextType
|
|
|
|
|
2022-03-18 05:17:35 -05:00
|
|
|
actorsInvolvedInVideo?: MActorLight[]
|
|
|
|
transaction?: Transaction
|
|
|
|
}) {
|
|
|
|
const { byActor, video, actorsInvolvedInVideo, transaction, contextType } = options
|
|
|
|
|
|
|
|
if (video.isOwned()) throw new Error('Cannot send activity to owned video origin ' + video.url)
|
|
|
|
|
|
|
|
let accountActor: MActorLight = (video as MVideoAccountLight).VideoChannel?.Account?.Actor
|
|
|
|
if (!accountActor) accountActor = await ActorModel.loadAccountActorByVideoId(video.id, transaction)
|
|
|
|
|
|
|
|
const audience = getOriginVideoAudience(accountActor, actorsInvolvedInVideo)
|
|
|
|
const activity = activityBuilder(audience)
|
|
|
|
|
|
|
|
return afterCommitIfTransaction(transaction, () => {
|
2022-03-23 10:14:33 -05:00
|
|
|
return unicastTo({
|
|
|
|
data: activity,
|
|
|
|
byActor,
|
|
|
|
toActorUrl: accountActor.getSharedInbox(),
|
|
|
|
contextType
|
|
|
|
})
|
2022-03-18 05:17:35 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-05-31 03:23:56 -05:00
|
|
|
async function forwardVideoRelatedActivity (
|
|
|
|
activity: Activity,
|
|
|
|
t: Transaction,
|
2020-02-28 09:03:39 -06:00
|
|
|
followersException: MActorWithInboxes[],
|
2020-02-04 08:45:41 -06:00
|
|
|
video: MVideoId
|
2018-05-31 03:23:56 -05:00
|
|
|
) {
|
|
|
|
// Mastodon does not add our announces in audience, so we forward to them manually
|
|
|
|
const additionalActors = await getActorsInvolvedInVideo(video, t)
|
|
|
|
const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
|
|
|
|
|
|
|
|
return forwardActivity(activity, t, followersException, additionalFollowerUrls)
|
|
|
|
}
|
2017-11-24 06:41:10 -06:00
|
|
|
|
|
|
|
async function forwardActivity (
|
|
|
|
activity: Activity,
|
|
|
|
t: Transaction,
|
2019-10-23 04:33:53 -05:00
|
|
|
followersException: MActorWithInboxes[] = [],
|
2018-01-08 03:00:35 -06:00
|
|
|
additionalFollowerUrls: string[] = []
|
2017-11-24 06:41:10 -06:00
|
|
|
) {
|
2018-07-30 10:02:40 -05:00
|
|
|
logger.info('Forwarding activity %s.', activity.id)
|
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
const to = activity.to || []
|
|
|
|
const cc = activity.cc || []
|
|
|
|
|
2018-01-08 03:00:35 -06:00
|
|
|
const followersUrls = additionalFollowerUrls
|
2017-11-24 06:41:10 -06:00
|
|
|
for (const dest of to.concat(cc)) {
|
|
|
|
if (dest.endsWith('/followers')) {
|
|
|
|
followersUrls.push(dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
|
|
|
|
const uris = await computeFollowerUris(toActorFollowers, followersException, t)
|
2017-11-24 06:41:10 -06:00
|
|
|
|
|
|
|
if (uris.length === 0) {
|
2017-12-14 10:38:41 -06:00
|
|
|
logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
|
2017-11-24 08:00:10 -06:00
|
|
|
return undefined
|
2017-11-24 06:41:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('Creating forwarding job.', { uris })
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
const payload: ActivitypubHttpBroadcastPayload = {
|
2017-11-24 06:41:10 -06:00
|
|
|
uris,
|
2022-03-23 10:14:33 -05:00
|
|
|
body: activity,
|
|
|
|
contextType: null
|
2017-11-24 06:41:10 -06:00
|
|
|
}
|
2022-08-08 08:48:17 -05:00
|
|
|
return afterCommitIfTransaction(t, () => JobQueue.Instance.createJobAsync({ type: 'activitypub-http-broadcast', payload }))
|
2017-11-24 06:41:10 -06:00
|
|
|
}
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2022-03-18 05:17:35 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
async function broadcastToFollowers (options: {
|
|
|
|
data: any
|
|
|
|
byActor: MActorId
|
|
|
|
toFollowersOf: MActorId[]
|
|
|
|
transaction: Transaction
|
|
|
|
contextType: ContextType
|
|
|
|
|
2022-06-17 07:08:13 -05:00
|
|
|
parallelizable?: boolean
|
2022-03-23 10:14:33 -05:00
|
|
|
actorsException?: MActorWithInboxes[]
|
|
|
|
}) {
|
2022-06-17 07:08:13 -05:00
|
|
|
const { data, byActor, toFollowersOf, transaction, contextType, actorsException = [], parallelizable } = options
|
2019-07-29 04:59:29 -05:00
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
const uris = await computeFollowerUris(toFollowersOf, actorsException, transaction)
|
|
|
|
|
|
|
|
return afterCommitIfTransaction(transaction, () => {
|
|
|
|
return broadcastTo({
|
|
|
|
uris,
|
|
|
|
data,
|
|
|
|
byActor,
|
2022-06-17 07:08:13 -05:00
|
|
|
parallelizable,
|
2022-03-23 10:14:33 -05:00
|
|
|
contextType
|
|
|
|
})
|
|
|
|
})
|
2018-01-08 03:00:35 -06:00
|
|
|
}
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
async function broadcastToActors (options: {
|
|
|
|
data: any
|
|
|
|
byActor: MActorId
|
|
|
|
toActors: MActor[]
|
|
|
|
transaction: Transaction
|
|
|
|
contextType: ContextType
|
|
|
|
actorsException?: MActorWithInboxes[]
|
|
|
|
}) {
|
|
|
|
const { data, byActor, toActors, transaction, contextType, actorsException = [] } = options
|
|
|
|
|
2018-01-08 03:00:35 -06:00
|
|
|
const uris = await computeUris(toActors, actorsException)
|
2022-03-23 10:14:33 -05:00
|
|
|
|
|
|
|
return afterCommitIfTransaction(transaction, () => {
|
|
|
|
return broadcastTo({
|
|
|
|
uris,
|
|
|
|
data,
|
|
|
|
byActor,
|
|
|
|
contextType
|
|
|
|
})
|
|
|
|
})
|
2018-01-08 03:00:35 -06:00
|
|
|
}
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
function broadcastTo (options: {
|
|
|
|
uris: string[]
|
|
|
|
data: any
|
|
|
|
byActor: MActorId
|
|
|
|
contextType: ContextType
|
2022-06-17 07:08:13 -05:00
|
|
|
parallelizable?: boolean // default to false
|
2022-03-23 10:14:33 -05:00
|
|
|
}) {
|
2022-06-17 07:08:13 -05:00
|
|
|
const { uris, data, byActor, contextType, parallelizable } = options
|
2022-03-23 10:14:33 -05:00
|
|
|
|
2018-01-08 03:00:35 -06:00
|
|
|
if (uris.length === 0) return undefined
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2021-10-13 04:47:32 -05:00
|
|
|
const broadcastUris: string[] = []
|
|
|
|
const unicastUris: string[] = []
|
2017-11-22 09:25:03 -06:00
|
|
|
|
2021-10-13 04:47:32 -05:00
|
|
|
// Bad URIs could be slow to respond, prefer to process them in a dedicated queue
|
|
|
|
for (const uri of uris) {
|
|
|
|
if (ActorFollowHealthCache.Instance.isBadInbox(uri)) {
|
|
|
|
unicastUris.push(uri)
|
|
|
|
} else {
|
|
|
|
broadcastUris.push(uri)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('Creating broadcast job.', { broadcastUris, unicastUris })
|
|
|
|
|
|
|
|
if (broadcastUris.length !== 0) {
|
|
|
|
const payload = {
|
|
|
|
uris: broadcastUris,
|
|
|
|
signatureActorId: byActor.id,
|
|
|
|
body: data,
|
|
|
|
contextType
|
|
|
|
}
|
|
|
|
|
2022-08-08 08:48:17 -05:00
|
|
|
JobQueue.Instance.createJobAsync({
|
2022-06-17 07:08:13 -05:00
|
|
|
type: parallelizable
|
|
|
|
? 'activitypub-http-broadcast-parallel'
|
|
|
|
: 'activitypub-http-broadcast',
|
|
|
|
|
|
|
|
payload
|
|
|
|
})
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2021-10-13 04:47:32 -05:00
|
|
|
for (const unicastUri of unicastUris) {
|
|
|
|
const payload = {
|
|
|
|
uri: unicastUri,
|
|
|
|
signatureActorId: byActor.id,
|
|
|
|
body: data,
|
|
|
|
contextType
|
|
|
|
}
|
|
|
|
|
2022-08-08 08:48:17 -05:00
|
|
|
JobQueue.Instance.createJobAsync({ type: 'activitypub-http-unicast', payload })
|
2021-10-13 04:47:32 -05:00
|
|
|
}
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
function unicastTo (options: {
|
|
|
|
data: any
|
|
|
|
byActor: MActorId
|
|
|
|
toActorUrl: string
|
|
|
|
contextType: ContextType
|
|
|
|
}) {
|
|
|
|
const { data, byActor, toActorUrl, contextType } = options
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
logger.debug('Creating unicast job.', { uri: toActorUrl })
|
2017-11-24 06:41:10 -06:00
|
|
|
|
2018-01-25 08:05:18 -06:00
|
|
|
const payload = {
|
|
|
|
uri: toActorUrl,
|
2017-12-14 10:38:41 -06:00
|
|
|
signatureActorId: byActor.id,
|
2020-02-03 04:12:29 -06:00
|
|
|
body: data,
|
|
|
|
contextType
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2022-08-08 08:48:17 -05:00
|
|
|
JobQueue.Instance.createJobAsync({ type: 'activitypub-http-unicast', payload })
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2018-05-25 04:32:36 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2018-05-25 04:32:36 -05:00
|
|
|
export {
|
|
|
|
broadcastToFollowers,
|
|
|
|
unicastTo,
|
|
|
|
forwardActivity,
|
2018-05-31 03:23:56 -05:00
|
|
|
broadcastToActors,
|
2022-03-18 05:17:35 -05:00
|
|
|
sendVideoActivityToOrigin,
|
2018-09-14 09:51:35 -05:00
|
|
|
forwardVideoRelatedActivity,
|
|
|
|
sendVideoRelatedActivity
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2018-05-25 04:32:36 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2017-12-19 03:34:56 -06:00
|
|
|
|
2019-10-23 04:33:53 -05:00
|
|
|
async function computeFollowerUris (toFollowersOf: MActorId[], actorsException: MActorWithInboxes[], t: Transaction) {
|
2018-09-11 09:27:07 -05:00
|
|
|
const toActorFollowerIds = toFollowersOf.map(a => a.id)
|
2017-11-24 06:41:10 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
|
2018-08-16 08:25:20 -05:00
|
|
|
const sharedInboxesException = await buildSharedInboxesException(actorsException)
|
|
|
|
|
2020-02-28 09:03:39 -06:00
|
|
|
return result.data.filter(sharedInbox => sharedInboxesException.includes(sharedInbox) === false)
|
2018-01-08 03:00:35 -06:00
|
|
|
}
|
|
|
|
|
2019-10-23 04:33:53 -05:00
|
|
|
async function computeUris (toActors: MActor[], actorsException: MActorWithInboxes[] = []) {
|
2018-08-16 08:25:20 -05:00
|
|
|
const serverActor = await getServerActor()
|
|
|
|
const targetUrls = toActors
|
|
|
|
.filter(a => a.id !== serverActor.id) // Don't send to ourselves
|
2019-10-23 04:33:53 -05:00
|
|
|
.map(a => a.getSharedInbox())
|
2018-08-16 08:25:20 -05:00
|
|
|
|
|
|
|
const toActorSharedInboxesSet = new Set(targetUrls)
|
2018-01-08 03:00:35 -06:00
|
|
|
|
2018-08-16 08:25:20 -05:00
|
|
|
const sharedInboxesException = await buildSharedInboxesException(actorsException)
|
2018-01-08 03:00:35 -06:00
|
|
|
return Array.from(toActorSharedInboxesSet)
|
2020-02-28 09:03:39 -06:00
|
|
|
.filter(sharedInbox => sharedInboxesException.includes(sharedInbox) === false)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
2018-08-16 08:25:20 -05:00
|
|
|
|
2019-10-23 04:33:53 -05:00
|
|
|
async function buildSharedInboxesException (actorsException: MActorWithInboxes[]) {
|
2018-08-16 08:25:20 -05:00
|
|
|
const serverActor = await getServerActor()
|
|
|
|
|
|
|
|
return actorsException
|
2019-10-23 04:33:53 -05:00
|
|
|
.map(f => f.getSharedInbox())
|
2018-08-16 08:25:20 -05:00
|
|
|
.concat([ serverActor.sharedInboxUrl ])
|
|
|
|
}
|