From 76d314ec00fe3922f67799cdefde0c284ef08657 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Thu, 6 Apr 2023 13:55:21 +0200 Subject: [PATCH] auto delete accounts after set time --- src/api/acts/client.ts | 1 + src/main.ts | 4 ++++ src/server/checks.ts | 17 +++++++++++++++++ src/setup/config.ts | 10 ++++++++-- src/sys/config.ts | 3 ++- src/sys/db.ts | 5 ++++- 6 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/server/checks.ts diff --git a/src/api/acts/client.ts b/src/api/acts/client.ts index ba90db9..e406416 100644 --- a/src/api/acts/client.ts +++ b/src/api/acts/client.ts @@ -18,6 +18,7 @@ export const deleteAccount: Act = { client.state = STATE.no; await update(accounts) .set(accounts.deleted, 1) + .set(accounts.deletedTime, uts()) .where(eq(accounts.accID, client.accID)) .query(db); aws("ok", ""); diff --git a/src/main.ts b/src/main.ts index 8ad318f..fe9f389 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,6 +20,7 @@ import { activatePost, addPostMethods } from "./api/post.js"; import { activateWS, closeWebSocket, wsOnConnection } from "./api/ws.js"; import { startUpdateCert } from "./server/serverCerts.js"; import { wait } from "./sys/tools.js"; +import { startChecks } from "./server/checks.js"; global.provideLog = 10; @@ -70,6 +71,8 @@ nman.addShutdownTask(() => new Promise(async (res, rej) => { if (serverclose != null) { serverclose.keepAliveTimeout = 100; serverclose.close(() => res()); + } else { + res(); } }), 10000); @@ -125,5 +128,6 @@ async function complete_loaded() { } activatePost(); activateWS(); + startChecks(); log("Server", 'Listening...'); } \ No newline at end of file diff --git a/src/server/checks.ts b/src/server/checks.ts new file mode 100644 index 0000000..6c99ff3 --- /dev/null +++ b/src/server/checks.ts @@ -0,0 +1,17 @@ +import { and, le, remove } from "dblang"; +import { addShutdownTask } from "nman"; +import { accounts, db } from "../sys/db.js" +import { uts } from "../sys/tools.js"; +import { oConf } from "../sys/config.js"; + +export function startChecks() { + let i1 = setInterval(async () => { + await remove(accounts) + .where(and( + accounts.deleted, + le(accounts.deletedTime, uts() - oConf.get("Settings", "deletedAccountKeepSec")) + )).query(db); + }, 60000); + + addShutdownTask(() => clearInterval(i1), 500); +} \ No newline at end of file diff --git a/src/setup/config.ts b/src/setup/config.ts index f53fe64..1c65776 100644 --- a/src/setup/config.ts +++ b/src/setup/config.ts @@ -256,8 +256,12 @@ const outbag = async () => { name: "defaultViewable", message: "Should outbag accounts be viewable by default?", initial: oConf.get("Settings", "defaultViewable"), - }, - + }, { + type: "number", + name: "deletedAccountKeepSec", + message: "Please enter the number of days a deleted Account is being save until final deletion!", + initial: oConf.get("Settings", "deletedAccountKeepSec") / 60 / 60 / 24 + } ]); if ( resp.maxUsers == null @@ -265,6 +269,7 @@ const outbag = async () => { || resp.defaultMaxRoomSize == null || resp.defaultMaxUsersPerRoom == null || resp.defaultViewable == null + || resp.deletedAccountKeepSec == null ) { warn("Setup", "You have to provide all informations!"); return false; @@ -285,6 +290,7 @@ const outbag = async () => { oConf.set("Settings", "defaultMaxRoomSize", resp.defaultMaxRoomSize); oConf.set("Settings", "defaultMaxUsersPerRoom", resp.defaultMaxUsersPerRoom); oConf.set("Settings", "defaultViewable", resp.defaultViewable); + oConf.set("Settings", "deletedAccountKeepSec", Math.round(resp.deletedAccountKeepSec * 60 * 60 * 24)); oConf.save(); return true; diff --git a/src/sys/config.ts b/src/sys/config.ts index 2544532..b3df7fc 100644 --- a/src/sys/config.ts +++ b/src/sys/config.ts @@ -26,7 +26,8 @@ const conf_struct = { defaultMaxRooms: { type: "number", default: 3, env: "OUTBAG_DEFAULT_MAX_ROOMS" },//Infinity = -1 defaultMaxRoomSize: { type: "number", default: 10000, env: "OUTBAG_DEFAULT_MAX_ROOMS_SIZE" },//Infinity = -1 defaultMaxUsersPerRoom: { type: "number", default: 5, env: "OUTBAG_DEFAULT_MAX_USERS_PER_ROOM" },//Infinity = -1 - defaultViewable: { type: "boolean", default: false, env: "OUTBAG_DEFAULT_VIEWABLE" } + defaultViewable: { type: "boolean", default: false, env: "OUTBAG_DEFAULT_VIEWABLE" }, + deletedAccountKeepSec: { type: "number", default: 60 * 60 * 24 * 20, env: "OUTBAG_DEL_ACCOUNT_KEEP_SEC" }, } }; diff --git a/src/sys/db.ts b/src/sys/db.ts index d2230d6..9e8143a 100644 --- a/src/sys/db.ts +++ b/src/sys/db.ts @@ -58,6 +58,7 @@ accounts.addAttributes({ viewable: { type: BOOL, default: false }, deleted: { type: BOOL, default: false }, + deletedTime: { type: BIGINT, default: 0 }, maxRooms: { type: INT }, maxRoomSize: { type: INT }, @@ -117,7 +118,9 @@ rooms.addAttributes({ owner: { type: INT, foreignKey: { - link: accounts.accID + link: accounts.accID, + onDelete: onAction.cascade, + onUpdate: onAction.cascade } }, rights: { type: INT, default: 0b11111 },