mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
Add more contribution help
This commit is contained in:
parent
460b68c381
commit
35f54d108f
9 changed files with 331 additions and 6 deletions
64
classes/activitypub.ts
Normal file
64
classes/activitypub.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import { APActivity, APActor } from "activitypub-types";
|
||||
|
||||
export class RemoteActor {
|
||||
private actorData: APActor | null;
|
||||
private actorUri: string;
|
||||
|
||||
constructor(actor: APActor | string) {
|
||||
if (typeof actor === "string") {
|
||||
this.actorUri = actor;
|
||||
this.actorData = null;
|
||||
} else {
|
||||
this.actorUri = actor.id || "";
|
||||
this.actorData = actor;
|
||||
}
|
||||
}
|
||||
|
||||
public async fetch() {
|
||||
const response = await fetch(this.actorUri);
|
||||
const actorJson = (await response.json()) as APActor;
|
||||
this.actorData = actorJson;
|
||||
}
|
||||
|
||||
public getData() {
|
||||
return this.actorData;
|
||||
}
|
||||
}
|
||||
|
||||
export class RemoteActivity {
|
||||
private data: APActivity | null;
|
||||
private uri: string;
|
||||
|
||||
constructor(uri: string, data: APActivity | null) {
|
||||
this.uri = uri;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public async fetch() {
|
||||
const response = await fetch(this.uri);
|
||||
const json = (await response.json()) as APActivity;
|
||||
this.data = json;
|
||||
}
|
||||
|
||||
public getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public async getActor() {
|
||||
if (!this.data) {
|
||||
throw new Error("No data");
|
||||
}
|
||||
|
||||
if (Array.isArray(this.data.actor)) {
|
||||
throw new Error("Multiple actors");
|
||||
}
|
||||
|
||||
if (typeof this.data.actor === "string") {
|
||||
const actor = new RemoteActor(this.data.actor);
|
||||
await actor.fetch();
|
||||
return actor.getData();
|
||||
}
|
||||
|
||||
return new RemoteActor(this.data.actor as any);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue