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

View file

@ -9,6 +9,7 @@ import {
ManyToOne, ManyToOne,
OneToMany, OneToMany,
PrimaryGeneratedColumn, PrimaryGeneratedColumn,
RemoveOptions,
UpdateDateColumn, UpdateDateColumn,
} from "typeorm"; } from "typeorm";
import { APIAccount } from "~types/entities/account"; import { APIAccount } from "~types/entities/account";
@ -174,7 +175,7 @@ export class User extends BaseEntity {
return relationship; return relationship;
} }
async selfDestruct() { async remove(options?: RemoveOptions | undefined) {
// Clean up tokens // Clean up tokens
const tokens = await Token.findBy({ const tokens = await Token.findBy({
user: { user: {
@ -182,9 +183,14 @@ export class User extends BaseEntity {
}, },
}); });
const statuses = await Status.findBy({ const statuses = await Status.find({
account: { where: {
id: this.id, account: {
id: this.id,
},
},
relations: {
object: true,
}, },
}); });
@ -199,6 +205,8 @@ export class User extends BaseEntity {
await Promise.all( await Promise.all(
relationships.map(async relationship => await relationship.remove()) relationships.map(async relationship => await relationship.remove())
); );
return await super.remove(options);
} }
async getRelationships() { 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 */ /* eslint-disable @typescript-eslint/no-unused-vars */
import { getConfig } from "@config"; import { getConfig } from "@config";
import { parseRequest } from "@request"; import { parseRequest } from "@request";
import { errorResponse, jsonResponse } from "@response"; import { errorResponse, jsonResponse } from "@response";
import { APActor } from "activitypub-types";
import { Application } from "~database/entities/Application"; 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 { Status } from "~database/entities/Status";
import { User } from "~database/entities/User"; import { User } from "~database/entities/User";
@ -101,6 +106,22 @@ export default async (req: Request): Promise<Response> => {
return errorResponse("Invalid visibility", 422); 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 // Create status
const newStatus = await Status.createNew({ const newStatus = await Status.createNew({
account: user, account: user,
@ -116,6 +137,13 @@ export default async (req: Request): Promise<Response> => {
sensitive: sensitive || false, sensitive: sensitive || false,
spoiler_text: spoiler_text || "", spoiler_text: spoiler_text || "",
emojis: [], emojis: [],
reply:
replyObject && replyActor
? {
actor: replyActor,
object: replyObject,
}
: undefined,
}); });
// TODO: add database jobs to deliver the post // TODO: add database jobs to deliver the post

View file

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

View file

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

View file

@ -188,6 +188,30 @@ describe("POST /@test/inbox", () => {
expect(activity?.actors[0].data).toEqual({ expect(activity?.actors[0].data).toEqual({
preferredUsername: "test", preferredUsername: "test",
id: `${config.http.base_url}/@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", type: "Person",
}); });