Add new API endpoints

This commit is contained in:
Jesse Wierzbinski 2023-09-27 08:45:07 -10:00
parent 95b46ba2e4
commit 1fb4600445
8 changed files with 96 additions and 14 deletions

View file

@ -222,6 +222,7 @@ export class RawActor extends BaseEntity {
* @returns Whether an actor with the specified ID exists in the database.
*/
static async exists(id: string) {
console.log(!!(await RawActor.getByActorId(id)));
return !!(await RawActor.getByActorId(id));
}
}

View file

@ -1,5 +1,5 @@
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import { APActor, APImage, APObject, DateTime } from "activitypub-types";
import { APImage, APObject, DateTime } from "activitypub-types";
import { getConfig } from "@config";
import { appendFile } from "fs/promises";
import { APIStatus } from "~types/entities/status";
@ -64,7 +64,7 @@ export class RawObject extends BaseEntity {
account:
(await (
await RawActor.getByActorId(
(this.data.attributedTo as APActor).id ?? ""
this.data.attributedTo as string
)
)?.toAPIAccount()) ?? (null as unknown as APIAccount),
created_at: new Date(this.data.published as DateTime).toISOString(),

View file

@ -99,7 +99,8 @@ export class Status extends BaseEntity {
async remove(options?: RemoveOptions | undefined) {
// Delete object
await this.object.remove(options);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (this.object) await this.object.remove(options);
return await super.remove(options);
}
@ -146,6 +147,8 @@ export class Status extends BaseEntity {
inReplyTo: data.reply?.object
? data.reply.object.data.id
: undefined,
published: new Date().toISOString(),
tag: [],
attributedTo: `${config.http.base_url}/@${data.account.username}`,
};
@ -231,7 +234,8 @@ export class Status extends BaseEntity {
}
async toAPI(): Promise<APIStatus> {
return {
return await this.object.toAPI();
/* return {
account: await this.account.toAPI(),
application: (await this.application?.toAPI()) ?? null,
bookmarked: false,
@ -241,7 +245,7 @@ export class Status extends BaseEntity {
),
favourited: false,
favourites_count: this.likes.length,
id: this.id,
id: this.object.id,
in_reply_to_account_id: null,
in_reply_to_id: null,
language: null,
@ -263,6 +267,6 @@ export class Status extends BaseEntity {
url: `${config.http.base_url}/@${this.account.username}/${this.id}`,
visibility: "public",
quote: null,
};
}; */
}
}

View file

@ -150,6 +150,7 @@ export class User extends BaseEntity {
relations: {
user: {
relationships: true,
actor: true,
},
},
});
@ -224,8 +225,21 @@ export class User extends BaseEntity {
async updateActor() {
// Check if actor exists
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const actor = this.actor ? this.actor : new RawActor();
// Check is actor already exists
const actorExists = await RawActor.getByActorId(
`${config.http.base_url}/@${this.username}`
);
let actor: RawActor;
if (actorExists) {
actor = actorExists;
} else {
actor = new RawActor();
}
actor.data = {
"@context": [