From 2dc447a446f785823cfe038bd46a20c3b53f247d Mon Sep 17 00:00:00 2001 From: Jan Szumotalski Date: Mon, 5 May 2025 18:54:26 +0200 Subject: [PATCH] fix: Changed group assignment. --- src/index.ts | 2 +- src/notif.ts | 2 +- src/pipelines/notif.ts | 65 +----------------------------------- src/routes/api/admin/accs.ts | 12 ++++--- src/schemas/Group.ts | 6 +--- src/schemas/User.ts | 10 +++--- 6 files changed, 17 insertions(+), 80 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3eeccee..58b8226 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,7 +25,7 @@ declare global { uname: string; admin?: number; locked?: boolean; - room?: number + room?: string } } } diff --git a/src/notif.ts b/src/notif.ts index f8786f1..d61d4a5 100644 --- a/src/notif.ts +++ b/src/notif.ts @@ -45,7 +45,7 @@ export class NotifcationHelper { return await this.send(message, await Notification.aggregate(roomNotif(room))) }, group: async (group: string) => { - return await this.send(message, await Notification.aggregate(groupNotif(group))) + return await this.send(message, []) }, withRoom: async () => { return await this.send(message, await Notification.aggregate(allNotif())) diff --git a/src/pipelines/notif.ts b/src/pipelines/notif.ts index ba5e390..143a6f6 100644 --- a/src/pipelines/notif.ts +++ b/src/pipelines/notif.ts @@ -82,70 +82,7 @@ function allNotif() { } function groupNotif(group: string) { - var pipeline: PipelineStage[] = [ - { - $match: - { - _id: new Types.ObjectId(group) - } - }, - { - $graphLookup: - { - from: "logins", - startWith: "$rooms", - connectFromField: "rooms", - connectToField: "room", - as: "logins", - }, - }, - { - $set: { - unames: { - $function: { - body: "function (arg, arg2) { if (!arg2) arg2 = []; return [...arg2,...arg.map((s) => s.uname)];}", - args: ["$logins", "$unames"], - lang: "js", - }, - }, - }, - }, - { - $unwind: - { - path: "$unames", - }, - }, - { - $graphLookup: - { - from: "notifications", - startWith: "$unames", - connectFromField: "unames", - connectToField: "uname", - as: "notif", - }, - }, - { - $project: - { - notif: 1, - }, - }, - { - $unwind: - { - path: "$notif", - }, - }, - { - $replaceRoot: - { - newRoot: "$notif", - }, - }, - ] - return pipeline + return } export { userNotif, roomNotif, allNotif, groupNotif } \ No newline at end of file diff --git a/src/routes/api/admin/accs.ts b/src/routes/api/admin/accs.ts index 95c6e7a..d692231 100644 --- a/src/routes/api/admin/accs.ts +++ b/src/routes/api/admin/accs.ts @@ -1,17 +1,19 @@ import User from "@schemas/User"; import { Router } from "express" import { Perms, adminCond, adminPerm } from "@/utility"; +import capability from "@/capability"; +import Group from "@/schemas/Group"; const accsRouter = Router() accsRouter.use(adminPerm(Perms.Accs)) accsRouter.get('/', async (req, res)=> { - if (req.user.admin) { - res.send(await User.find({"uname": {"$ne": req.user.uname}}, {pass: 0})) - return + var data = { + users: await User.find({"uname": {"$ne": req.user.uname}}, {pass: 0}), + groups: capability.settings.groups ? await Group.find() : undefined } - res.send(await User.find({"uname": {"$ne": req.user.uname}}, {pass: 0, admin: 0})) + res.send(data) }) accsRouter.post('/', async (req, res)=> { @@ -45,7 +47,7 @@ accsRouter.put('/:id', async (req, res)=> { if (adminCond(req.body.flags, Perms.Superadmin)) { res.status(400).send("Cannot set superadmin") } else { - await user.set({uname: req.body.uname, room: req.body.room, admin: req.body.flags, fname: req.body.fname, surname: req.body.surname}).save() + await user.set({uname: req.body.uname, room: req.body.room, admin: req.body.flags, fname: req.body.fname, surname: req.body.surname, groups: req.body.groups}).save() res.send({status: 200}) } } diff --git a/src/schemas/Group.ts b/src/schemas/Group.ts index 6cc4411..4db1b38 100644 --- a/src/schemas/Group.ts +++ b/src/schemas/Group.ts @@ -2,14 +2,10 @@ import { ObjectId, Schema, model } from "mongoose" interface IGroup { name: string; - rooms?: number[]; - unames?: string[]; } const groupSchema = new Schema({ - name: {type: String, required: true}, - rooms: [Schema.Types.Number], - unames: [Schema.Types.String] + name: {type: String, required: true} }) export default model("group", groupSchema) \ No newline at end of file diff --git a/src/schemas/User.ts b/src/schemas/User.ts index 4d40073..a5e7fcb 100644 --- a/src/schemas/User.ts +++ b/src/schemas/User.ts @@ -1,25 +1,27 @@ -import mongoose, { Schema } from "mongoose" +import mongoose, { ObjectId, Schema } from "mongoose" // TODO: Unify `fname` and `surename` into single field interface IUser { uname: string; pass: string; - room?: number; + room?: string; admin?: number; locked?: boolean; fname?: string; surname?: string; + groups: ObjectId[]; } const userSchema = new Schema({ uname: {type: String, required: true}, pass: {type: String, required: true, default: "$2y$10$wxDhf.XiXkmdKrFqYUEa0.F4Bf.pDykZaMmgjvyLyeRP3E/Xy0hbC"}, - room: Number, + room: String, admin: Number, locked: {type: Boolean, default: false}, fname: String, - surname: String + surname: String, + groups: [{type: mongoose.Types.ObjectId, ref: "Group"}] }) export default mongoose.model("logins", userSchema) \ No newline at end of file