mirror of
https://github.com/mollersuite/monofile.git
synced 2024-11-22 05:46:26 -08:00
good enough
This commit is contained in:
parent
037684094a
commit
8f13bf2fea
68
src/server/lib/codes.ts
Normal file
68
src/server/lib/codes.ts
Normal 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
|
||||
}
|
||||
}
|
|
@ -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<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
|
||||
}
|
||||
}
|
||||
}
|
||||
export * as CodeMgr from "./codes.js"
|
Loading…
Reference in a new issue