X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fsend%2Futils.ts;h=4f69afb00098efd3683916ebd7691615f032112c;hb=5224c394b3bbac6ec1543e41fa0ec6db436e84fa;hp=da437292eef24543457f6d2b77793e49b9ceed70;hpb=06a05d5f4784a7cbb27aa1188385b5679845dad8;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/send/utils.ts b/server/lib/activitypub/send/utils.ts index da437292e..4f69afb00 100644 --- a/server/lib/activitypub/send/utils.ts +++ b/server/lib/activitypub/send/utils.ts @@ -1,17 +1,47 @@ import { Transaction } from 'sequelize' -import { Activity } from '../../../../shared/models/activitypub' +import { Activity, ActivityAudience } from '../../../../shared/models/activitypub' import { logger } from '../../../helpers/logger' import { ActorModel } from '../../../models/activitypub/actor' import { ActorFollowModel } from '../../../models/activitypub/actor-follow' import { JobQueue } from '../../job-queue' import { VideoModel } from '../../../models/video/video' -import { getActorsInvolvedInVideo } from '../audience' +import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience' import { getServerActor } from '../../../helpers/utils' +import { afterCommitIfTransaction } from '../../../helpers/database-utils' +import { ActorFollowerException, ActorModelId, ActorModelOnly } from '../../../typings/models' + +async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: { + byActor: ActorModelOnly, + video: VideoModel, + transaction?: Transaction +}) { + const { byActor, video, transaction } = options + + const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, transaction) + + // Send to origin + if (video.isOwned() === false) { + const audience = getRemoteVideoAudience(video, actorsInvolvedInVideo) + const activity = activityBuilder(audience) + + return afterCommitIfTransaction(transaction, () => { + return unicastTo(activity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) + }) + } + + // Send to followers + const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo) + const activity = activityBuilder(audience) + + const actorsException = [ byActor ] + + return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException) +} async function forwardVideoRelatedActivity ( activity: Activity, t: Transaction, - followersException: ActorModel[] = [], + followersException: ActorFollowerException[] = [], video: VideoModel ) { // Mastodon does not add our announces in audience, so we forward to them manually @@ -24,7 +54,7 @@ async function forwardVideoRelatedActivity ( async function forwardActivity ( activity: Activity, t: Transaction, - followersException: ActorModel[] = [], + followersException: ActorFollowerException[] = [], additionalFollowerUrls: string[] = [] ) { logger.info('Forwarding activity %s.', activity.id) @@ -53,31 +83,33 @@ async function forwardActivity ( uris, body: activity } - return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }) + return afterCommitIfTransaction(t, () => JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })) } async function broadcastToFollowers ( data: any, - byActor: ActorModel, - toActorFollowers: ActorModel[], + byActor: ActorModelId, + toFollowersOf: ActorModelId[], t: Transaction, - actorsException: ActorModel[] = [] + actorsException: ActorFollowerException[] = [] ) { - const uris = await computeFollowerUris(toActorFollowers, actorsException, t) - return broadcastTo(uris, data, byActor) + const uris = await computeFollowerUris(toFollowersOf, actorsException, t) + + return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor)) } async function broadcastToActors ( data: any, - byActor: ActorModel, - toActors: ActorModel[], - actorsException: ActorModel[] = [] + byActor: ActorModelId, + toActors: ActorModelOnly[], + t?: Transaction, + actorsException: ActorFollowerException[] = [] ) { const uris = await computeUris(toActors, actorsException) - return broadcastTo(uris, data, byActor) + return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor)) } -async function broadcastTo (uris: string[], data: any, byActor: ActorModel) { +function broadcastTo (uris: string[], data: any, byActor: ActorModelId) { if (uris.length === 0) return undefined logger.debug('Creating broadcast job.', { uris }) @@ -91,7 +123,7 @@ async function broadcastTo (uris: string[], data: any, byActor: ActorModel) { return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }) } -async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) { +function unicastTo (data: any, byActor: ActorModelId, toActorUrl: string) { logger.debug('Creating unicast job.', { uri: toActorUrl }) const payload = { @@ -100,7 +132,7 @@ async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) { body: data } - return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload }) + JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload }) } // --------------------------------------------------------------------------- @@ -110,13 +142,14 @@ export { unicastTo, forwardActivity, broadcastToActors, - forwardVideoRelatedActivity + forwardVideoRelatedActivity, + sendVideoRelatedActivity } // --------------------------------------------------------------------------- -async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) { - const toActorFollowerIds = toActorFollower.map(a => a.id) +async function computeFollowerUris (toFollowersOf: ActorModelId[], actorsException: ActorFollowerException[], t: Transaction) { + const toActorFollowerIds = toFollowersOf.map(a => a.id) const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t) const sharedInboxesException = await buildSharedInboxesException(actorsException) @@ -124,7 +157,7 @@ async function computeFollowerUris (toActorFollower: ActorModel[], actorsExcepti return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) } -async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) { +async function computeUris (toActors: ActorModelOnly[], actorsException: ActorFollowerException[] = []) { const serverActor = await getServerActor() const targetUrls = toActors .filter(a => a.id !== serverActor.id) // Don't send to ourselves @@ -137,7 +170,7 @@ async function computeUris (toActors: ActorModel[], actorsException: ActorModel[ .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) } -async function buildSharedInboxesException (actorsException: ActorModel[]) { +async function buildSharedInboxesException (actorsException: ActorFollowerException[]) { const serverActor = await getServerActor() return actorsException