ident
All repositories: gitoria
6.7 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 }}// AN AGENT IDENTITY (ident#27): made from the Access page, only a name; `kind = 'agent'` keeps it out of the login selector. Answers { error, field } or { identity }static createAgent = (accountId, name) => {c = checkFields({ identityName = name })if (c.error != null) { return c }if (c.fields.identityName == null || c.fields.identityName.trim() == '') { return { error = 'give the identity a name' field = 'identityName' } }if (identityRecords(accountId).length >= maxIdentities) { return { error = 'an account can have at most ' + maxIdentities + ' identities' field = '' } }id = identitiesTable.put({ account = accountId shortId = freshShortId() identityName = c.fields.identityName.trim() nickname = '' firstname = '' lastname = '' avatar = '' kind = 'agent' created = now() updated = now() })return { identity = id }}static isAgent = (r) => { return r != null && r.kind == 'agent' }// 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
- 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
- 991641d4Merge branch 'f10-access' into mainIdent
- e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
- df7696dbident#34: tests: access ticks selector quoting fixmre
- 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
- 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
- dc702419Merge branch 'f9-choose' into mainIdent
- 8caec771|||PR ident#34 restore .choose hookmre
- 0dbff960Merge branch 'f8-shared' into mainIdent
- d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
- 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
- eb039073ident#34: tests: signin and migration find the choice without the classmre
- 4b94a594ident#34: tests: fix selector of the identity choicemre
- bebc88e4ident#34: main.hl drops the unstyled classes inboxlink and whomre
- b4e7ad04ident#34: the identity choice link/button has no class (rule on #chooselist a)mre
- 3c33d6eaident#34: the connection link in inbox has no class (rule on #conns a)mre
- dc97ec0dident#34: the Notif. chip and the Used in link share chipLook (Style.pChip) instead of the classes chip/used/notifmre
- 378bd432ident#34: the identity picture look is a static in styles.hl (Style.pAvatar); sizes stay with each listmre
- 1cf79e7eident#34: the wide page column is a static in styles.hl (Style.pWide), not the class "wide"mre
- ddc7d135Merge branch 'f7-hooks' into mainIdent