PeerTube/server/lib/activitypub/send/utils.ts

171 lines
5.5 KiB
TypeScript
Raw Normal View History

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