2023-01-12 21:40:33 -07:00
|
|
|
import { validate as validateEmail } from "deep-email-validator";
|
|
|
|
import { UserProfiles } from "@/models/index.js";
|
|
|
|
import { fetchMeta } from "@/misc/fetch-meta.js";
|
2021-11-07 04:16:01 -07:00
|
|
|
|
|
|
|
export async function validateEmailForAccount(emailAddress: string): Promise<{
|
|
|
|
available: boolean;
|
2023-01-12 21:40:33 -07:00
|
|
|
reason: null | "used" | "format" | "disposable" | "mx" | "smtp";
|
2021-11-07 04:16:01 -07:00
|
|
|
}> {
|
2022-07-09 00:05:55 -06:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
2022-03-26 00:34:00 -06:00
|
|
|
const exist = await UserProfiles.countBy({
|
2021-11-07 04:16:01 -07:00
|
|
|
emailVerified: true,
|
|
|
|
email: emailAddress,
|
|
|
|
});
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
const validated = meta.enableActiveEmailValidation
|
|
|
|
? await validateEmail({
|
|
|
|
email: emailAddress,
|
|
|
|
validateRegex: true,
|
|
|
|
validateMx: true,
|
|
|
|
validateTypo: false, // TLDを見ているみたいだけどclubとか弾かれるので
|
|
|
|
validateDisposable: true, // 捨てアドかどうかチェック
|
|
|
|
validateSMTP: false, // 日本だと25ポートが殆どのプロバイダーで塞がれていてタイムアウトになるので
|
|
|
|
})
|
|
|
|
: { valid: true };
|
2021-11-07 04:16:01 -07:00
|
|
|
|
|
|
|
const available = exist === 0 && validated.valid;
|
|
|
|
|
|
|
|
return {
|
|
|
|
available,
|
2023-01-12 21:40:33 -07:00
|
|
|
reason: available
|
|
|
|
? null
|
|
|
|
: exist !== 0
|
|
|
|
? "used"
|
|
|
|
: validated.reason === "regex"
|
|
|
|
? "format"
|
|
|
|
: validated.reason === "disposable"
|
|
|
|
? "disposable"
|
|
|
|
: validated.reason === "mx"
|
|
|
|
? "mx"
|
|
|
|
: validated.reason === "smtp"
|
|
|
|
? "smtp"
|
|
|
|
: null,
|
2021-11-07 04:16:01 -07:00
|
|
|
};
|
|
|
|
}
|