1
0
Fork 0
peertube/server/lib/activitypub/send/utils.ts

185 lines
6.1 KiB
TypeScript
Raw Normal View History

2017-11-20 03:43:39 -05:00
import { Transaction } from 'sequelize'
2018-09-14 10:51:35 -04:00
import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
2017-12-28 05:16:08 -05:00
import { logger } from '../../../helpers/logger'
2017-12-14 11:38:41 -05:00
import { ActorModel } from '../../../models/activitypub/actor'
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
import { JobQueue } from '../../job-queue'
2018-09-14 10:51:35 -04:00
import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience'
import { getServerActor } from '../../../helpers/utils'
2019-07-29 05:59:29 -04:00
import { afterCommitIfTransaction } from '../../../helpers/database-utils'
import { MActorWithInboxes, MActor, MActorId, MActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../../typings/models'
import { ContextType } from '@server/helpers/activitypub'
2018-09-14 10:51:35 -04:00
async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
2020-01-31 10:56:52 -05:00
byActor: MActorLight
video: MVideoAccountLight
2020-02-04 03:12:08 -05:00
transaction?: Transaction
contextType?: ContextType
2018-09-14 10:51:35 -04:00
}) {
const { byActor, video, transaction, contextType } = options
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, transaction)
2018-09-14 10:51:35 -04:00
// Send to origin
if (video.isOwned() === false) {
const audience = getRemoteVideoAudience(video, actorsInvolvedInVideo)
2018-09-14 10:51:35 -04:00
const activity = activityBuilder(audience)
return afterCommitIfTransaction(transaction, () => {
return unicastTo(activity, byActor, video.VideoChannel.Account.Actor.getSharedInbox(), contextType)
2019-07-29 05:59:29 -04:00
})
2018-09-14 10:51:35 -04:00
}
// Send to followers
const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
const activity = activityBuilder(audience)
const actorsException = [ byActor ]
2019-07-29 05:59:29 -04:00
return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException, contextType)
2018-09-14 10:51:35 -04:00
}
async function forwardVideoRelatedActivity (
activity: Activity,
t: Transaction,
followersException: MActorWithInboxes[] = [],
video: MVideoId
) {
// 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)
}
async function forwardActivity (
activity: Activity,
t: Transaction,
followersException: MActorWithInboxes[] = [],
2018-01-08 04:00:35 -05:00
additionalFollowerUrls: string[] = []
) {
2018-07-30 11:02:40 -04:00
logger.info('Forwarding activity %s.', activity.id)
const to = activity.to || []
const cc = activity.cc || []
2018-01-08 04:00:35 -05:00
const followersUrls = additionalFollowerUrls
for (const dest of to.concat(cc)) {
if (dest.endsWith('/followers')) {
followersUrls.push(dest)
}
}
2017-12-14 11:38:41 -05:00
const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
const uris = await computeFollowerUris(toActorFollowers, followersException, t)
if (uris.length === 0) {
2017-12-14 11:38:41 -05:00
logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
2017-11-24 09:00:10 -05:00
return undefined
}
logger.debug('Creating forwarding job.', { uris })
const payload = {
uris,
body: activity
}
2019-07-29 05:59:29 -04:00
return afterCommitIfTransaction(t, () => JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }))
}
2017-11-20 03:43:39 -05:00
2017-11-22 10:25:03 -05:00
async function broadcastToFollowers (
data: any,
2019-08-15 05:53:26 -04:00
byActor: MActorId,
toFollowersOf: MActorId[],
2017-11-22 10:25:03 -05:00
t: Transaction,
actorsException: MActorWithInboxes[] = [],
contextType?: ContextType
2017-11-22 10:25:03 -05:00
) {
2018-09-11 10:27:07 -04:00
const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
2019-07-29 05:59:29 -04:00
return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
2018-01-08 04:00:35 -05:00
}
async function broadcastToActors (
data: any,
2019-08-15 05:53:26 -04:00
byActor: MActorId,
toActors: MActor[],
2019-07-29 05:59:29 -04:00
t?: Transaction,
actorsException: MActorWithInboxes[] = [],
contextType?: ContextType
2018-01-08 04:00:35 -05:00
) {
const uris = await computeUris(toActors, actorsException)
return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
2018-01-08 04:00:35 -05:00
}
function broadcastTo (uris: string[], data: any, byActor: MActorId, contextType?: ContextType) {
2018-01-08 04:00:35 -05:00
if (uris.length === 0) return undefined
2017-11-20 03:43:39 -05:00
logger.debug('Creating broadcast job.', { uris })
2017-11-22 10:25:03 -05:00
const payload = {
2017-11-22 10:25:03 -05:00
uris,
2017-12-14 11:38:41 -05:00
signatureActorId: byActor.id,
body: data,
contextType
2017-11-20 03:43:39 -05:00
}
return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
2017-11-20 03:43:39 -05:00
}
function unicastTo (data: any, byActor: MActorId, toActorUrl: string, contextType?: ContextType) {
2017-12-14 11:38:41 -05:00
logger.debug('Creating unicast job.', { uri: toActorUrl })
const payload = {
uri: toActorUrl,
2017-12-14 11:38:41 -05:00
signatureActorId: byActor.id,
body: data,
contextType
2017-11-20 03:43:39 -05:00
}
2019-07-29 05:59:29 -04:00
JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
2017-11-20 03:43:39 -05:00
}
2018-05-25 05:32:36 -04:00
// ---------------------------------------------------------------------------
2017-11-20 03:43:39 -05:00
2018-05-25 05:32:36 -04:00
export {
broadcastToFollowers,
unicastTo,
forwardActivity,
broadcastToActors,
2018-09-14 10:51:35 -04:00
forwardVideoRelatedActivity,
sendVideoRelatedActivity
2017-11-20 03:43:39 -05:00
}
2018-05-25 05:32:36 -04:00
// ---------------------------------------------------------------------------
2017-12-19 04:34:56 -05:00
async function computeFollowerUris (toFollowersOf: MActorId[], actorsException: MActorWithInboxes[], t: Transaction) {
2018-09-11 10:27:07 -04:00
const toActorFollowerIds = toFollowersOf.map(a => a.id)
2017-12-14 11:38:41 -05:00
const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
const sharedInboxesException = await buildSharedInboxesException(actorsException)
2018-01-08 04:00:35 -05:00
return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
}
async function computeUris (toActors: MActor[], actorsException: MActorWithInboxes[] = []) {
const serverActor = await getServerActor()
const targetUrls = toActors
.filter(a => a.id !== serverActor.id) // Don't send to ourselves
.map(a => a.getSharedInbox())
const toActorSharedInboxesSet = new Set(targetUrls)
2018-01-08 04:00:35 -05:00
const sharedInboxesException = await buildSharedInboxesException(actorsException)
2018-01-08 04:00:35 -05:00
return Array.from(toActorSharedInboxesSet)
2018-05-25 05:32:36 -04:00
.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
2017-11-20 03:43:39 -05:00
}
async function buildSharedInboxesException (actorsException: MActorWithInboxes[]) {
const serverActor = await getServerActor()
return actorsException
.map(f => f.getSharedInbox())
.concat([ serverActor.sharedInboxUrl ])
}