1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-10 20:52:42 +01:00

🚧 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 { handleFile, route } from "@fosscord/api";
import { route } from "@fosscord/api";
const router: Router = Router();
// 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"] });
if (channel.type === ChannelType.DM) {
const recipient = await Recipient.findOneOrFail({ where: { channel_id: channel_id, user_id: req.user_id } })
recipient.closed = true
const recipient = await Recipient.findOneOrFail({ where: { channel_id: channel_id, user_id: req.user_id } });
recipient.closed = true;
await Promise.all([
recipient.save(),
emitEvent({ event: "CHANNEL_DELETE", data: channel, user_id: req.user_id } as ChannelDeleteEvent)
]);
} else if (channel.type === ChannelType.GROUP_DM) {
await Channel.removeRecipientFromChannel(channel, req.user_id)
await Channel.removeRecipientFromChannel(channel, req.user_id);
} else {
//TODO messages in this channel should be deleted before deleting the channel
await Promise.all([

98
util/package-lock.json generated
View File

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

View File

@ -1,5 +1,14 @@
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 "missing-native-js-functions";
@ -69,6 +78,44 @@ export class BaseClassWithoutId extends BaseEntity {
const repository = this.getRepository();
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 {

View File

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

View File

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