From a219c9100b3ce8774d454497d46be87465bf664e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 23 Mar 2022 16:14:33 +0100 Subject: Refactor AP context builder --- server/lib/activitypub/send/send-create.ts | 67 ++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 13 deletions(-) (limited to 'server/lib/activitypub/send/send-create.ts') diff --git a/server/lib/activitypub/send/send-create.ts b/server/lib/activitypub/send/send-create.ts index f6d897220..5d8763495 100644 --- a/server/lib/activitypub/send/send-create.ts +++ b/server/lib/activitypub/send/send-create.ts @@ -25,7 +25,7 @@ import { const lTags = loggerTagsFactory('ap', 'create') -async function sendCreateVideo (video: MVideoAP, t: Transaction) { +async function sendCreateVideo (video: MVideoAP, transaction: Transaction) { if (!video.hasPrivacyForFederation()) return undefined logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid)) @@ -36,7 +36,13 @@ async function sendCreateVideo (video: MVideoAP, t: Transaction) { const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC) const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience) - return broadcastToFollowers(createActivity, byActor, [ byActor ], t) + return broadcastToFollowers({ + data: createActivity, + byActor, + toFollowersOf: [ byActor ], + transaction, + contextType: 'Video' + }) } async function sendCreateCacheFile ( @@ -55,7 +61,7 @@ async function sendCreateCacheFile ( }) } -async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transaction) { +async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, transaction: Transaction) { if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid)) @@ -63,7 +69,7 @@ async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transac const byActor = playlist.OwnerAccount.Actor const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC) - const object = await playlist.toActivityPubObject(null, t) + const object = await playlist.toActivityPubObject(null, transaction) const createActivity = buildCreateActivity(playlist.url, byActor, object, audience) const serverActor = await getServerActor() @@ -71,19 +77,25 @@ async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transac if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor) - return broadcastToFollowers(createActivity, byActor, toFollowersOf, t) + return broadcastToFollowers({ + data: createActivity, + byActor, + toFollowersOf, + transaction, + contextType: 'Playlist' + }) } -async function sendCreateVideoComment (comment: MCommentOwnerVideo, t: Transaction) { +async function sendCreateVideoComment (comment: MCommentOwnerVideo, transaction: Transaction) { logger.info('Creating job to send comment %s.', comment.url) const isOrigin = comment.Video.isOwned() const byActor = comment.Account.Actor - const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t) + const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, transaction) const commentObject = comment.toActivityPubObject(threadParentComments) - const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t) + const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, transaction) // Add the actor that commented too actorsInvolvedInComment.push(byActor) @@ -101,16 +113,45 @@ async function sendCreateVideoComment (comment: MCommentOwnerVideo, t: Transacti // This was a reply, send it to the parent actors const actorsException = [ byActor ] - await broadcastToActors(createActivity, byActor, parentsCommentActors, t, actorsException) + await broadcastToActors({ + data: createActivity, + byActor, + toActors: parentsCommentActors, + transaction, + actorsException, + contextType: 'Comment' + }) // Broadcast to our followers - await broadcastToFollowers(createActivity, byActor, [ byActor ], t) + await broadcastToFollowers({ + data: createActivity, + byActor, + toFollowersOf: [ byActor ], + transaction, + contextType: 'Comment' + }) // Send to actors involved in the comment - if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException) + if (isOrigin) { + return broadcastToFollowers({ + data: createActivity, + byActor, + toFollowersOf: actorsInvolvedInComment, + transaction, + actorsException, + contextType: 'Comment' + }) + } // Send to origin - t.afterCommit(() => unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.getSharedInbox())) + return transaction.afterCommit(() => { + return unicastTo({ + data: createActivity, + byActor, + toActorUrl: comment.Video.VideoChannel.Account.Actor.getSharedInbox(), + contextType: 'Comment' + }) + }) } function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate { @@ -144,8 +185,8 @@ async function sendVideoRelatedCreateActivity (options: { video: MVideoAccountLight url: string object: any + contextType: ContextType transaction?: Transaction - contextType?: ContextType }) { const activityBuilder = (audience: ActivityAudience) => { return buildCreateActivity(options.url, options.byActor, options.object, audience) -- cgit v1.2.3