More refactoring, API route fixes

This commit is contained in:
Jesse Wierzbinski 2023-09-26 13:08:05 -10:00
parent 3b452d66aa
commit 95b46ba2e4
6 changed files with 72 additions and 16 deletions

View file

@ -7,7 +7,6 @@ import {
JoinTable,
ManyToMany,
ManyToOne,
OneToOne,
PrimaryGeneratedColumn,
RemoveOptions,
UpdateDateColumn,
@ -46,7 +45,10 @@ export class Status extends BaseEntity {
})
reblog?: Status;
@OneToOne(() => Status)
@ManyToOne(() => RawObject, {
nullable: true,
onDelete: "SET NULL",
})
object!: RawObject;
@Column("boolean")
@ -63,12 +65,12 @@ export class Status extends BaseEntity {
@ManyToOne(() => RawObject, {
nullable: true,
})
in_reply_to_post!: RawObject;
in_reply_to_post!: RawObject | null;
@ManyToOne(() => RawActor, {
nullable: true,
})
in_reply_to_account!: RawActor;
in_reply_to_account!: RawActor | null;
@Column("boolean")
sensitive!: boolean;
@ -99,9 +101,7 @@ export class Status extends BaseEntity {
// Delete object
await this.object.remove(options);
await super.remove(options);
return this;
return await super.remove(options);
}
static async createNew(data: {
@ -225,7 +225,7 @@ export class Status extends BaseEntity {
}
// TODO: Add default language
await newStatus.object.save();
await newStatus.save();
return newStatus;
}

View file

@ -9,6 +9,7 @@ import {
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
RemoveOptions,
UpdateDateColumn,
} from "typeorm";
import { APIAccount } from "~types/entities/account";
@ -174,7 +175,7 @@ export class User extends BaseEntity {
return relationship;
}
async selfDestruct() {
async remove(options?: RemoveOptions | undefined) {
// Clean up tokens
const tokens = await Token.findBy({
user: {
@ -182,9 +183,14 @@ export class User extends BaseEntity {
},
});
const statuses = await Status.findBy({
account: {
id: this.id,
const statuses = await Status.find({
where: {
account: {
id: this.id,
},
},
relations: {
object: true,
},
});
@ -199,6 +205,8 @@ export class User extends BaseEntity {
await Promise.all(
relationships.map(async relationship => await relationship.remove())
);
return await super.remove(options);
}
async getRelationships() {

View file

@ -1,8 +1,13 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { getConfig } from "@config";
import { parseRequest } from "@request";
import { errorResponse, jsonResponse } from "@response";
import { APActor } from "activitypub-types";
import { Application } from "~database/entities/Application";
import { RawActor } from "~database/entities/RawActor";
import { RawObject } from "~database/entities/RawObject";
import { Status } from "~database/entities/Status";
import { User } from "~database/entities/User";
@ -101,6 +106,22 @@ export default async (req: Request): Promise<Response> => {
return errorResponse("Invalid visibility", 422);
}
// Get reply account and status if exists
let replyObject: RawObject | null = null;
let replyActor: RawActor | null = null;
if (in_reply_to_id) {
replyObject = await RawObject.findOne({
where: {
id: in_reply_to_id,
},
});
replyActor = await RawActor.getByActorId(
(replyObject?.data.attributedTo as APActor).id ?? ""
);
}
// Create status
const newStatus = await Status.createNew({
account: user,
@ -116,6 +137,13 @@ export default async (req: Request): Promise<Response> => {
sensitive: sensitive || false,
spoiler_text: spoiler_text || "",
emojis: [],
reply:
replyObject && replyActor
? {
actor: replyActor,
object: replyObject,
}
: undefined,
});
// TODO: add database jobs to deliver the post

View file

@ -80,7 +80,6 @@ afterAll(async () => {
);
if (user) {
await user.selfDestruct();
await user.remove();
}
});

View file

@ -580,9 +580,6 @@ afterAll(async () => {
})
);
await user.selfDestruct();
await user.remove();
await user2.selfDestruct();
await user2.remove();
});

View file

@ -188,6 +188,30 @@ describe("POST /@test/inbox", () => {
expect(activity?.actors[0].data).toEqual({
preferredUsername: "test",
id: `${config.http.base_url}/@test`,
summary: "",
publicKey: {
id: `${config.http.base_url}/@test/actor#main-key`,
owner: `${config.http.base_url}/@test/actor`,
publicKeyPem: expect.any(String),
},
outbox: `${config.http.base_url}/@test/outbox`,
manuallyApprovesFollowers: false,
followers: `${config.http.base_url}/@test/followers`,
following: `${config.http.base_url}/@test/following`,
name: "",
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
],
icon: {
type: "Image",
url: "",
},
image: {
type: "Image",
url: "",
},
inbox: `${config.http.base_url}/@test/inbox`,
type: "Person",
});