ident
All repositories: gitoria
5.8 KB
// lib/identities.hl — THE IDENTITIES of an account (piece 1, ticket #24; short ids ident#23; avatar ticket #15).// Every write to the identities table is here. Field checks, the label and the short id's alphabet:// lib/identities-helpers.hl. Statics only, the server realm.//// identitiesTable pk @id index @account, !shortId { account (account @id), shortId (5 chars, the PUBLIC id, ident#23), identityName, nickname,// firstname, lastname, avatar (data URL of the uploaded picture, ticket #15), created, updated }// every field but `account` optional//// An account ALWAYS has at least one identity (the first login makes its DEFAULT one, lib/accounts.hl// createAccount): the last one cannot be deleted. The DEFAULT identity is the account's oldest remaining// one (by `created`). Public identity ids are the records' UUIDs.import { MPackDB } from 'hl:mpackdb'import { now } from 'hl:time'import { dir, countOf, first, merged, isId, str, oldestFirst, envNumber } from './util.hl'import { checkFields, shownAvatar, labelOf, newShortId, normShortId } from './identities-helpers.hl'static identitiesTable = new MPackDB(file = dir + '/identities.db', primaryKey = '@id', indexes = ['@account' '!shortId'])// ---- THE SHORT ID (ident#23) ------------------------------------------------------------// ONE PUBLIC ID per identity, kept forever, the same in every app: 5 characters (e.g. a68sz)// (lib/identities-helpers.hl newShortId). Case does not matter (stored lowercase, looked up// lowercased). Unique: checked against the `!shortId` index when it is made. Random (not// counted), so it says nothing about how many identities exist.// a fresh short id nobody hasstatic freshShortId = () => {let id = newShortId()while (countOf(identitiesTable.find('shortId', id)) > 0) { id = newShortId() }return id}static identityByShortId = (v) => {t = normShortId(v)return t == null ? null : first(identitiesTable.find('shortId', t))}// every identity made before ident#23 gets its short id now, once (at load)static backfillShortIds = () => {let n = 0rows = identitiesTable.find(null, null)if (countOf(rows) > 0) {for (r of rows) {if (r.shortId == null || r.shortId == '') {identitiesTable.update(r.id, merged(r, { shortId = freshShortId() }))n = n + 1}}}return n}static shortIdsBackfilled = backfillShortIds()// ---- reading ------------------------------------------------------------------------------// the account's identities, oldest first (stored `created`); the first is the DEFAULT onestatic identityRecords = (accountId) => {if (!isId(accountId)) { return [] }return oldestFirst(identitiesTable.find('account', accountId))}static identityRows = (accountId) => {recs = identityRecords(accountId)out = []let n = 1for (r of recs) {let full = (str(r.firstname) + ' ' + str(r.lastname)).trim()let parts = []if (str(r.nickname) != '') { parts.push('“' + r.nickname + '”') }if (full != '') { parts.push(full) }out.push({id = r.idshortId = str(r.shortId)usedHref = '/identity/' + r.idlabel = labelOf(r, n)identityName = str(r.identityName)nickname = str(r.nickname)firstname = str(r.firstname)lastname = str(r.lastname)avatar = shownAvatar(r)hasAvatar = shownAvatar(r) != ''details = parts.length > 0 ? parts.join(' · ') : 'no names'isDefault = n == 1canDelete = recs.length > 1})n = n + 1}return out}// the identity `id` if it belongs to the account, else nullstatic ownIdentity = (accountId, id) => {if (!isId(id)) { return null }r = identitiesTable.fetch(id)if (r == null || r.account != accountId) { return null }return r}// any identity record by its id (null when it is gone), and the account it belongs tostatic identityRecord = (identityId) => { return isId(identityId) ? identitiesTable.fetch(identityId) : null }static accountOfIdentity = (identityId) => {r = identityRecord(identityId)return r != null ? r.account : null}// ---- writing ------------------------------------------------------------------------------// THE DEFAULT IDENTITY of a new account (lib/accounts.hl createAccount); its names are optional (asked next)static addDefaultIdentity = (accountId) => {return identitiesTable.put({ account = accountId shortId = freshShortId() identityName = 'Default' nickname = '' firstname = '' lastname = '' avatar = '' created = now() updated = now() })}// answers { error, field } or { identity (public id) }static maxIdentities = envNumber('IDENT_MAX_IDENTITIES', 50)static createIdentity = (accountId, fields) => {c = checkFields(fields)if (c.error != null) { return c }if (identityRecords(accountId).length >= maxIdentities) { return { error = 'an account can have at most ' + maxIdentities + ' identities' field = '' } }rec = { account = accountId shortId = freshShortId() identityName = '' nickname = '' firstname = '' lastname = '' avatar = '' created = now() updated = now() }for (k of c.fields.keys()) { rec[k] = c.fields[k] }id = identitiesTable.put(rec)return { identity = id }}// only the fields named change; answers { error, field } or { identity }static updateIdentity = (accountId, id, fields) => {r = ownIdentity(accountId, id)if (r == null) { return { error = 'no such identity' field = 'id' } }c = checkFields(fields)if (c.error != null) { return c }changes = { updated = now() }for (k of c.fields.keys()) { changes[k] = c.fields[k] }identitiesTable.update(r.id, merged(r, changes))return { identity = id }}// THE LAST IDENTITY STAYS: answers { error } or { deleted }static deleteIdentity = (accountId, id) => {r = ownIdentity(accountId, id)if (r == null) { return { error = 'no such identity' } }if (identityRecords(accountId).length <= 1) { return { error = 'this is your only identity — an account always keeps one' } }identitiesTable.delete(r.id)return { deleted = id }}
Branches
Latest commits
- a22d2daaident#31: the sign-in frame is no taller than the tallest step needs (plain CSS height per width), gate check for itmre
- a77d1b22ident#31: the welcome form keeps editId a plain value (browser gate), README, wider rows in the framemre
- 5c481bfdident#31: sign in to ident inside the selector's panel (email, code, identity in an iframe; a new address goes to ident in the same tab and comes back)mre
- 061abfe2ident: the apps gate waits until the test app's login button stops moving before it clicks (the page grows after load and the click missed about every second run)mre
- 45f7e4daident: the user menu's Sign out waits for the server before it reloads (the apps gate failed because the session could still be signed in)mre
- f07658c7ident: home page shows the account once (account bar removed, the user menu carries email, Sign out, Sign out everywhere); wildcard origins https://*.domain for apps on many subdomains; gates follow (WIP: gates not re-run after the last edits)mre
- f2c43bbbtheme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
- 6d4f996esessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
- 029eb5e5deploy: security gate (tests/security.mjs)mre
- d3c065a8security audit (SI): uploadMax 0, Secure __Host- cookie behind https, headers on function routes/files, page CSP+referrer meta, app/identity caps, tests/security.mjs, reportmre
- 5561a0cbreport I4-mobilemre
- 4ca2ed20404 page in the shell (viewport, status 404, way home); phone: account actions live in the user menu so the open menu covers nothing; mobile gate; signoutall waits for a cold startmre
- 9baad745tests: browser gate waits for the reloaded document (offline reload race under CPU throttle); HL_CPU_THROTTLE knobmre
- b96796bdtests: wait for hydration before real clicks after a navigation (H2)mre
- 9722964fre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
- c9b196c9tests/iplimit: wait for the welcome form itself after B's first login (identities list renders before it); mission 017 reportmre
- 72e0f587ident#26: identity page with "Used in" (app, since, last use, Notif. chip, empty state); lastLogin recorded at exchange; gates + lisbeth E2E; mission 016 reportmre
- 94576fbcident#26 WIP: identity page /identity/:iid with "Used in" (app, since, last use, Notif. chip), lastLogin recorded at exchange, "Used in…" link on the identities list; apps gate 4b, e2e-usedin.mjsmre
- 349cb502tests/invites: wait for hydration on the code page before entering the code (native GET submit raced the welcome form); mission 015 reportmre
- d1bf4720tests: no hard-coded ports — every gate's ports and fixture origins come from <GATE>_PORT / _CHROME env (defaults unchanged; Bob/fixture origins follow, selector cookie name too)mre