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
|
@ -1,7 +1,7 @@
|
|||
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
||||
import type { Request, Response } from "express";
|
||||
import { updates } from "../db/schema.js";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import { inviteCodes, updates } from "../db/schema.js";
|
||||
import { count, desc, eq } from "drizzle-orm";
|
||||
import fs from "node:fs/promises";
|
||||
|
||||
const nonceChars =
|
||||
|
@ -62,3 +62,29 @@ export async function render(
|
|||
};
|
||||
res.render(page, { ...o, ...stuff });
|
||||
}
|
||||
|
||||
const inviteCodeChars = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
export async function createInviteCode(db: NodePgDatabase, user: number, expires: Date, confersModerator = false) {
|
||||
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,
|
||||
confersModerator
|
||||
});
|
||||
return token;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue