From 346be1d4781d0a3cd3f25e166511af8dee3a03aa Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 19 Jun 2024 17:08:59 +0200 Subject: [PATCH] Support Service AP actors --- server/core/helpers/actors.ts | 15 ++++++++++----- .../custom-validators/activitypub/actor.ts | 3 ++- .../core/lib/activitypub/actors/shared/creator.ts | 5 +++-- .../lib/activitypub/process/process-announce.ts | 2 +- .../lib/activitypub/process/process-delete.ts | 5 +++-- .../lib/activitypub/process/process-update.ts | 4 +++- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/server/core/helpers/actors.ts b/server/core/helpers/actors.ts index 5be5c9ee7..5e92fc008 100644 --- a/server/core/helpers/actors.ts +++ b/server/core/helpers/actors.ts @@ -1,17 +1,22 @@ +import { ActivityPubActorType } from '@peertube/peertube-models' import { WEBSERVER } from '@server/initializers/constants.js' -function handleToNameAndHost (handle: string) { +export function handleToNameAndHost (handle: string) { let [ name, host ] = handle.split('@') if (host === WEBSERVER.HOST) host = null return { name, host, handle } } -function handlesToNameAndHost (handles: string[]) { +export function handlesToNameAndHost (handles: string[]) { return handles.map(h => handleToNameAndHost(h)) } -export { - handleToNameAndHost, - handlesToNameAndHost +const accountType = new Set([ 'Person', 'Application', 'Group', 'Service', 'Organization' ]) +export function isAccountActor (type: ActivityPubActorType) { + return accountType.has(type) +} + +export function isChannelActor (type: ActivityPubActorType) { + return type === 'Group' } diff --git a/server/core/helpers/custom-validators/activitypub/actor.ts b/server/core/helpers/custom-validators/activitypub/actor.ts index 71ddbc323..bcd37ffa1 100644 --- a/server/core/helpers/custom-validators/activitypub/actor.ts +++ b/server/core/helpers/custom-validators/activitypub/actor.ts @@ -20,8 +20,9 @@ function isActorPublicKeyObjectValid (publicKeyObject: any) { isActorPublicKeyValid(publicKeyObject.publicKeyPem) } +const actorTypes = new Set([ 'Person', 'Application', 'Group', 'Service', 'Organization' ]) function isActorTypeValid (type: string) { - return type === 'Person' || type === 'Application' || type === 'Group' || type === 'Service' || type === 'Organization' + return actorTypes.has(type) } function isActorPublicKeyValid (publicKey: string) { diff --git a/server/core/lib/activitypub/actors/shared/creator.ts b/server/core/lib/activitypub/actors/shared/creator.ts index 426f918fb..a7e3f7f01 100644 --- a/server/core/lib/activitypub/actors/shared/creator.ts +++ b/server/core/lib/activitypub/actors/shared/creator.ts @@ -18,6 +18,7 @@ import { import { updateActorImages } from '../image.js' import { getActorAttributesFromObject, getActorDisplayNameFromObject, getImagesInfoFromObject } from './object-to-model-attributes.js' import { fetchActorFollowsCount } from './url-to-object.js' +import { isAccountActor, isChannelActor } from '@server/helpers/actors.js' export class APActorCreator { @@ -43,12 +44,12 @@ export class APActorCreator { await this.tryToFixActorUrlIfNeeded(actorCreated, actorInstance, created, t) - if (actorCreated.type === 'Person' || actorCreated.type === 'Application') { // Account or PeerTube instance + if (isAccountActor(actorCreated.type)) { actorCreated.Account = await this.saveAccount(actorCreated, t) as MAccountDefault actorCreated.Account.Actor = actorCreated } - if (actorCreated.type === 'Group') { // Video channel + if (isChannelActor(actorCreated.type)) { const channel = await this.saveVideoChannel(actorCreated, t) actorCreated.VideoChannel = Object.assign(channel, { Actor: actorCreated, Account: this.ownerActor.Account }) } diff --git a/server/core/lib/activitypub/process/process-announce.ts b/server/core/lib/activitypub/process/process-announce.ts index ac135eae2..8500efd4d 100644 --- a/server/core/lib/activitypub/process/process-announce.ts +++ b/server/core/lib/activitypub/process/process-announce.ts @@ -14,7 +14,7 @@ async function processAnnounceActivity (options: APProcessorOptions