gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit378bd432378bd432ident#34: the identity picture look is a static in styles.hl (Style.pAvatar); sizes stay with each listmre378bd432/lib/identities.hl

6.7 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, envNumber } 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 maxIdentities = envNumber('IDENT_MAX_IDENTITIES', 50)
  101. static createIdentity = (accountId, fields) => {
  102. c = checkFields(fields)
  103. if (c.error != null) { return c }
  104. if (identityRecords(accountId).length >= maxIdentities) { return { error = 'an account can have at most ' + maxIdentities + ' identities' field = '' } }
  105. rec = { account = accountId shortId = freshShortId() identityName = '' nickname = '' firstname = '' lastname = '' avatar = '' created = now() updated = now() }
  106. for (k of c.fields.keys()) { rec[k] = c.fields[k] }
  107. id = identitiesTable.put(rec)
  108. return { identity = id }
  109. }
  110. // 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 }
  111. static createAgent = (accountId, name) => {
  112. c = checkFields({ identityName = name })
  113. if (c.error != null) { return c }
  114. if (c.fields.identityName == null || c.fields.identityName.trim() == '') { return { error = 'give the identity a name' field = 'identityName' } }
  115. if (identityRecords(accountId).length >= maxIdentities) { return { error = 'an account can have at most ' + maxIdentities + ' identities' field = '' } }
  116. id = identitiesTable.put({ account = accountId shortId = freshShortId() identityName = c.fields.identityName.trim() nickname = '' firstname = '' lastname = '' avatar = '' kind = 'agent' created = now() updated = now() })
  117. return { identity = id }
  118. }
  119. static isAgent = (r) => { return r != null && r.kind == 'agent' }
  120. // only the fields named change; answers { error, field } or { identity }
  121. static updateIdentity = (accountId, id, fields) => {
  122. r = ownIdentity(accountId, id)
  123. if (r == null) { return { error = 'no such identity' field = 'id' } }
  124. c = checkFields(fields)
  125. if (c.error != null) { return c }
  126. changes = { updated = now() }
  127. for (k of c.fields.keys()) { changes[k] = c.fields[k] }
  128. identitiesTable.update(r.id, merged(r, changes))
  129. return { identity = id }
  130. }
  131. // THE LAST IDENTITY STAYS: answers { error } or { deleted }
  132. static deleteIdentity = (accountId, id) => {
  133. r = ownIdentity(accountId, id)
  134. if (r == null) { return { error = 'no such identity' } }
  135. if (identityRecords(accountId).length <= 1) { return { error = 'this is your only identity — an account always keeps one' } }
  136. identitiesTable.delete(r.id)
  137. return { deleted = id }
  138. }

Branches

Latest commits

  • 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
  • eee0b513|||PR ident#34 part: test-hook classes replaced by name/element/attribute selectors (tests adjusted), the shared lead/message/notice looks are statics in styles.hlmre
  • ccd6d0c4ident#34: browser gate: rowButton finds the row's button by namemre
  • 24dd414eident#34: the shared lead, message and notice looks are statics in components/styles.hl, imported by the components (lib/looks.hl gone)mre
  • 03ed26c7ident#34: access.hl: the remaining lead classes droppedmre
  • 1d066c42ident#34: access.hl drops its unstyled lead class (the shared lead rule would otherwise mute it)mre
  • 1e1dd7b4ident#34: the access page keeps its lead class string (the page has no ident-card, so its lead line was never muted; a shared rule would change the look)mre
  • 89ac6553ident#34: the lead, message and notice lines are named with Style.pLead / pMessage / pNotice (shared bodies in lib/looks.hl) instead of class stringsmre
  • e84be3deident#34: browser gate lists the Edit/Delete buttons by namemre
  • fb7111faident#34: access page: unstyled test-hook classes (idrow, never, who, whoname, appname, neverrevoke) dropped; the tests use element/name selectorsmre
  • 9f2ad803ident#34: notification switches: push/email by name=..., locked by aria-disabled, the switch look is the role=switch button (no switch/push/email/locked classes)mre
  • e0f0ddaaident#34: inbox: the Mark read/unread button is styled as the button of its note-foot, the icon is found as the row's img (no toggle/icon classes)mre
  • 16b49756ident#34: unused row classes (choice, usedrow, kind, shortid) droppedmre
  • f9d6fb38ident#34: apps and identity rows: the Edit/New secret/Delete buttons carry name=..., the API key is found by its element; unused row classes (app, identity, shortid) droppedmre
  • b323265aMerge branch 'f5-styles' into mainIdent
  • 9ced53c3|||PR ident#33 ident#35 every component's styles live in its own Style block; styles.hl keeps the page-wide rest; the account bar is phone first (min-width: 60rem block); the avatar preview is hidden with the hidden attribute (ident#34 in part)mre
  • f28333e4ident#33 ident#35: styles.hl keeps only the accent, the page column and the shared patterns; the account bar is written phone first (base rule + one min-width: 60rem block) instead of a max-width overridemre
  • 5b4541fcident#33: the selector sign-in frame's rules move into embed.hl's Stylemre