All files / app-leaderboard/src profiles.ts

88% Statements 66/75
80% Branches 36/45
100% Functions 7/7
91.22% Lines 52/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174          15x 15x     15x 15x     15x                                                               89x 712x 89x             21x 21x 13x 13x 10x                     15x       14x 14x 13x 7x 6x               91x 91x 88x 88x 88x 88x         88x   3x             94x       94x 93x       93x                             9x 9x 9x 9x 8x 8x 6x 6x 4x     5x 4x 4x 3x   4x 4x 4x       4x 4x                   4x         3x         3x    
/** Local ranking projection. Accounts owns profile edits in OIDC deployments. */
import { and, eq, ne } from 'drizzle-orm';
import type { Database } from './db/client.ts';
import { handleHistory, profile } from './db/schema.ts';
 
const HANDLE_ALPHABET = 'abcdefghijklmnopqrstuvwxyz0123456789';
const HANDLE_ATTEMPTS = 5;
 
/** Handles: letters, digits and `_`, 3 to 15 characters, case-insensitive (stored in lower case). */
const HANDLE_PATTERN = /^[A-Za-z0-9_]{3,15}$/;
export const DISPLAY_NAME_MAX = 32;
 
/** Handles nobody can take: they name the site, its pages, or look official. */
const RESERVED_HANDLES = new Set([
  'admin',
  'administrator',
  'anonymous',
  'api',
  'be_music',
  'bemusic',
  'chart',
  'charts',
  'chartset',
  'chartsets',
  'help',
  'leaderboard',
  'me',
  'mod',
  'moderator',
  'null',
  'official',
  'player',
  'players',
  'root',
  'score',
  'scores',
  'settings',
  'staff',
  'support',
  'system',
  'undefined',
]);
 
/** A random handle (`player_` and eight lowercase letters or digits) until the player picks one. */
export function generateHandle(bytes: Uint8Array = crypto.getRandomValues(new Uint8Array(8))): string {
  let suffix = '';
  for (const byte of bytes) suffix += HANDLE_ALPHABET[byte % HANDLE_ALPHABET.length];
  return `player_${suffix}`;
}
 
export type HandleCheck = { ok: true; handle: string } | { ok: false; reason: 'format' | 'reserved' };
 
/** Checks a handle the player typed and gives its stored form (lower case). */
export function normalizeHandle(input: string): HandleCheck {
  const handle = input.trim();
  if (!HANDLE_PATTERN.test(handle)) return { ok: false, reason: 'format' };
  const lower = handle.toLowerCase();
  if (RESERVED_HANDLES.has(lower)) return { ok: false, reason: 'reserved' };
  return { ok: true, handle: lower };
}
 
export type DisplayNameCheck =
  | { ok: true; displayName: string }
  | { ok: false; reason: 'empty' | 'length' | 'characters' };
 
/**
 * Line breaks, other control characters and the bidirectional controls that can disguise a name (U+202A–U+202E,
 * U+2066–U+2069, U+200E, U+200F, U+061C).
 */
const FORBIDDEN_IN_NAME = /[\p{Cc}\p{Zl}\p{Zp}‪-‮⁦-⁩‎‏؜]/u;
 
/** Checks a display name: 1 to 32 characters (code points) after trimming, Japanese and other scripts welcome. */
export function normalizeDisplayName(input: string): DisplayNameCheck {
  const displayName = input.normalize('NFC').trim();
  if (displayName.length === 0) return { ok: false, reason: 'empty' };
  if (FORBIDDEN_IN_NAME.test(displayName)) return { ok: false, reason: 'characters' };
  if ([...displayName].length > DISPLAY_NAME_MAX) return { ok: false, reason: 'length' };
  return { ok: true, displayName };
}
 
/**
 * Creates the public profile of a new account with a generated handle (retrying on the rare collision). OAuth profile
 * names are never copied in; the player names themselves later.
 */
export async function createProfile(db: Database, userId: string, isAnonymous: boolean): Promise<void> {
  const existing = await db.query.profile.findFirst({ where: eq(profile.userId, userId), columns: { userId: true } });
  if (existing !== undefined) return;
  for (let attempt = 0; attempt < HANDLE_ATTEMPTS; attempt += 1) {
    const handle = generateHandle();
    Iif (await isHandleTaken(db, handle, userId)) continue;
    const inserted = await db
      .insert(profile)
      .values({ userId, handle, displayName: isAnonymous ? 'Anonymous' : 'Player' })
      .onConflictDoNothing()
      .returning({ userId: profile.userId });
    if (inserted.length > 0) return;
    // Concurrent first requests may have inserted this player's profile with another generated handle.
    Eif (await db.query.profile.findFirst({ where: eq(profile.userId, userId), columns: { userId: true } })) return;
  }
  throw new Error('could not generate a unique handle');
}
 
/** Whether another account holds `handle`, now or as one of its former handles. */
async function isHandleTaken(db: Database, handle: string, userId: string): Promise<boolean> {
  const current = await db.query.profile.findFirst({
    where: and(eq(profile.handle, handle), ne(profile.userId, userId)),
    columns: { userId: true },
  });
  if (current !== undefined) return true;
  const former = await db.query.handleHistory.findFirst({
    where: and(eq(handleHistory.handle, handle), ne(handleHistory.userId, userId)),
    columns: { userId: true },
  });
  return former !== undefined;
}
 
export type ProfileUpdate = { handle?: string; displayName?: string };
export type ProfileUpdateResult =
  | { ok: true }
  | { ok: false; status: 400; error: 'invalid-handle' | 'invalid-display-name'; detail: string }
  | { ok: false; status: 404; error: 'not-found' }
  | { ok: false; status: 409; error: 'handle-taken' };
 
/**
 * Changes a player's handle and display name. The old handle is kept in `handle_history`: still reserved for the
 * player, and its pages redirect to the new one. Taking back a former handle of one's own removes it from the history.
 */
export async function updateProfile(db: Database, userId: string, update: ProfileUpdate): Promise<ProfileUpdateResult> {
  const current = await db.query.profile.findFirst({ where: eq(profile.userId, userId) });
  Iif (current === undefined) return { ok: false, status: 404, error: 'not-found' };
  const changes: { handle?: string; displayName?: string } = {};
  if (update.handle !== undefined) {
    const checked = normalizeHandle(update.handle);
    if (!checked.ok) return { ok: false, status: 400, error: 'invalid-handle', detail: checked.reason };
    Eif (checked.handle !== current.handle) {
      if (await isHandleTaken(db, checked.handle, userId)) return { ok: false, status: 409, error: 'handle-taken' };
      changes.handle = checked.handle;
    }
  }
  if (update.displayName !== undefined) {
    const checked = normalizeDisplayName(update.displayName);
    if (!checked.ok) return { ok: false, status: 400, error: 'invalid-display-name', detail: checked.reason };
    changes.displayName = checked.displayName;
  }
  Iif (changes.handle === undefined && changes.displayName === undefined) return { ok: true };
  const newHandle = changes.handle;
  Iif (newHandle === undefined) {
    await db.update(profile).set(changes).where(eq(profile.userId, userId));
    return { ok: true };
  }
  try {
    await db.batch([
      db.insert(handleHistory).values({ handle: current.handle, userId }).onConflictDoNothing(),
      db.delete(handleHistory).where(and(eq(handleHistory.handle, newHandle), eq(handleHistory.userId, userId))),
      db.update(profile).set(changes).where(eq(profile.userId, userId)),
    ]);
  } catch (error) {
    // Another account took the handle between the check and the write (the unique index refused it).
    if (String(error).includes('UNIQUE')) return { ok: false, status: 409, error: 'handle-taken' };
    throw error;
  }
  return { ok: true };
}
 
/** The current handle of whoever used `handle` before, for redirecting old player URLs. */
export async function resolveFormerHandle(db: Database, handle: string): Promise<string | undefined> {
  const [row] = await db
    .select({ handle: profile.handle })
    .from(handleHistory)
    .innerJoin(profile, eq(profile.userId, handleHistory.userId))
    .where(eq(handleHistory.handle, handle.toLowerCase()));
  return row?.handle;
}