diff --git a/src/server/lib/codes.ts b/src/server/lib/codes.ts new file mode 100644 index 0000000..715b124 --- /dev/null +++ b/src/server/lib/codes.ts @@ -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(), + byUser: new Map(), + }, + ]) +) as Record< + Intent, + { byId: Map; byUser: Map } +> + +// 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 + } +} \ No newline at end of file diff --git a/src/server/lib/mail.ts b/src/server/lib/mail.ts index ee5899f..9f24098 100644 --- a/src/server/lib/mail.ts +++ b/src/server/lib/mail.ts @@ -37,71 +37,4 @@ export function sendMail(to: string, subject: string, content: string) { }) } -export namespace CodeMgr { - 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(), - byUser: new Map(), - }, - ]) - ) as Record< - Intent, - { byId: Map; byUser: Map } - > - - // 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 - } - } -} +export * as CodeMgr from "./codes.js" \ No newline at end of file