0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Simplified URL creation for Actor JSON-LD

no-issue

This makes the code easier to understand and maintain, and reduces the overhead
of converting to/from a Map. It also changes the URLs and makes them path based
This commit is contained in:
Fabien O'Carroll 2024-04-17 19:45:46 +07:00 committed by Fabien 'egg' O'Carroll
parent 7a2e66708c
commit 1d1c33db1e

View file

@ -13,43 +13,44 @@ type CreateActorData = ActorData & {
id? : ObjectID id? : ObjectID
}; };
function makeUrl(base: URL, props: Map<string, string>): URL { function makeUrl(base: URL, props: Record<string, string>): URL {
const url = new URL(base.href); const url = new URL(`${props.type}`, base.href);
for (const [key, value] of props.entries()) { for (const [key, value] of Object.entries(props)) {
if (key !== 'type') {
url.searchParams.set(key, value); url.searchParams.set(key, value);
} }
}
return url; return url;
} }
function toMap(obj: Record<string, string>): Map<string, string> {
return new Map(Object.entries(obj));
}
export class Actor extends Entity<ActorData> { export class Actor extends Entity<ActorData> {
get username() { get username() {
return this.attr.username; return this.attr.username;
} }
getJSONLD(url: URL): ActivityPub.Actor & ActivityPub.RootObject { getJSONLD(url: URL): ActivityPub.Actor & ActivityPub.RootObject {
const actor = makeUrl(url, toMap({ if (!url.href.endsWith('/')) {
url.href += '/';
}
const actor = makeUrl(url, {
type: 'actor', type: 'actor',
id: this.id.toHexString() id: this.id.toHexString()
})); });
const publicKey = makeUrl(url, toMap({ const publicKey = makeUrl(url, {
type: 'key', type: 'key',
owner: this.id.toHexString() owner: this.id.toHexString()
})); });
const inbox = makeUrl(url, toMap({ const inbox = makeUrl(url, {
type: 'inbox', type: 'inbox',
owner: this.id.toHexString() owner: this.id.toHexString()
})); });
const outbox = makeUrl(url, toMap({ const outbox = makeUrl(url, {
type: 'outbox', type: 'outbox',
owner: this.id.toHexString() owner: this.id.toHexString()
})); });
return { return {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',