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:
parent
7a2e66708c
commit
1d1c33db1e
1 changed files with 17 additions and 16 deletions
|
@ -13,43 +13,44 @@ type CreateActorData = ActorData & {
|
|||
id? : ObjectID
|
||||
};
|
||||
|
||||
function makeUrl(base: URL, props: Map<string, string>): URL {
|
||||
const url = new URL(base.href);
|
||||
for (const [key, value] of props.entries()) {
|
||||
url.searchParams.set(key, value);
|
||||
function makeUrl(base: URL, props: Record<string, string>): URL {
|
||||
const url = new URL(`${props.type}`, base.href);
|
||||
for (const [key, value] of Object.entries(props)) {
|
||||
if (key !== 'type') {
|
||||
url.searchParams.set(key, value);
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
function toMap(obj: Record<string, string>): Map<string, string> {
|
||||
return new Map(Object.entries(obj));
|
||||
}
|
||||
|
||||
export class Actor extends Entity<ActorData> {
|
||||
get username() {
|
||||
return this.attr.username;
|
||||
}
|
||||
|
||||
getJSONLD(url: URL): ActivityPub.Actor & ActivityPub.RootObject {
|
||||
const actor = makeUrl(url, toMap({
|
||||
if (!url.href.endsWith('/')) {
|
||||
url.href += '/';
|
||||
}
|
||||
const actor = makeUrl(url, {
|
||||
type: 'actor',
|
||||
id: this.id.toHexString()
|
||||
}));
|
||||
});
|
||||
|
||||
const publicKey = makeUrl(url, toMap({
|
||||
const publicKey = makeUrl(url, {
|
||||
type: 'key',
|
||||
owner: this.id.toHexString()
|
||||
}));
|
||||
});
|
||||
|
||||
const inbox = makeUrl(url, toMap({
|
||||
const inbox = makeUrl(url, {
|
||||
type: 'inbox',
|
||||
owner: this.id.toHexString()
|
||||
}));
|
||||
});
|
||||
|
||||
const outbox = makeUrl(url, toMap({
|
||||
const outbox = makeUrl(url, {
|
||||
type: 'outbox',
|
||||
owner: this.id.toHexString()
|
||||
}));
|
||||
});
|
||||
|
||||
return {
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
|
|
Loading…
Reference in a new issue