mirror of
https://git.sr.ht/~roxwize/mipilin
synced 2025-05-10 23:33:07 +00:00
:(
Signed-off-by: roxwize <rae@roxwize.xyz>
This commit is contained in:
parent
afc634b0d2
commit
d173e6ea73
22 changed files with 1433 additions and 472 deletions
165
routes/util.ts
165
routes/util.ts
|
@ -5,92 +5,113 @@ import { count, desc, eq } from "drizzle-orm";
|
|||
import fs from "node:fs/promises";
|
||||
|
||||
export enum UserStatus {
|
||||
MODERATOR = 0b001,
|
||||
BANNED = 0b010,
|
||||
TRUSTED = 0b100
|
||||
};
|
||||
MODERATOR = 0b001,
|
||||
BANNED = 0b010,
|
||||
TRUSTED = 0b100
|
||||
}
|
||||
|
||||
const nonceChars =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_";
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_";
|
||||
let nonce: string;
|
||||
export function setNonce() {
|
||||
nonce = "";
|
||||
for (let i = 0; i < 32; i++)
|
||||
nonce += nonceChars[Math.floor(Math.random() * nonceChars.length)];
|
||||
return nonce;
|
||||
nonce = "";
|
||||
for (let i = 0; i < 32; i++)
|
||||
nonce += nonceChars[Math.floor(Math.random() * nonceChars.length)];
|
||||
return nonce;
|
||||
}
|
||||
export function getNonce() {
|
||||
if (!nonce)
|
||||
throw new Error("Nonce doesn't exist");
|
||||
return nonce;
|
||||
if (!nonce) throw new Error("Nonce doesn't exist");
|
||||
return nonce;
|
||||
}
|
||||
|
||||
let moods: string[], moodsSorted: string[];
|
||||
export async function getMoods() {
|
||||
if (!moods)
|
||||
moods = (await fs.readFile("./static/moods.txt"))
|
||||
.toString("utf-8")
|
||||
.split(";");
|
||||
if (!moodsSorted) moodsSorted = Array.from(moods).sort();
|
||||
return { moods, moodsSorted };
|
||||
if (!moods)
|
||||
moods = (await fs.readFile("./static/moods.txt"))
|
||||
.toString("utf-8")
|
||||
.split(";");
|
||||
if (!moodsSorted) moodsSorted = Array.from(moods).sort();
|
||||
return { moods, moodsSorted };
|
||||
}
|
||||
|
||||
export async function render(
|
||||
db: NodePgDatabase,
|
||||
page: string,
|
||||
title: string,
|
||||
res: Response,
|
||||
req: Request,
|
||||
stuff?: Object
|
||||
db: NodePgDatabase,
|
||||
page: string,
|
||||
title: string,
|
||||
res: Response,
|
||||
req: Request,
|
||||
stuff?: Object
|
||||
) {
|
||||
//? maybe you should cache this and save the current mood to the session until it's changed
|
||||
const { moods } = await getMoods();
|
||||
let currentMood: string;
|
||||
if (req.session["loggedIn"]) {
|
||||
const update = (
|
||||
await db
|
||||
.select({ mood: updates.mood })
|
||||
.from(updates)
|
||||
.where(eq(updates.user, req.session["uid"]))
|
||||
.orderBy(desc(updates.date))
|
||||
.limit(1)
|
||||
)[0];
|
||||
currentMood = moods[update?.mood];
|
||||
}
|
||||
|
||||
const o = {
|
||||
title,
|
||||
session: req.session,
|
||||
flashes: req.flash(),
|
||||
moods,
|
||||
currentMood,
|
||||
nonce
|
||||
};
|
||||
res.render(page, { ...o, ...stuff });
|
||||
}
|
||||
|
||||
const inviteCodeChars = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
export async function createInviteCode(db: NodePgDatabase, user: number, expires: Date, confers = 0) {
|
||||
let existingToken = 1, token: string;
|
||||
while (existingToken) {
|
||||
token = user.toString().padStart(4, "0") + "-"
|
||||
for (let i = 0; i < 17; i++) {
|
||||
if ((i + 1) % 6 === 0) {
|
||||
token += "-";
|
||||
continue;
|
||||
}
|
||||
token += inviteCodeChars[Math.floor(Math.random() * inviteCodeChars.length)];
|
||||
//? maybe you should cache this and save the current mood to the session until it's changed
|
||||
const { moods } = await getMoods();
|
||||
let currentMood: string;
|
||||
if (req.session["loggedIn"]) {
|
||||
const update = (
|
||||
await db
|
||||
.select({ mood: updates.mood })
|
||||
.from(updates)
|
||||
.where(eq(updates.user, req.session["uid"]))
|
||||
.orderBy(desc(updates.date))
|
||||
.limit(1)
|
||||
)[0];
|
||||
currentMood = moods[update?.mood];
|
||||
}
|
||||
existingToken = (await db.select({ value: count() }).from(inviteCodes).where(eq(inviteCodes.token, token)))[0].value;
|
||||
}
|
||||
|
||||
//@ts-expect-error
|
||||
await db.insert(inviteCodes).values({
|
||||
token,
|
||||
user: user || undefined,
|
||||
granted: new Date(Date.now()),
|
||||
expires,
|
||||
confers
|
||||
});
|
||||
return token;
|
||||
const o = {
|
||||
title,
|
||||
session: req.session,
|
||||
flashes: req.flash(),
|
||||
moods,
|
||||
currentMood,
|
||||
nonce
|
||||
};
|
||||
res.render(page, { ...o, ...stuff });
|
||||
}
|
||||
export async function render404(
|
||||
db: NodePgDatabase,
|
||||
res: Response,
|
||||
req: Request
|
||||
) {
|
||||
res.statusCode = 404;
|
||||
render(db, "404", "not found", res, req);
|
||||
}
|
||||
|
||||
const inviteCodeChars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
export async function createInviteCode(
|
||||
db: NodePgDatabase,
|
||||
user: number,
|
||||
expires: Date,
|
||||
confers = 0
|
||||
) {
|
||||
let existingToken = 1,
|
||||
token: string;
|
||||
while (existingToken) {
|
||||
token = user.toString().padStart(4, "0") + "-";
|
||||
for (let i = 0; i < 17; i++) {
|
||||
if ((i + 1) % 6 === 0) {
|
||||
token += "-";
|
||||
continue;
|
||||
}
|
||||
token +=
|
||||
inviteCodeChars[
|
||||
Math.floor(Math.random() * inviteCodeChars.length)
|
||||
];
|
||||
}
|
||||
existingToken = (
|
||||
await db
|
||||
.select({ value: count() })
|
||||
.from(inviteCodes)
|
||||
.where(eq(inviteCodes.token, token))
|
||||
)[0].value;
|
||||
}
|
||||
|
||||
//@ts-expect-error
|
||||
await db.insert(inviteCodes).values({
|
||||
token,
|
||||
user: user || undefined,
|
||||
granted: new Date(Date.now()),
|
||||
expires,
|
||||
confers
|
||||
});
|
||||
return token;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue