gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchf11-upsert8f3d9bc9|||PR ident#42 keyed upsert and removemref11-upsert/lib/identities.hl

7.1 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 // the lookup key of upsert and remove; `id` stays for every reader (row.id on a record with only @id reads nothing)
  64. id = r.id
  65. shortId = str(r.shortId)
  66. usedHref = '/identity/' + r.id
  67. label = labelOf(r, n)
  68. identityName = str(r.identityName)
  69. nickname = str(r.nickname)
  70. firstname = str(r.firstname)
  71. lastname = str(r.lastname)
  72. avatar = shownAvatar(r)
  73. hasAvatar = shownAvatar(r) != ''
  74. details = parts.length > 0 ? parts.join(' · ') : 'no names'
  75. isDefault = n == 1
  76. canDelete = recs.length > 1
  77. })
  78. n = n + 1
  79. }
  80. return out
  81. }
  82. // ONE row of identityRows (the same as in the list: position-dependent parts included), or null when the account has no such identity
  83. static identityRow = (accountId, id) => {
  84. for (row of identityRows(accountId)) { if ('' + row.id == '' + id) { return row } }
  85. return null
  86. }
  87. // the identity `id` if it belongs to the account, else null
  88. static ownIdentity = (accountId, id) => {
  89. if (!isId(id)) { return null }
  90. r = identitiesTable.fetch(id)
  91. if (r == null || r.account != accountId) { return null }
  92. return r
  93. }
  94. // any identity record by its id (null when it is gone), and the account it belongs to
  95. static identityRecord = (identityId) => { return isId(identityId) ? identitiesTable.fetch(identityId) : null }
  96. static accountOfIdentity = (identityId) => {
  97. r = identityRecord(identityId)
  98. return r != null ? r.account : null
  99. }
  100. // ---- writing ------------------------------------------------------------------------------
  101. // THE DEFAULT IDENTITY of a new account (lib/accounts.hl createAccount); its names are optional (asked next)
  102. static addDefaultIdentity = (accountId) => {
  103. return identitiesTable.put({ account = accountId shortId = freshShortId() identityName = 'Default' nickname = '' firstname = '' lastname = '' avatar = '' created = now() updated = now() })
  104. }
  105. // answers { error, field } or { identity (public id) }
  106. static maxIdentities = envNumber('IDENT_MAX_IDENTITIES', 50)
  107. static createIdentity = (accountId, fields) => {
  108. c = checkFields(fields)
  109. if (c.error != null) { return c }
  110. if (identityRecords(accountId).length >= maxIdentities) { return { error = 'an account can have at most ' + maxIdentities + ' identities' field = '' } }
  111. rec = { account = accountId shortId = freshShortId() identityName = '' nickname = '' firstname = '' lastname = '' avatar = '' created = now() updated = now() }
  112. for (k of c.fields.keys()) { rec[k] = c.fields[k] }
  113. id = identitiesTable.put(rec)
  114. return { identity = id }
  115. }
  116. // 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 }
  117. static createAgent = (accountId, name) => {
  118. c = checkFields({ identityName = name })
  119. if (c.error != null) { return c }
  120. if (c.fields.identityName == null || c.fields.identityName.trim() == '') { return { error = 'give the identity a name' field = 'identityName' } }
  121. if (identityRecords(accountId).length >= maxIdentities) { return { error = 'an account can have at most ' + maxIdentities + ' identities' field = '' } }
  122. id = identitiesTable.put({ account = accountId shortId = freshShortId() identityName = c.fields.identityName.trim() nickname = '' firstname = '' lastname = '' avatar = '' kind = 'agent' created = now() updated = now() })
  123. return { identity = id }
  124. }
  125. static isAgent = (r) => { return r != null && r.kind == 'agent' }
  126. // only the fields named change; answers { error, field } or { identity }
  127. static updateIdentity = (accountId, id, fields) => {
  128. r = ownIdentity(accountId, id)
  129. if (r == null) { return { error = 'no such identity' field = 'id' } }
  130. c = checkFields(fields)
  131. if (c.error != null) { return c }
  132. changes = { updated = now() }
  133. for (k of c.fields.keys()) { changes[k] = c.fields[k] }
  134. identitiesTable.update(r.id, merged(r, changes))
  135. return { identity = id }
  136. }
  137. // THE LAST IDENTITY STAYS: answers { error } or { deleted }
  138. static deleteIdentity = (accountId, id) => {
  139. r = ownIdentity(accountId, id)
  140. if (r == null) { return { error = 'no such identity' } }
  141. if (identityRecords(accountId).length <= 1) { return { error = 'this is your only identity — an account always keeps one' } }
  142. identitiesTable.delete(r.id)
  143. return { deleted = id }
  144. }

Branches

Latest commits

  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre
  • 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre
  • 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