diff --git a/src/helpers/usettings.ts b/src/helpers/usettings.ts index c4d06cb..14626a1 100644 --- a/src/helpers/usettings.ts +++ b/src/helpers/usettings.ts @@ -1,5 +1,7 @@ +import { project } from "@/utility"; import { readFileSync, writeFileSync } from "node:fs"; -interface IUSettings { + +export interface IUSettings { keyrooms: string[]; rooms: string[]; cleanThings: string[]; @@ -24,7 +26,7 @@ class UOptions { return this._settings; } public set settings(value: IUSettings) { - this._settings = value; + this._settings = project(value, ['cleanThings', 'keyrooms', 'menu', 'rooms', 'security']) as typeof value this.save() } diff --git a/src/routes/api/admin/settings.ts b/src/routes/api/admin/settings.ts index 15bc750..6b2192a 100644 --- a/src/routes/api/admin/settings.ts +++ b/src/routes/api/admin/settings.ts @@ -1,5 +1,5 @@ import { Router } from "express"; -import { adminPerm, Perms, project } from "@/utility"; +import { adminPerm, Perms } from "@/utility"; import usettings from "@/helpers/usettings"; export const settingsRouter = Router() @@ -11,7 +11,7 @@ settingsRouter.get('/', (req, res) => { }) settingsRouter.post('/', (req, res) => { - usettings.settings = project(req.body, {keyrooms: true, cleanThings: true, rooms: true, menu: true, security: true}) + usettings.settings = req.body res.send({status: 200}) }) diff --git a/src/utility.ts b/src/utility.ts index 7705c89..cf676e7 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -38,12 +38,18 @@ var adminCond = (adminInt = 0, perm: Perms) => { return (adminInt & perm) == perm } -var project = (obj: any, projection: any) => { - let obj2: any = {} - for (let key in projection) { - if (key in obj) obj2[key] = obj[key] +export function project(obj: T | any, projection: (keyof T)[] | { [key in keyof T]: any}): Partial { + let obj2: Partial = {} + if (projection instanceof Array) { + for (let key of projection) { + if (key in obj) obj2[key] = obj[key] + } + } else { + for (let key in projection) { + if (key in obj) obj2[key] = obj[key] + } } return obj2 } -export {islogged, isadmin, adminPerm, Perms, adminCond, project}; \ No newline at end of file +export {islogged, isadmin, adminPerm, Perms, adminCond}; \ No newline at end of file