From e78069d90449100cb728b957cdac5e83f6005f2e Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sun, 20 Nov 2022 21:21:12 +0100 Subject: [PATCH] server: implement moveTo property on actors Co-authored-by: Mary Strodl Co-authored-by: amybones --- .../src/remote/activitypub/models/person.ts | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/remote/activitypub/models/person.ts b/packages/backend/src/remote/activitypub/models/person.ts index ba677a9dc..17d9858e4 100644 --- a/packages/backend/src/remote/activitypub/models/person.ts +++ b/packages/backend/src/remote/activitypub/models/person.ts @@ -39,7 +39,7 @@ const summaryLength = 2048; * @param x Fetched object * @param uri Fetch target URI */ -function validateActor(x: IObject): IActor { +async function validateActor(x: IObject, resolver: Resolver): Promise { if (x == null) { throw new Error('invalid Actor: object is null'); } @@ -61,6 +61,22 @@ function validateActor(x: IObject): IActor { throw new StatusError('cannot resolve local user', 400, 'cannot resolve local user'); } + if (x.movedTo !== undefined) { + if (!(typeof x.movedTo === 'string' && x.movedTo.length > 0)) { + throw new Error('invalid Actor: wrong movedTo'); + } + if (x.movedTo === uri) { + throw new Error('invalid Actor: moved to self'); + } + // This may throw an exception if we cannot resolve the move target. + // If we are processing an incoming activity, this is desired behaviour + // because that will cause the activity to be retried. + await resolvePerson(x.movedTo, resolver) + .then(moveTarget => { + x.movedTo = moveTarget.id + }); + } + if (!(typeof x.inbox === 'string' && x.inbox.length > 0)) { throw new Error('invalid Actor: wrong inbox'); } @@ -137,7 +153,7 @@ export async function fetchPerson(uri: string): Promise { export async function createPerson(value: string | IObject, resolver: Resolver): Promise { const object = await resolver.resolve(value) as any; - const person = validateActor(object); + const person = await validateActor(object, resolver); apLogger.info(`Creating the Person: ${person.id}`); @@ -177,6 +193,7 @@ export async function createPerson(value: string | IObject, resolver: Resolver): isBot, isCat: (person as any).isCat === true, showTimelineReplies: false, + movedToId: person.movedTo, })) as IRemoteUser; await transactionalEntityManager.save(new UserProfile({ @@ -287,7 +304,7 @@ export async function updatePerson(value: IObject | string, resolver: Resolver): const object = await resolver.resolve(value); - const person = validateActor(object); + const person = await validateActor(object, resolver); apLogger.info(`Updating the Person: ${person.id}`); @@ -328,6 +345,7 @@ export async function updatePerson(value: IObject | string, resolver: Resolver): isCat: (person as any).isCat === true, isLocked: !!person.manuallyApprovesFollowers, isExplorable: !!person.discoverable, + movedToId: person.movedTo, } as Partial; if (avatar) {