gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit383cce23383cce23|||PR Prevent saved local edits from restoring deleted notesAtlas383cce23/lib/notes.hl

8.5 KB

  1. // lib/notes.hl — THE NOTES (ticket notes#1) and their SYNC (tickets notes#2, notes#4). One hl:mpackdb table,
  2. // storage/mpackdb/notes.db:
  3. // pk @id index owner { owner, text (Markdown), created, edited, gone, local, by, since, copyOf } times = ms
  4. // gone = ms a note was deleted (0 = alive; the row stays as a TOMBSTONE, /api/notes reports it)
  5. // local = the id the creating tab gave the note before it had a server id (a retried save finds it again)
  6. // by = the login tag (lib/users.hl tagOf) of the browser whose save made this version ('' = /api/notes or older)
  7. // since = the `edited` that browser had seen when it started this run of saves (its `base`; `from` is a reserved word)
  8. // copyOf = on a conflict copy: the note it was split from ('' otherwise)
  9. // The SUBJECT of a note is not stored: it is the first non-empty line of the text, without leading `#`
  10. // (CONCEPT.md "Decisions"); the page (components/notes.hl) derives it and orders the sidebar by `edited`.
  11. // Every function takes the owner's user id and only ever reaches that user's notes. Every write to the table is here.
  12. // A note's VERSION is its `edited` (ms); a browser sends the `edited` it last saw of a note (`base`).
  13. // CONTINUATION (notes#4): a browser offline queues one save after another, all with the same old `base`. The second
  14. // one would look like a conflict, so a save whose `by` + `base` are the note's `by` + `since` continues that run.
  15. // A browser that LOST a conflict keeps writing into its copy (same `copyOf`, `by`, `since`), not into a new copy each time.
  16. // Looked up in lib/notes-helpers.hl: textError (size limit), isAlive (tombstones), stampOf (version stamps), copyMark.
  17. import { MPackDB } from 'hl:mpackdb'
  18. import { now } from 'hl:time'
  19. import { storageDir, countOfList } from './util.hl'
  20. import { textError, isAlive, stampOf, copyMark } from './notes-helpers.hl'
  21. static notesTable = new MPackDB(file = storageDir + '/notes.db', primaryKey = '@id', indexes = ['owner'])
  22. // the stored record of note `n` with a new text / version / tombstone / run — every field, written out (hybriel#23)
  23. static recordOf = (Hybrid n, String text, Number edited, Number gone, String by, Number since) => {
  24. return { id = n.id owner = n.owner text = text created = n.created edited = edited gone = gone local = n.local == null ? '' : n.local by = by since = since copyOf = n.copyOf == null ? '' : n.copyOf }
  25. }
  26. // one note of this owner, or null
  27. static noteOf = (String userId, id) => {
  28. if (id == null || hlTypeName(id) != 'String' || id == '') { return null }
  29. n = notesTable.fetch(id)
  30. if (n == null || n.owner != userId || !isAlive(n)) { return null }
  31. return n
  32. }
  33. // THE PAGE'S LIST (components/notes.hl `notes`, the push notesChanged): every live note of the owner, with its text
  34. static notesFor = (String userId) => {
  35. out = []
  36. if (userId == null) { return out }
  37. found = notesTable.find('owner', userId)
  38. let i = 0
  39. n = countOfList(found)
  40. while (i < n) {
  41. r = found[i]
  42. if (isAlive(r)) { out.push({ id = r.id text = r.text edited = r.edited local = r.local == null ? '' : r.local copyOf = r.copyOf == null ? '' : r.copyOf }) }
  43. i = i + 1
  44. }
  45. return out
  46. }
  47. // what /api/notes compares: id, edited, gone (no text) of every note of the owner, tombstones included
  48. static metaRows = (String userId) => {
  49. found = notesTable.find('owner', userId)
  50. out = []
  51. let i = 0
  52. n = countOfList(found)
  53. while (i < n) {
  54. out.push({ id = found[i].id edited = found[i].edited gone = isAlive(found[i]) ? 0 : found[i].gone })
  55. i = i + 1
  56. }
  57. return out
  58. }
  59. // one note with its text, or null
  60. static noteView = (String userId, id) => {
  61. n = noteOf(userId, id)
  62. return n == null ? null : { id = n.id text = n.text edited = n.edited created = n.created }
  63. }
  64. static newNote = (String userId, String text, String local, Number edited, String by, Number since, String copyOf) => {
  65. t = now()
  66. id = notesTable.put({ owner = userId text = text created = t edited = stampOf(edited, 0) gone = 0 local = local by = by since = since copyOf = copyOf })
  67. return id
  68. }
  69. // the live note this browser made under its own id `local` (a save of a never-synced note, or a retry), or null
  70. static noteOfLocal = (String userId, String local) => {
  71. if (local == null || local == '') { return null }
  72. mine = notesTable.find('owner', userId)
  73. let i = 0
  74. let n = null
  75. c = countOfList(mine)
  76. while (i < c && n == null) {
  77. if (isAlive(mine[i]) && mine[i].local == local) { n = mine[i] }
  78. i = i + 1
  79. }
  80. return n
  81. }
  82. // the copy this browser (`by`, run `since`) already made of note `id` when it lost a conflict, or null
  83. static copyOfRun = (String userId, String id, String by, Number since) => {
  84. if (by == null || by == '') { return null }
  85. mine = notesTable.find('owner', userId)
  86. let i = 0
  87. let n = null
  88. c = countOfList(mine)
  89. while (i < c && n == null) {
  90. x = mine[i]
  91. if (isAlive(x) && x.copyOf == id && x.by == by && x.since == since) { n = x }
  92. i = i + 1
  93. }
  94. return n
  95. }
  96. // does a save (`by`, `base`) continue the run of saves that made the note's current version?
  97. static continues = (Hybrid n, String by, Number base) => { return n.edited == base || (by != null && by != '' && n.by == by && n.since == base) }
  98. static answerOf = (Hybrid n, Hybrid copy) => {
  99. return { id = n.id edited = n.edited text = n.text copy = copy == null ? null : { id = copy.id edited = copy.edited text = copy.text } }
  100. }
  101. // A SAVE from a browser: { id (server id or ''), local (the browser's own id of a never-synced note), text,
  102. // base (the `edited` the browser last saw, 0 = never), at (when the browser made the edit), by (its login tag, '' = none) }
  103. // → { id, edited, text, copy? }: `text` is what the note is NOW; `copy` = { id, edited, text } is the version that
  104. // lost a conflict, kept as a note of its own — nothing is ever lost.
  105. static pushNote = (String userId, String id, String local, String text, Number base, Number at, String by) => {
  106. bad = textError(text)
  107. if (bad != null) { return { error = bad } }
  108. let n = null
  109. if (id != '') { n = noteOf(userId, id) }
  110. if (n == null) { n = noteOfLocal(userId, local) }
  111. if (n == null) {
  112. // a new note (or one deleted elsewhere while this browser edited it: the edit becomes a note again)
  113. nid = newNote(userId, text, local, at > 0 ? at : now(), by, 0, '')
  114. if (nid == null) { return { error = 'could not store the note: ' + notesTable.lastError() } }
  115. return answerOf(notesTable.fetch(nid), null)
  116. }
  117. if (n.text == text) { return answerOf(n, null) }
  118. // this browser lost a conflict on this note before: its run goes on in its copy
  119. had = copyOfRun(userId, n.id, by, base)
  120. if (had != null) {
  121. notesTable.update(had.id, recordOf(had, text + copyMark, stampOf(at, had.edited), 0, by, base))
  122. return answerOf(n, notesTable.fetch(had.id))
  123. }
  124. if (continues(n, by, base)) {
  125. notesTable.update(n.id, recordOf(n, text, stampOf(at, n.edited), 0, by, base))
  126. after = notesTable.fetch(n.id)
  127. if (after == null || after.text != text) { return { error = 'could not store the note: ' + notesTable.lastError() } }
  128. return answerOf(after, null)
  129. }
  130. // CONFLICT: changed here AND there. The later edit wins (last write wins); the other version becomes a copy.
  131. if (at > n.edited) {
  132. cid = newNote(userId, n.text + copyMark, '', n.edited, n.by == null ? '' : n.by, n.since == null ? 0 : n.since, n.id)
  133. notesTable.update(n.id, recordOf(n, text, stampOf(at, n.edited), 0, by, base))
  134. return answerOf(notesTable.fetch(n.id), cid == null ? null : notesTable.fetch(cid))
  135. }
  136. cid = newNote(userId, text + copyMark, '', at, by, base, n.id)
  137. return answerOf(n, cid == null ? null : notesTable.fetch(cid))
  138. }
  139. // A DELETE from a browser. Deleted elsewhere already = fine. Changed elsewhere since the browser's `base` (and not by
  140. // this browser's own run) = NOT deleted (the changed note is kept; the browser gets it back). A note the browser made
  141. // and never had a server id for is found by `local`. Deleted = a tombstone (text '', gone = now).
  142. static removeNote = (String userId, id, Number base, String local, String by) => {
  143. let raw = id == null || hlTypeName(id) != 'String' || id == '' ? null : notesTable.fetch(id)
  144. if (raw == null && local != null && local != '') { raw = noteOfLocal(userId, local) }
  145. if (raw == null || raw.owner != userId) { return { ok = true } }
  146. if (!isAlive(raw)) { return { ok = true } }
  147. if (!continues(raw, by, base) && !(local != null && local != '' && raw.local == local && raw.by == by)) { return { kept = true id = raw.id edited = raw.edited text = raw.text } }
  148. notesTable.update(raw.id, recordOf(raw, '', raw.edited, now(), raw.by == null ? '' : raw.by, raw.since == null ? 0 : raw.since))
  149. return { ok = true }
  150. }

Branches

Latest commits

  • 383cce23|||PR Prevent saved local edits from restoring deleted notesAtlas
  • 99ca1cf6Merge branch 'vendor-day56' into mainAtlas
  • 649cef37|||PR Vendor Hybriel day56Atlas
  • aadd0abcMerge branch 'hl-standard-2' into mainAtlas
  • 52ffac9f|||PR Keyboard and reload checks for the notes gate (HL-STANDARD F21, F5)Atlas
  • 617ee8c0Merge branch 'hl-standard' into mainAtlas
  • 452458ee|||PR Type the function parameters of notes' own .hl files (HL-STANDARD L10)Atlas
  • 23758cd6Re-vendor complete Hybriel plugins and runtime to day44mre
  • 5d7cc700Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 3e07b1cfVendor complete Hybriel day39 plugin sources and librariesmre
  • d7366702Check session cookie flags after login and assert anonymous GET stays cookielessmre
  • 16bc8fd8Re-vendor Hybriel day38b with the mpackdb store-open race fixmre
  • d860dc2dnotes#12: the markup names its own rules instead of hand-written class strings: the selected row is `aria-current="page"` (the list-first theme rule styles it, no class), the delete question is `Style.pAsk` and follows its own `open` attribute (bound to `asking`), the error line is `Style.pMessage` (body once in lib/looks.hl, rule in notes.hl, main.hl and loginfailed.hl). The CSS declarations are the same, only selectors changed (.ask -> p.ask, .ask.open -> p.ask[open], .message -> p.message); the pages look the same (AE 0 at 4 sizes). main.hl's selectorClass in/out stays (login.js reads it).mre
  • 2e7f8664notes#13: the list and back links pass the note key from the loop (emit pick(e, r.key)) instead of writing data-note into the View and reading it back with getAttribute; the two window.history.replaceState calls and the identcode e.target.value read stay (hl:web has no replace-navigation, a bound hidden input is not adopted on a script's change event), see NO13 reportmre
  • 4dc98282notes#15: a deleted note made in this tab leaves the list at once (the delete now removes this tab's edit under every key the row has: local id, server id); gate: browser.mjs notes#15 section, red on the old codemre
  • e4e7402fnotes#11: plain declarations instead of let for variables never reassigned (components/notes.hl 3, lib/jsoncheck.hl 5, lib/util.hl 2, tools/migrate-short-ids.hl 8)mre
  • 27b6a09enotes#8: Style blocks in the nested form (note-list, note-editor, .ask with their children nested, :empty as a nested pseudo-class); the same CSS rules, the pages look the samemre
  • 71a9538bnotes: B27W report, log linemre
  • 78ba0073notes: an ident access key (Authorization: Bearer idt_…) reads and writes its identity's notes through /api/me and /api/notes; gate tests/ident-access-notes.mjs (ident#27)mre
  • 3de46dacnotes: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 73+5 failing → 78 passedmre