mirror of
https://git.sr.ht/~roxwize/mipilin
synced 2025-05-10 23:33:07 +00:00
invite cooooddeesss
Signed-off-by: roxwize <rae@roxwize.xyz>
This commit is contained in:
parent
7b563f5c31
commit
5abe0b5fad
22 changed files with 2326 additions and 16 deletions
60
routes/admin.ts
Normal file
60
routes/admin.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
||||
import { Express } from "express";
|
||||
import { createInviteCode, render } from "./util.js";
|
||||
import { inviteCodes, users } from "../db/schema.js";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
export default function (app: Express, db: NodePgDatabase) {
|
||||
app.get("/mod", async (req, res) => {
|
||||
if (!req.session["loggedIn"] || !req.session["moderator"]) {
|
||||
res.redirect("/");
|
||||
return;
|
||||
}
|
||||
|
||||
const now = dayjs();
|
||||
const codes = (
|
||||
await db
|
||||
.select({ expires: inviteCodes.expires, token: inviteCodes.token, uname: users.name })
|
||||
.from(inviteCodes)
|
||||
.leftJoin(users, eq(inviteCodes.user, users.id))
|
||||
.orderBy(desc(inviteCodes.granted))
|
||||
).map((e) => {
|
||||
return {
|
||||
expires: e.expires,
|
||||
token: e.token,
|
||||
uname: e.uname,
|
||||
expiresString: now.to(dayjs(e.expires))
|
||||
};
|
||||
});
|
||||
render(db, "admin", "Admin Panel", res, req, { codes });
|
||||
});
|
||||
|
||||
app.post("/mod/codes/delete", async (req, res) => {
|
||||
if (!req.session["loggedIn"] || !req.session["moderator"]) {
|
||||
res.redirect("/");
|
||||
return;
|
||||
}
|
||||
|
||||
await db.delete(inviteCodes).where(eq(inviteCodes.token, req.body.token));
|
||||
req.flash("success", "Deleted.");
|
||||
res.redirect("/mod");
|
||||
})
|
||||
app.post("/mod/codes/create", async (req, res) => {
|
||||
if (!req.session["loggedIn"] || !req.session["moderator"]) {
|
||||
res.redirect("/");
|
||||
return;
|
||||
}
|
||||
|
||||
const expiration = new Date(req.body.expiration || 0);
|
||||
if (req.body.expiration && expiration.getTime() <= Date.now()) {
|
||||
req.flash("error", "Chosen expiration date is in the past.");
|
||||
res.redirect("/mod");
|
||||
return;
|
||||
}
|
||||
const code = await createInviteCode(db, req.session["uid"], expiration);
|
||||
|
||||
req.flash("success", `Your code has been created as <b>${code}</b>.`);
|
||||
res.redirect("/mod");
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue