A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {UserID} from '@app/api/BrandedTypes';
import {getInstanceProductName} from '@app/api/instance/ProductName';
import type {User} from '@app/api/models/User';
import type {UserRepository} from '@app/api/user/repositories/UserRepository';
import {USERNAME_MODE_DISCRIMINATOR} from '@app/api/user/UserTag';
import {randomUsernameVariant} from '@app/api/utils/UsernameSuggestionUtils';
import {DELETED_USER_USERNAME, UserFlags} from '@fluxer/constants/src/UserConstants';
import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';
import type {ICacheService} from '@pkgs/cache/src/ICacheService';
import {ms} from 'itty-time';
type UsernameLookup = Pick<UserRepository, 'findUsersByUsername'>;
interface UniqueUsernameDeps {
users: UsernameLookup;
cache: ICacheService;
}
export interface UsernameReservation {
readonly username: string;
release(): Promise<void>;
}
export interface ParsedLoginHandle {
username: string;
discriminator: number | null;
}
const RESERVATION_TTL_SECONDS = 15;
const RESERVATION_WAIT_MS = ms('5 seconds');
const RESERVATION_RETRY_MS = 50;
const DERIVE_ATTEMPTS = 20;
const LOGIN_HANDLE_REGEX = /^([a-zA-Z0-9_]{1,32})(?:#(\d{1,4}))?$/;
function usernameTakenError(): InputValidationError {
return InputValidationError.fromCode('username', ValidationErrorCodes.USERNAME_ALREADY_TAKEN);
}
export function isUsernameTakenError(error: unknown): boolean {
return (
error instanceof InputValidationError &&
(error.getLocalizedErrors()?.some(({code}) => code === ValidationErrorCodes.USERNAME_ALREADY_TAKEN) ?? false)
);
}
export function assertNoDiscriminatorChange(requested: number | undefined, current: number): void {
if (requested === undefined || requested === current || requested === USERNAME_MODE_DISCRIMINATOR) return;
throw InputValidationError.fromCode('discriminator', ValidationErrorCodes.DISCRIMINATOR_NOT_SUPPORTED_ON_INSTANCE);
}
export function parseLoginHandle(input: string): ParsedLoginHandle | null {
const match = LOGIN_HANDLE_REGEX.exec(input.trim());
if (!match) return null;
return {
username: match[1]!,
discriminator: match[2] === undefined ? null : Number.parseInt(match[2], 10),
};
}
function isPerson(user: User): boolean {
return !user.isBot && (user.flags & UserFlags.DELETED) === 0n;
}
async function findUsernameHolders(users: UsernameLookup, username: string): Promise<Array<User>> {
const usernameLower = username.toLowerCase();
const holders = new Map<UserID, User>();
for (const user of await users.findUsersByUsername(username)) {
if (user.username.toLowerCase() === usernameLower) holders.set(user.id, user);
}
return [...holders.values()];
}
async function findPeopleByUsername(users: UsernameLookup, username: string): Promise<Array<User>> {
return (await findUsernameHolders(users, username)).filter(isPerson);
}
export async function findPersonByLoginHandle(users: UsernameLookup, handle: ParsedLoginHandle): Promise<User | null> {
const people = await findPeopleByUsername(users, handle.username);
const matches =
handle.discriminator === null ? people : people.filter((user) => user.discriminator === handle.discriminator);
return matches.length === 1 ? matches[0]! : null;
}
export async function isUsernameTaken(
users: UsernameLookup,
username: string,
exceptUserId?: UserID,
): Promise<boolean> {
const usernameLower = username.toLowerCase();
if (usernameLower === DELETED_USER_USERNAME.toLowerCase()) return true;
if (usernameLower === getInstanceProductName().toLowerCase()) return true;
const holders = await findUsernameHolders(users, username);
return holders.some(
(user) => user.id !== exceptUserId && (isPerson(user) || user.discriminator === USERNAME_MODE_DISCRIMINATOR),
);
}
async function acquireReservationLock(cache: ICacheService, key: string): Promise<string | null> {
const deadline = Date.now() + RESERVATION_WAIT_MS;
while (Date.now() < deadline) {
const token = await cache.acquireLock(key, RESERVATION_TTL_SECONDS);
if (token) return token;
await new Promise((resolve) => setTimeout(resolve, RESERVATION_RETRY_MS));
}
return null;
}
export async function reserveUsername(
deps: UniqueUsernameDeps,
username: string,
exceptUserId?: UserID,
): Promise<UsernameReservation> {
const key = `unique-username:${username.toLowerCase()}`;
const token = await acquireReservationLock(deps.cache, key);
if (!token) throw usernameTakenError();
const keepAlive = setInterval(
() => {
deps.cache.extendLock(key, token, RESERVATION_TTL_SECONDS).catch(() => {});
},
(RESERVATION_TTL_SECONDS * 1000) / 3,
);
const release = async () => {
clearInterval(keepAlive);
await deps.cache.releaseLock(key, token);
};
try {
if (await isUsernameTaken(deps.users, username, exceptUserId)) throw usernameTakenError();
} catch (error) {
await release();
throw error;
}
return {username, release};
}
export async function reserveDerivedUsername(
deps: UniqueUsernameDeps,
base: string,
exceptUserId?: UserID,
): Promise<UsernameReservation> {
for (let attempt = 0; attempt < DERIVE_ATTEMPTS; attempt++) {
const candidate = attempt === 0 ? base : randomUsernameVariant(base);
if (await isUsernameTaken(deps.users, candidate, exceptUserId)) continue;
try {
return await reserveUsername(deps, candidate, exceptUserId);
} catch (error) {
if (!isUsernameTakenError(error)) throw error;
}
}
throw usernameTakenError();
}
export async function deriveAvailableUsername(users: UsernameLookup, base: string): Promise<string> {
for (let attempt = 0; attempt < DERIVE_ATTEMPTS; attempt++) {
const candidate = attempt === 0 ? base : randomUsernameVariant(base);
if (!(await isUsernameTaken(users, candidate))) return candidate;
}
throw usernameTakenError();
}