🚧 auto delete relations

This commit is contained in:
Flam3rboy 2021-09-20 17:11:22 +02:00
parent 3c8b0f43c4
commit ce09e01c21
5 changed files with 112 additions and 69 deletions

View File

@ -1,6 +1,15 @@
import { Channel, ChannelDeleteEvent, ChannelPermissionOverwriteType, ChannelType, ChannelUpdateEvent, emitEvent, Recipient } from "@fosscord/util"; import {
Channel,
ChannelDeleteEvent,
ChannelPermissionOverwriteType,
ChannelType,
ChannelUpdateEvent,
emitEvent,
Recipient,
handleFile
} from "@fosscord/util";
import { Request, Response, Router } from "express"; import { Request, Response, Router } from "express";
import { handleFile, route } from "@fosscord/api"; import { route } from "@fosscord/api";
const router: Router = Router(); const router: Router = Router();
// TODO: delete channel // TODO: delete channel
@ -20,15 +29,14 @@ router.delete("/", route({ permission: "MANAGE_CHANNELS" }), async (req: Request
const channel = await Channel.findOneOrFail({ where: { id: channel_id }, relations: ["recipients"] }); const channel = await Channel.findOneOrFail({ where: { id: channel_id }, relations: ["recipients"] });
if (channel.type === ChannelType.DM) { if (channel.type === ChannelType.DM) {
const recipient = await Recipient.findOneOrFail({ where: { channel_id: channel_id, user_id: req.user_id } }) const recipient = await Recipient.findOneOrFail({ where: { channel_id: channel_id, user_id: req.user_id } });
recipient.closed = true recipient.closed = true;
await Promise.all([ await Promise.all([
recipient.save(), recipient.save(),
emitEvent({ event: "CHANNEL_DELETE", data: channel, user_id: req.user_id } as ChannelDeleteEvent) emitEvent({ event: "CHANNEL_DELETE", data: channel, user_id: req.user_id } as ChannelDeleteEvent)
]); ]);
} else if (channel.type === ChannelType.GROUP_DM) { } else if (channel.type === ChannelType.GROUP_DM) {
await Channel.removeRecipientFromChannel(channel, req.user_id) await Channel.removeRecipientFromChannel(channel, req.user_id);
} else { } else {
//TODO messages in this channel should be deleted before deleting the channel //TODO messages in this channel should be deleted before deleting the channel
await Promise.all([ await Promise.all([

98
util/package-lock.json generated
View File

@ -226,19 +226,19 @@
} }
}, },
"node_modules/@babel/helper-module-transforms": { "node_modules/@babel/helper-module-transforms": {
"version": "7.15.4", "version": "7.15.7",
"resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.15.4.tgz", "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.15.7.tgz",
"integrity": "sha512-9fHHSGE9zTC++KuXLZcB5FKgvlV83Ox+NLUmQTawovwlJ85+QMhk1CnVk406CQVj97LaWod6KVjl2Sfgw9Aktw==", "integrity": "sha512-ZNqjjQG/AuFfekFTY+7nY4RgBSklgTu970c7Rj3m/JOhIu5KPBUuTA9AY6zaKcUvk4g6EbDXdBnhi35FAssdSw==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"@babel/helper-module-imports": "^7.15.4", "@babel/helper-module-imports": "^7.15.4",
"@babel/helper-replace-supers": "^7.15.4", "@babel/helper-replace-supers": "^7.15.4",
"@babel/helper-simple-access": "^7.15.4", "@babel/helper-simple-access": "^7.15.4",
"@babel/helper-split-export-declaration": "^7.15.4", "@babel/helper-split-export-declaration": "^7.15.4",
"@babel/helper-validator-identifier": "^7.14.9", "@babel/helper-validator-identifier": "^7.15.7",
"@babel/template": "^7.15.4", "@babel/template": "^7.15.4",
"@babel/traverse": "^7.15.4", "@babel/traverse": "^7.15.4",
"@babel/types": "^7.15.4" "@babel/types": "^7.15.6"
}, },
"engines": { "engines": {
"node": ">=6.9.0" "node": ">=6.9.0"
@ -305,9 +305,9 @@
} }
}, },
"node_modules/@babel/helper-validator-identifier": { "node_modules/@babel/helper-validator-identifier": {
"version": "7.14.9", "version": "7.15.7",
"resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.14.9.tgz", "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.15.7.tgz",
"integrity": "sha512-pQYxPY0UP6IHISRitNe8bsijHex4TWZXi2HwKVsjPiltzlhse2znVcm9Ace510VT1kxIHjGJCZZQBX2gJDbo0g==", "integrity": "sha512-K4JvCtQqad9OY2+yTU8w+E82ywk/fe+ELNlt1G8z3bVGlZfn/hOcQQsUhGhW/N+tb3fxK800wLtKOE/aM0m72w==",
"dev": true, "dev": true,
"engines": { "engines": {
"node": ">=6.9.0" "node": ">=6.9.0"
@ -422,9 +422,9 @@
} }
}, },
"node_modules/@babel/parser": { "node_modules/@babel/parser": {
"version": "7.15.6", "version": "7.15.7",
"resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.6.tgz", "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.7.tgz",
"integrity": "sha512-S/TSCcsRuCkmpUuoWijua0Snt+f3ewU/8spLo+4AXJCZfT0bVCzLD5MuOKdrx0mlAptbKzn5AdgEIIKXxXkz9Q==", "integrity": "sha512-rycZXvQ+xS9QyIcJ9HXeDWf1uxqlbVFAUq0Rq0dbc50Zb/+wUe/ehyfzGfm9KZZF0kBejYgxltBXocP+gKdL2g==",
"dev": true, "dev": true,
"bin": { "bin": {
"parser": "bin/babel-parser.js" "parser": "bin/babel-parser.js"
@ -1204,9 +1204,9 @@
} }
}, },
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "14.17.16", "version": "14.17.17",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.16.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.17.tgz",
"integrity": "sha512-WiFf2izl01P1CpeY8WqFAeKWwByMueBEkND38EcN8N68qb0aDG3oIS1P5MhAX5kUdr469qRyqsY/MjanLjsFbQ==" "integrity": "sha512-niAjcewgEYvSPCZm3OaM9y6YQrL2SEPH9PymtE6fuZAvFiP6ereCcvApGl2jKTq7copTIguX3PBvfP08LN4LvQ=="
}, },
"node_modules/@types/node-fetch": { "node_modules/@types/node-fetch": {
"version": "2.5.12", "version": "2.5.12",
@ -1941,9 +1941,9 @@
} }
}, },
"node_modules/caniuse-lite": { "node_modules/caniuse-lite": {
"version": "1.0.30001257", "version": "1.0.30001258",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001257.tgz", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001258.tgz",
"integrity": "sha512-JN49KplOgHSXpIsVSF+LUyhD8PUp6xPpAXeRrrcBh4KBeP7W864jHn6RvzJgDlrReyeVjMFJL3PLpPvKIxlIHA==", "integrity": "sha512-RBByOG6xWXUp0CR2/WU2amXz3stjKpSl5J1xU49F1n2OxD//uBZO4wCKUiG+QMGf7CHGfDDcqoKriomoGVxTeA==",
"dev": true, "dev": true,
"funding": { "funding": {
"type": "opencollective", "type": "opencollective",
@ -2498,9 +2498,9 @@
"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
}, },
"node_modules/electron-to-chromium": { "node_modules/electron-to-chromium": {
"version": "1.3.840", "version": "1.3.843",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.840.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.843.tgz",
"integrity": "sha512-yRoUmTLDJnkIJx23xLY7GbSvnmDCq++NSuxHDQ0jiyDJ9YZBUGJcrdUqm+ZwZFzMbCciVzfem2N2AWiHJcWlbw==", "integrity": "sha512-OWEwAbzaVd1Lk9MohVw8LxMXFlnYd9oYTYxfX8KS++kLLjDfbovLOcEEXwRhG612dqGQ6+44SZvim0GXuBRiKg==",
"dev": true "dev": true
}, },
"node_modules/emittery": { "node_modules/emittery": {
@ -6249,13 +6249,12 @@
} }
}, },
"node_modules/stack-utils": { "node_modules/stack-utils": {
"version": "2.0.4", "version": "2.0.5",
"resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.4.tgz", "resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.5.tgz",
"integrity": "sha512-ERg+H//lSSYlZhBIUu+wJnqg30AbyBbpZlIhcshpn7BNzpoRODZgfyr9J+8ERf3ooC6af3u7Lcl01nleau7MrA==", "integrity": "sha512-xrQcmYhOsn/1kX+Vraq+7j4oE2j/6BFscZ0etmYg81xuM8Gq0022Pxb8+IqgOFUIaxHs0KaSb7T1+OegiNrNFA==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"escape-string-regexp": "^2.0.0", "escape-string-regexp": "^2.0.0"
"source-map-support": "^0.5.20"
}, },
"engines": { "engines": {
"node": ">=10" "node": ">=10"
@ -7535,19 +7534,19 @@
} }
}, },
"@babel/helper-module-transforms": { "@babel/helper-module-transforms": {
"version": "7.15.4", "version": "7.15.7",
"resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.15.4.tgz", "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.15.7.tgz",
"integrity": "sha512-9fHHSGE9zTC++KuXLZcB5FKgvlV83Ox+NLUmQTawovwlJ85+QMhk1CnVk406CQVj97LaWod6KVjl2Sfgw9Aktw==", "integrity": "sha512-ZNqjjQG/AuFfekFTY+7nY4RgBSklgTu970c7Rj3m/JOhIu5KPBUuTA9AY6zaKcUvk4g6EbDXdBnhi35FAssdSw==",
"dev": true, "dev": true,
"requires": { "requires": {
"@babel/helper-module-imports": "^7.15.4", "@babel/helper-module-imports": "^7.15.4",
"@babel/helper-replace-supers": "^7.15.4", "@babel/helper-replace-supers": "^7.15.4",
"@babel/helper-simple-access": "^7.15.4", "@babel/helper-simple-access": "^7.15.4",
"@babel/helper-split-export-declaration": "^7.15.4", "@babel/helper-split-export-declaration": "^7.15.4",
"@babel/helper-validator-identifier": "^7.14.9", "@babel/helper-validator-identifier": "^7.15.7",
"@babel/template": "^7.15.4", "@babel/template": "^7.15.4",
"@babel/traverse": "^7.15.4", "@babel/traverse": "^7.15.4",
"@babel/types": "^7.15.4" "@babel/types": "^7.15.6"
} }
}, },
"@babel/helper-optimise-call-expression": { "@babel/helper-optimise-call-expression": {
@ -7596,9 +7595,9 @@
} }
}, },
"@babel/helper-validator-identifier": { "@babel/helper-validator-identifier": {
"version": "7.14.9", "version": "7.15.7",
"resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.14.9.tgz", "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.15.7.tgz",
"integrity": "sha512-pQYxPY0UP6IHISRitNe8bsijHex4TWZXi2HwKVsjPiltzlhse2znVcm9Ace510VT1kxIHjGJCZZQBX2gJDbo0g==", "integrity": "sha512-K4JvCtQqad9OY2+yTU8w+E82ywk/fe+ELNlt1G8z3bVGlZfn/hOcQQsUhGhW/N+tb3fxK800wLtKOE/aM0m72w==",
"dev": true "dev": true
}, },
"@babel/helper-validator-option": { "@babel/helper-validator-option": {
@ -7688,9 +7687,9 @@
} }
}, },
"@babel/parser": { "@babel/parser": {
"version": "7.15.6", "version": "7.15.7",
"resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.6.tgz", "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.7.tgz",
"integrity": "sha512-S/TSCcsRuCkmpUuoWijua0Snt+f3ewU/8spLo+4AXJCZfT0bVCzLD5MuOKdrx0mlAptbKzn5AdgEIIKXxXkz9Q==", "integrity": "sha512-rycZXvQ+xS9QyIcJ9HXeDWf1uxqlbVFAUq0Rq0dbc50Zb/+wUe/ehyfzGfm9KZZF0kBejYgxltBXocP+gKdL2g==",
"dev": true "dev": true
}, },
"@babel/plugin-syntax-async-generators": { "@babel/plugin-syntax-async-generators": {
@ -8336,9 +8335,9 @@
} }
}, },
"@types/node": { "@types/node": {
"version": "14.17.16", "version": "14.17.17",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.16.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.17.tgz",
"integrity": "sha512-WiFf2izl01P1CpeY8WqFAeKWwByMueBEkND38EcN8N68qb0aDG3oIS1P5MhAX5kUdr469qRyqsY/MjanLjsFbQ==" "integrity": "sha512-niAjcewgEYvSPCZm3OaM9y6YQrL2SEPH9PymtE6fuZAvFiP6ereCcvApGl2jKTq7copTIguX3PBvfP08LN4LvQ=="
}, },
"@types/node-fetch": { "@types/node-fetch": {
"version": "2.5.12", "version": "2.5.12",
@ -8945,9 +8944,9 @@
"dev": true "dev": true
}, },
"caniuse-lite": { "caniuse-lite": {
"version": "1.0.30001257", "version": "1.0.30001258",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001257.tgz", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001258.tgz",
"integrity": "sha512-JN49KplOgHSXpIsVSF+LUyhD8PUp6xPpAXeRrrcBh4KBeP7W864jHn6RvzJgDlrReyeVjMFJL3PLpPvKIxlIHA==", "integrity": "sha512-RBByOG6xWXUp0CR2/WU2amXz3stjKpSl5J1xU49F1n2OxD//uBZO4wCKUiG+QMGf7CHGfDDcqoKriomoGVxTeA==",
"dev": true "dev": true
}, },
"caseless": { "caseless": {
@ -9405,9 +9404,9 @@
"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
}, },
"electron-to-chromium": { "electron-to-chromium": {
"version": "1.3.840", "version": "1.3.843",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.840.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.843.tgz",
"integrity": "sha512-yRoUmTLDJnkIJx23xLY7GbSvnmDCq++NSuxHDQ0jiyDJ9YZBUGJcrdUqm+ZwZFzMbCciVzfem2N2AWiHJcWlbw==", "integrity": "sha512-OWEwAbzaVd1Lk9MohVw8LxMXFlnYd9oYTYxfX8KS++kLLjDfbovLOcEEXwRhG612dqGQ6+44SZvim0GXuBRiKg==",
"dev": true "dev": true
}, },
"emittery": { "emittery": {
@ -12300,13 +12299,12 @@
} }
}, },
"stack-utils": { "stack-utils": {
"version": "2.0.4", "version": "2.0.5",
"resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.4.tgz", "resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.5.tgz",
"integrity": "sha512-ERg+H//lSSYlZhBIUu+wJnqg30AbyBbpZlIhcshpn7BNzpoRODZgfyr9J+8ERf3ooC6af3u7Lcl01nleau7MrA==", "integrity": "sha512-xrQcmYhOsn/1kX+Vraq+7j4oE2j/6BFscZ0etmYg81xuM8Gq0022Pxb8+IqgOFUIaxHs0KaSb7T1+OegiNrNFA==",
"dev": true, "dev": true,
"requires": { "requires": {
"escape-string-regexp": "^2.0.0", "escape-string-regexp": "^2.0.0"
"source-map-support": "^0.5.20"
} }
}, },
"statuses": { "statuses": {

View File

@ -1,5 +1,14 @@
import "reflect-metadata"; import "reflect-metadata";
import { BaseEntity, BeforeInsert, BeforeUpdate, EntityMetadata, FindConditions, PrimaryColumn } from "typeorm"; import {
BaseEntity,
BeforeInsert,
BeforeUpdate,
EntityMetadata,
FindConditions,
getConnection,
PrimaryColumn,
RemoveOptions,
} from "typeorm";
import { Snowflake } from "../util/Snowflake"; import { Snowflake } from "../util/Snowflake";
import "missing-native-js-functions"; import "missing-native-js-functions";
@ -69,6 +78,44 @@ export class BaseClassWithoutId extends BaseEntity {
const repository = this.getRepository(); const repository = this.getRepository();
return repository.decrement(conditions, propertyPath, value); return repository.decrement(conditions, propertyPath, value);
} }
static async delete<T>(criteria: FindConditions<T>, options?: RemoveOptions) {
if (!criteria) throw new Error("You need to specify delete criteria");
const repository = this.getRepository();
const promises = repository.metadata.relations.map((x) => {
if (x.orphanedRowAction !== "delete") return;
if (typeof x.type === "string") return;
const foreignKey =
x.foreignKeys.find((key) => key.entityMetadata === repository.metadata) ||
x.inverseRelation?.foreignKeys[0]; // find foreign key for this entity
if (!foreignKey) {
throw new Error(
`Foreign key not found for entity ${repository.metadata.name} in relation ${x.propertyName}`
);
}
console.log(foreignKey);
const id = (criteria as any)[foreignKey.referencedColumnNames[0]];
if (!id) throw new Error("id missing in criteria options");
if (x.relationType === "many-to-many" || x.relationType === "one-to-many") {
return getConnection()
.createQueryBuilder()
.relation(this, x.propertyName)
.of(id)
.remove({ [foreignKey.columnNames[0]]: id });
} else if (x.relationType === "one-to-one" || x.relationType === "many-to-one") {
return getConnection()
.createQueryBuilder()
.from(x.inverseEntityMetadata, "user")
.of(id)
.remove({ [foreignKey.name]: id });
}
});
await Promise.all(promises);
return super.delete(criteria, options);
}
} }
export class BaseClass extends BaseClassWithoutId { export class BaseClass extends BaseClassWithoutId {

View File

@ -1,14 +1,4 @@
import { import { Column, Entity, JoinColumn, ManyToOne, OneToMany, RelationId } from "typeorm";
Column,
Entity,
FindConditions,
JoinColumn,
ManyToOne,
ObjectID,
OneToMany,
RelationId,
RemoveOptions,
} from "typeorm";
import { BaseClass } from "./BaseClass"; import { BaseClass } from "./BaseClass";
import { Guild } from "./Guild"; import { Guild } from "./Guild";
import { PublicUserProjection, User } from "./User"; import { PublicUserProjection, User } from "./User";

View File

@ -21,7 +21,7 @@ export function initDatabase() {
// //
entities: Object.values(Models).filter((x) => x.constructor.name !== "Object"), entities: Object.values(Models).filter((x) => x.constructor.name !== "Object"),
synchronize: true, synchronize: true,
logging: false, logging: true,
cache: { cache: {
duration: 1000 * 3, // cache all find queries for 3 seconds duration: 1000 * 3, // cache all find queries for 3 seconds
}, },