From ba03c6a2f798f39171b0ba9f19f1e26cf86ba4ff Mon Sep 17 00:00:00 2001 From: Grant Date: Fri, 12 Jul 2024 19:29:06 -0600 Subject: [PATCH] [admin] Implement user notices --- packages/server/src/api/admin.ts | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/packages/server/src/api/admin.ts b/packages/server/src/api/admin.ts index eb101a4..f3a2661 100644 --- a/packages/server/src/api/admin.ts +++ b/packages/server/src/api/admin.ts @@ -590,6 +590,95 @@ app.delete("/user/:sub/ban", async (req, res) => { res.json({ success: true, auditLog }); }); +/** + * Send notice to every connected socket + * + * @body title string + * @body body string? + */ +app.post("/user/all/notice", async (req, res) => { + let title: string = req.body.title; + + if (typeof req.body.title !== "string") { + return res + .status(400) + .json({ success: false, error: "Title is not a string" }); + } + + if ( + typeof req.body.body !== "undefined" && + typeof req.body.body !== "string" + ) { + return res + .status(400) + .json({ success: false, error: "Body is set but is not a string" }); + } + + const sockets = await SocketServer.instance.io.fetchSockets(); + + for (const socket of sockets) { + socket.emit("alert", { + is: "modal", + action: "moderation", + dismissable: true, + title, + body: req.body.body, + }); + } + + res.json({ success: true }); +}); + +/** + * Send notice to user + * + * @param :sub User id + * @body title string + * @body body string? + */ +app.post("/user/:sub/notice", async (req, res) => { + let user: User; + + try { + user = await User.fromSub(req.params.sub); + } catch (e) { + if (e instanceof UserNotFound) { + res.status(404).json({ success: false, error: "User not found" }); + } else { + Logger.error(`/user/${req.params.sub}/ban Error ` + (e as any)?.message); + res.status(500).json({ success: false, error: "Internal error" }); + } + return; + } + + let title: string = req.body.title; + + if (typeof req.body.title !== "string") { + return res + .status(400) + .json({ success: false, error: "Title is not a string" }); + } + + if ( + typeof req.body.body !== "undefined" && + typeof req.body.body !== "string" + ) { + return res + .status(400) + .json({ success: false, error: "Body is set but is not a string" }); + } + + user.notify({ + is: "modal", + action: "moderation", + dismissable: true, + title, + body: req.body.body, + }); + + res.json({ success: true }); +}); + app.get("/instance/:domain/ban", async (req, res) => { // get ban information