From f289111f6d064ddc2d16eb2ff5ace09c8c25680c Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Wed, 15 May 2024 16:05:12 +0700 Subject: [PATCH] Extracted Actor resource ids into getters Now that we have the URI object, we don't need to convert the Actor to JSONLD to get its resource ids. Instead we can have shared getters that expose the ids as a URI, that can be realized as/when they're needed with the base URL. This makes the code a little cleaner and more performant. --- .../src/core/activitypub/actor.entity.ts | 63 ++++++++++++------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/ghost/ghost/src/core/activitypub/actor.entity.ts b/ghost/ghost/src/core/activitypub/actor.entity.ts index f3f5992bef..672ebdd5df 100644 --- a/ghost/ghost/src/core/activitypub/actor.entity.ts +++ b/ghost/ghost/src/core/activitypub/actor.entity.ts @@ -24,6 +24,39 @@ type CreateActorData = ActorData & { }; export class Actor extends Entity { + private getURI(input: string): URI { + const id = this.id.toHexString(); + return new URI(input.replace(':id', id)); + } + + get actorId() { + return this.getURI('actor/:id'); + } + + get publicKeyId() { + return this.getURI('actor/:id#main-key'); + } + + get inboxId() { + return this.getURI('inbox/:id'); + } + + get outboxId() { + return this.getURI('outbox/:id'); + } + + get followingCollectionId() { + return this.getURI('following/:id'); + } + + get followersCollectionId() { + return this.getURI('followers/:id'); + } + + get featuredCollectionId() { + return this.getURI('featured/:id'); + } + get username() { return this.attr.username; } @@ -47,10 +80,6 @@ export class Actor extends Entity { return this.attr.followers; } - get actorId() { - return new URI(`actor/${this.id.toHexString()}`); - } - async sign(request: Request, baseUrl: URL): Promise { const keyId = new URL(this.getJSONLD(baseUrl).publicKey.id); const key = crypto.createPrivateKey(this.attr.privateKey); @@ -131,14 +160,6 @@ export class Actor extends Entity { if (!url.href.endsWith('/')) { url.href += '/'; } - const id = this.id.toHexString(); - const actor = new URL(`actor/${id}`, url.href); - const publicKey = new URL(`actor/${id}#main-key`, url.href); - const inbox = new URL(`inbox/${id}`, url.href); - const outbox = new URL(`outbox/${id}`, url.href); - const following = new URL(`following/${id}`, url.href); - const followers = new URL(`followers/${id}`, url.href); - const featured = new URL(`featured/${id}`, url.href); return { '@context': [ @@ -169,11 +190,11 @@ export class Actor extends Entity { } ], type: 'Person', - id: actor.href, + id: this.actorId.getValue(url), name: this.displayName, // Full name preferredUsername: this.username, // Username summary: 'The bio for the actor', // Bio - url: actor.href, // Profile URL + url: this.actorId.getValue(url), // Profile URL icon: '', // Avatar image: '', // Header image published: '1970-01-01T00:00:00Z', // When profile was created @@ -186,15 +207,15 @@ export class Actor extends Entity { }], // Collections - following: following.href, - followers: followers.href, - inbox: inbox.href, - outbox: outbox.href, - featured: featured.href, + following: this.followingCollectionId.getValue(url), + followers: this.followersCollectionId.getValue(url), + inbox: this.inboxId.getValue(url), + outbox: this.outboxId.getValue(url), + featured: this.featuredCollectionId.getValue(url), publicKey: { - id: publicKey.href, - owner: actor.href, + id: this.publicKeyId.getValue(url), + owner: this.actorId.getValue(url), publicKeyPem: this.attr.publicKey } };