good enough

This commit is contained in:
May 2024-05-22 19:40:12 +00:00 committed by GitHub
parent 037684094a
commit 8f13bf2fea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 69 additions and 68 deletions

68
src/server/lib/codes.ts Normal file
View file

@ -0,0 +1,68 @@
import { generateFileId } from "./files.js";
export const Intents = ["verifyEmail", "recoverAccount", "deletionOtp"] as const
export type Intent = (typeof Intents)[number]
export function isIntent(intent: string): intent is Intent {
return intent in Intents
}
export let codes = Object.fromEntries(
Intents.map((e) => [
e,
{
byId: new Map<string, Code>(),
byUser: new Map<string, Code[]>(),
},
])
) as Record<
Intent,
{ byId: Map<string, Code>; byUser: Map<string, Code[]> }
>
// this is stupid whyd i write this
export class Code {
readonly id: string = generateFileId(12)
readonly for: string
readonly intent: Intent
readonly expiryClear: NodeJS.Timeout
readonly data: any
constructor(
intent: Intent,
forUser: string,
data?: any,
time: number = 15 * 60 * 1000
) {
this.for = forUser
this.intent = intent
this.expiryClear = setTimeout(this.terminate.bind(this), time)
this.data = data
codes[intent].byId.set(this.id, this)
let byUser = codes[intent].byUser.get(this.for)
if (!byUser) {
byUser = []
codes[intent].byUser.set(this.for, byUser)
}
byUser.push(this)
}
terminate() {
codes[this.intent].byId.delete(this.id)
let bu = codes[this.intent].byUser.get(this.id)!
bu.splice(bu.indexOf(this), 1)
clearTimeout(this.expiryClear)
}
check(forUser: string) {
return forUser === this.for
}
}

View file

@ -37,71 +37,4 @@ export function sendMail(to: string, subject: string, content: string) {
}) })
} }
export namespace CodeMgr { export * as CodeMgr from "./codes.js"
export const Intents = ["verifyEmail", "recoverAccount"] as const
export type Intent = (typeof Intents)[number]
export function isIntent(intent: string): intent is Intent {
return intent in Intents
}
export let codes = Object.fromEntries(
Intents.map((e) => [
e,
{
byId: new Map<string, Code>(),
byUser: new Map<string, Code[]>(),
},
])
) as Record<
Intent,
{ byId: Map<string, Code>; byUser: Map<string, Code[]> }
>
// this is stupid whyd i write this
export class Code {
readonly id: string = generateFileId(12)
readonly for: string
readonly intent: Intent
readonly expiryClear: NodeJS.Timeout
readonly data: any
constructor(
intent: Intent,
forUser: string,
data?: any,
time: number = 15 * 60 * 1000
) {
this.for = forUser
this.intent = intent
this.expiryClear = setTimeout(this.terminate.bind(this), time)
this.data = data
codes[intent].byId.set(this.id, this)
let byUser = codes[intent].byUser.get(this.for)
if (!byUser) {
byUser = []
codes[intent].byUser.set(this.for, byUser)
}
byUser.push(this)
}
terminate() {
codes[this.intent].byId.delete(this.id)
let bu = codes[this.intent].byUser.get(this.id)!
bu.splice(bu.indexOf(this), 1)
clearTimeout(this.expiryClear)
}
check(forUser: string) {
return forUser === this.for
}
}
}