gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit4ca2ed204ca2ed20404 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 startmre4ca2ed20/lib/identities.hl

5.6 KB

  1. // lib/identities.hl — THE IDENTITIES of an account (piece 1, ticket #24; short ids ident#23; avatar ticket #15).
  2. // Every write to the identities table is here. Field checks, the label and the short id's alphabet:
  3. // lib/identities-helpers.hl. Statics only, the server realm.
  4. //
  5. // identitiesTable pk @id index @account, !shortId { account (account @id), shortId (5 chars, the PUBLIC id, ident#23), identityName, nickname,
  6. // firstname, lastname, avatar (data URL of the uploaded picture, ticket #15), created, updated }
  7. // every field but `account` optional
  8. //
  9. // An account ALWAYS has at least one identity (the first login makes its DEFAULT one, lib/accounts.hl
  10. // createAccount): the last one cannot be deleted. The DEFAULT identity is the account's oldest remaining
  11. // one (by `created`). Public identity ids are the records' UUIDs.
  12. import { MPackDB } from 'hl:mpackdb'
  13. import { now } from 'hl:time'
  14. import { dir, countOf, first, merged, isId, str, oldestFirst } from './util.hl'
  15. import { checkFields, shownAvatar, labelOf, newShortId, normShortId } from './identities-helpers.hl'
  16. static identitiesTable = new MPackDB(file = dir + '/identities.db', primaryKey = '@id', indexes = ['@account' '!shortId'])
  17. // ---- THE SHORT ID (ident#23) ------------------------------------------------------------
  18. // ONE PUBLIC ID per identity, kept forever, the same in every app: 5 characters (e.g. a68sz)
  19. // (lib/identities-helpers.hl newShortId). Case does not matter (stored lowercase, looked up
  20. // lowercased). Unique: checked against the `!shortId` index when it is made. Random (not
  21. // counted), so it says nothing about how many identities exist.
  22. // a fresh short id nobody has
  23. static freshShortId = () => {
  24. let id = newShortId()
  25. while (countOf(identitiesTable.find('shortId', id)) > 0) { id = newShortId() }
  26. return id
  27. }
  28. static identityByShortId = (v) => {
  29. t = normShortId(v)
  30. return t == null ? null : first(identitiesTable.find('shortId', t))
  31. }
  32. // every identity made before ident#23 gets its short id now, once (at load)
  33. static backfillShortIds = () => {
  34. let n = 0
  35. rows = identitiesTable.find(null, null)
  36. if (countOf(rows) > 0) {
  37. for (r of rows) {
  38. if (r.shortId == null || r.shortId == '') {
  39. identitiesTable.update(r.id, merged(r, { shortId = freshShortId() }))
  40. n = n + 1
  41. }
  42. }
  43. }
  44. return n
  45. }
  46. static shortIdsBackfilled = backfillShortIds()
  47. // ---- reading ------------------------------------------------------------------------------
  48. // the account's identities, oldest first (stored `created`); the first is the DEFAULT one
  49. static identityRecords = (accountId) => {
  50. if (!isId(accountId)) { return [] }
  51. return oldestFirst(identitiesTable.find('account', accountId))
  52. }
  53. static identityRows = (accountId) => {
  54. recs = identityRecords(accountId)
  55. out = []
  56. let n = 1
  57. for (r of recs) {
  58. let full = (str(r.firstname) + ' ' + str(r.lastname)).trim()
  59. let parts = []
  60. if (str(r.nickname) != '') { parts.push('“' + r.nickname + '”') }
  61. if (full != '') { parts.push(full) }
  62. out.push({
  63. id = r.id
  64. shortId = str(r.shortId)
  65. usedHref = '/identity/' + r.id
  66. label = labelOf(r, n)
  67. identityName = str(r.identityName)
  68. nickname = str(r.nickname)
  69. firstname = str(r.firstname)
  70. lastname = str(r.lastname)
  71. avatar = shownAvatar(r)
  72. hasAvatar = shownAvatar(r) != ''
  73. details = parts.length > 0 ? parts.join(' · ') : 'no names'
  74. isDefault = n == 1
  75. canDelete = recs.length > 1
  76. })
  77. n = n + 1
  78. }
  79. return out
  80. }
  81. // the identity `id` if it belongs to the account, else null
  82. static ownIdentity = (accountId, id) => {
  83. if (!isId(id)) { return null }
  84. r = identitiesTable.fetch(id)
  85. if (r == null || r.account != accountId) { return null }
  86. return r
  87. }
  88. // any identity record by its id (null when it is gone), and the account it belongs to
  89. static identityRecord = (identityId) => { return isId(identityId) ? identitiesTable.fetch(identityId) : null }
  90. static accountOfIdentity = (identityId) => {
  91. r = identityRecord(identityId)
  92. return r != null ? r.account : null
  93. }
  94. // ---- writing ------------------------------------------------------------------------------
  95. // THE DEFAULT IDENTITY of a new account (lib/accounts.hl createAccount); its names are optional (asked next)
  96. static addDefaultIdentity = (accountId) => {
  97. return identitiesTable.put({ account = accountId shortId = freshShortId() identityName = 'Default' nickname = '' firstname = '' lastname = '' avatar = '' created = now() updated = now() })
  98. }
  99. // answers { error, field } or { identity (public id) }
  100. static createIdentity = (accountId, fields) => {
  101. c = checkFields(fields)
  102. if (c.error != null) { return c }
  103. rec = { account = accountId shortId = freshShortId() identityName = '' nickname = '' firstname = '' lastname = '' avatar = '' created = now() updated = now() }
  104. for (k of c.fields.keys()) { rec[k] = c.fields[k] }
  105. id = identitiesTable.put(rec)
  106. return { identity = id }
  107. }
  108. // only the fields named change; answers { error, field } or { identity }
  109. static updateIdentity = (accountId, id, fields) => {
  110. r = ownIdentity(accountId, id)
  111. if (r == null) { return { error = 'no such identity' field = 'id' } }
  112. c = checkFields(fields)
  113. if (c.error != null) { return c }
  114. changes = { updated = now() }
  115. for (k of c.fields.keys()) { changes[k] = c.fields[k] }
  116. identitiesTable.update(r.id, merged(r, changes))
  117. return { identity = id }
  118. }
  119. // THE LAST IDENTITY STAYS: answers { error } or { deleted }
  120. static deleteIdentity = (accountId, id) => {
  121. r = ownIdentity(accountId, id)
  122. if (r == null) { return { error = 'no such identity' } }
  123. if (identityRecords(accountId).length <= 1) { return { error = 'this is your only identity — an account always keeps one' } }
  124. identitiesTable.delete(r.id)
  125. return { deleted = id }
  126. }

Branches

Latest commits

  • 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
  • 6280c92fre-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 147744bftests/migration: the fixture app origin follows MIGRATION_GATE_TESTAPP_PORT (oldstore-009 OLD_TESTAPP)mre
  • 48cd36bctests/migration: ports from env (MIGRATION_GATE_PORT, MIGRATION_GATE_TESTAPP_PORT); 8395/8396 were taken by a foreign servermre
  • 3d9b2fd8docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 19b7e3c3deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • c8a596f4re-vendor Hybriel 66c3db71 (web batch 4); e2e: main menu marked after in-place sign-in is a normal check; report 014mre
  • 8f3dc1ectests: delivery allow-list for the stopped server's network errors uses the gate port, not 8700mre
  • 9cc8eddftests: notify and iplimit gate Chrome ports from env (NOTIFY_GATE_CHROME, IPLIMIT_GATE_CHROME_A/B)mre
  • d6da2d02tests: delivery and migration gate ports from env (DELIVERY_GATE_PORT, DELIVERY_GATE_CHROME, MIGRATION_GATE_CHROME) — hard-coded 8700-8719 collided with a worker during deploymre
  • 7276a008ident mission 013: re-vendor Hybriel master f9614007 (#200, #202 mpackdb copy fix); gates and e2e rerunmre
  • e13e30c3ident mission 013: report, mission, repro, LOGmre