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.
This commit is contained in:
Fabien O'Carroll 2024-05-15 16:05:12 +07:00 committed by Fabien 'egg' O'Carroll
parent b7efe4ff8e
commit f289111f6d

View File

@ -24,6 +24,39 @@ type CreateActorData = ActorData & {
}; };
export class Actor extends Entity<ActorData> { export class Actor extends Entity<ActorData> {
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() { get username() {
return this.attr.username; return this.attr.username;
} }
@ -47,10 +80,6 @@ export class Actor extends Entity<ActorData> {
return this.attr.followers; return this.attr.followers;
} }
get actorId() {
return new URI(`actor/${this.id.toHexString()}`);
}
async sign(request: Request, baseUrl: URL): Promise<Request> { async sign(request: Request, baseUrl: URL): Promise<Request> {
const keyId = new URL(this.getJSONLD(baseUrl).publicKey.id); const keyId = new URL(this.getJSONLD(baseUrl).publicKey.id);
const key = crypto.createPrivateKey(this.attr.privateKey); const key = crypto.createPrivateKey(this.attr.privateKey);
@ -131,14 +160,6 @@ export class Actor extends Entity<ActorData> {
if (!url.href.endsWith('/')) { if (!url.href.endsWith('/')) {
url.href += '/'; 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 { return {
'@context': [ '@context': [
@ -169,11 +190,11 @@ export class Actor extends Entity<ActorData> {
} }
], ],
type: 'Person', type: 'Person',
id: actor.href, id: this.actorId.getValue(url),
name: this.displayName, // Full name name: this.displayName, // Full name
preferredUsername: this.username, // Username preferredUsername: this.username, // Username
summary: 'The bio for the actor', // Bio summary: 'The bio for the actor', // Bio
url: actor.href, // Profile URL url: this.actorId.getValue(url), // Profile URL
icon: '', // Avatar icon: '', // Avatar
image: '', // Header image image: '', // Header image
published: '1970-01-01T00:00:00Z', // When profile was created published: '1970-01-01T00:00:00Z', // When profile was created
@ -186,15 +207,15 @@ export class Actor extends Entity<ActorData> {
}], }],
// Collections // Collections
following: following.href, following: this.followingCollectionId.getValue(url),
followers: followers.href, followers: this.followersCollectionId.getValue(url),
inbox: inbox.href, inbox: this.inboxId.getValue(url),
outbox: outbox.href, outbox: this.outboxId.getValue(url),
featured: featured.href, featured: this.featuredCollectionId.getValue(url),
publicKey: { publicKey: {
id: publicKey.href, id: this.publicKeyId.getValue(url),
owner: actor.href, owner: this.actorId.getValue(url),
publicKeyPem: this.attr.publicKey publicKeyPem: this.attr.publicKey
} }
}; };