From 4d027a424e442f10371d205e68aa3bf63d0eba0e Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Fri, 8 Sep 2023 21:33:04 -0400 Subject: Implement Pomelo Registration --- src/util/entities/User.ts | 40 ++++++++++++++++++++++ src/util/schemas/UsernameAttemptUnauthedSchema.ts | 3 ++ src/util/schemas/index.ts | 1 + .../schemas/responses/UsernameAttemptResponse.ts | 3 ++ 4 files changed, 47 insertions(+) create mode 100644 src/util/schemas/UsernameAttemptUnauthedSchema.ts create mode 100644 src/util/schemas/responses/UsernameAttemptResponse.ts (limited to 'src/util') diff --git a/src/util/entities/User.ts b/src/util/entities/User.ts index d1fbb5c2..5ec9862e 100644 --- a/src/util/entities/User.ts +++ b/src/util/entities/User.ts @@ -344,6 +344,7 @@ export class User extends BaseClass { password, id, req, + global_name, }: { username: string; password?: string; @@ -351,8 +352,10 @@ export class User extends BaseClass { date_of_birth?: Date; // "2000-04-03" id?: string; req?: Request; + global_name?: string; }) { const { uniqueUsernames } = Config.get().general; + const { minUsername, maxUsername } = Config.get().limits.user; // trim special uf8 control characters -> Backspace, Newline, ... username = trimSpecial(username); @@ -374,6 +377,34 @@ export class User extends BaseClass { } } + if (uniqueUsernames) { + // check if there is already an account with this username + if (!User.isUsernameAvailable(username)) + throw FieldErrors({ + username: { + code: "USERNAME_ALREADY_TAKEN", + message: + req?.t("common:field.USERNAME_ALREADY_TAKEN") || "", + }, + }); + + // validate username length + if ( + username.length < minUsername || + username.length > maxUsername + ) { + throw FieldErrors({ + username: { + code: "BASE_TYPE_BAD_LENGTH", + message: + req?.t("common:field.BASE_TYPE_BAD_LENGTH", { + length: `${minUsername} and ${maxUsername}`, + }) || "", + }, + }); + } + } + // TODO: save date_of_birth // appearently discord doesn't save the date of birth and just calculate if nsfw is allowed // if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false @@ -386,6 +417,7 @@ export class User extends BaseClass { const user = User.create({ username: uniqueUsernames ? username.toLowerCase() : username, + global_name: uniqueUsernames ? global_name : undefined, discriminator, id: id || Snowflake.generate(), email: email, @@ -429,6 +461,14 @@ export class User extends BaseClass { return user; } + + static async isUsernameAvailable(username: string) { + const user = await User.findOne({ + where: { username }, + select: ["id"], + }); + return !user; + } } export const CUSTOM_USER_FLAG_OFFSET = BigInt(1) << BigInt(32); diff --git a/src/util/schemas/UsernameAttemptUnauthedSchema.ts b/src/util/schemas/UsernameAttemptUnauthedSchema.ts new file mode 100644 index 00000000..0ac83dd0 --- /dev/null +++ b/src/util/schemas/UsernameAttemptUnauthedSchema.ts @@ -0,0 +1,3 @@ +export interface UsernameAttemptUnauthedSchema { + username: string; +} diff --git a/src/util/schemas/index.ts b/src/util/schemas/index.ts index 44a504cd..bb449e45 100644 --- a/src/util/schemas/index.ts +++ b/src/util/schemas/index.ts @@ -72,6 +72,7 @@ export * from "./UserModifySchema"; export * from "./UserNoteUpdateSchema"; export * from "./UserProfileModifySchema"; export * from "./UserSettingsSchema"; +export * from "./UsernameAttemptUnauthedSchema"; export * from "./Validator"; export * from "./VanityUrlSchema"; export * from "./VoiceIdentifySchema"; diff --git a/src/util/schemas/responses/UsernameAttemptResponse.ts b/src/util/schemas/responses/UsernameAttemptResponse.ts new file mode 100644 index 00000000..864a3bb0 --- /dev/null +++ b/src/util/schemas/responses/UsernameAttemptResponse.ts @@ -0,0 +1,3 @@ +export interface UsernameAttemptResponse { + taken: boolean; +} -- cgit 1.5.1