notes
All repositories: gitoria
8.8 KB
// lib/notes.hl — THE NOTES (ticket notes#1) and their SYNC (tickets notes#2, notes#4). One hl:mpackdb table,// storage/mpackdb/notes.db:// pk @id index owner { owner, text (Markdown), created, edited, gone, local, by, since, copyOf } times = ms// gone = ms a note was deleted (0 = alive; the row stays as a TOMBSTONE, /api/notes reports it)// local = the id the creating tab gave the note before it had a server id (a retried save finds it again)// by = the login tag (lib/users.hl tagOf) of the browser whose save made this version ('' = /api/notes or older)// since = the `edited` that browser had seen when it started this run of saves (its `base`; `from` is a reserved word)// copyOf = on a conflict copy: the note it was split from ('' otherwise)// The SUBJECT of a note is not stored: it is the first non-empty line of the text, without leading `#`// (CONCEPT.md "Decisions"); the page (components/notes.hl) derives it and orders the sidebar by `edited`.// Every function takes the owner's user id and only ever reaches that user's notes. Every write to the table is here.// A note's VERSION is its `edited` (ms); a browser sends the `edited` it last saw of a note (`base`).// CONTINUATION (notes#4): a browser offline queues one save after another, all with the same old `base`. The second// one would look like a conflict, so a save whose `by` + `base` are the note's `by` + `since` continues that run.// A browser that LOST a conflict keeps writing into its copy (same `copyOf`, `by`, `since`), not into a new copy each time.// Looked up in lib/notes-helpers.hl: textError (size limit), isAlive (tombstones), stampOf (version stamps), copyMark.import { keepDraft, acceptDraft, deleteDrafts } from './drafts.hl'import { MPackDB } from 'hl:mpackdb'import { now } from 'hl:time'import { storageDir, countOfList } from './util.hl'import { textError, isAlive, stampOf, copyMark } from './notes-helpers.hl'static notesTable = new MPackDB(file = storageDir + '/notes.db', primaryKey = '@id', indexes = ['owner'])// the stored record of note `n` with a new text / version / tombstone / run — every field, written out (hybriel#23)static recordOf = (Hybrid n, String text, Number edited, Number gone, String by, Number since) => {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 }}// one note of this owner, or nullstatic noteOf = (String userId, id) => {if (id == null || hlTypeName(id) != 'String' || id == '') { return null }n = notesTable.fetch(id)if (n == null || n.owner != userId || !isAlive(n)) { return null }return n}// THE PAGE'S LIST (components/notes.hl `notes`, the push notesChanged): every live note of the owner, with its textstatic notesFor = (String userId) => {out = []if (userId == null) { return out }found = notesTable.find('owner', userId)let i = 0n = countOfList(found)while (i < n) {r = found[i]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 }) }i = i + 1}return out}// what /api/notes compares: id, edited, gone (no text) of every note of the owner, tombstones includedstatic metaRows = (String userId) => {found = notesTable.find('owner', userId)out = []let i = 0n = countOfList(found)while (i < n) {out.push({ id = found[i].id edited = found[i].edited gone = isAlive(found[i]) ? 0 : found[i].gone })i = i + 1}return out}// one note with its text, or nullstatic noteView = (String userId, id) => {n = noteOf(userId, id)return n == null ? null : { id = n.id text = n.text edited = n.edited created = n.created }}static newNote = (String userId, String text, String local, Number edited, String by, Number since, String copyOf) => {t = now()id = notesTable.put({ owner = userId text = text created = t edited = stampOf(edited, 0) gone = 0 local = local by = by since = since copyOf = copyOf })return id}// the live note this browser made under its own id `local` (a save of a never-synced note, or a retry), or nullstatic noteOfLocal = (String userId, String local) => {if (local == null || local == '') { return null }mine = notesTable.find('owner', userId)let i = 0let n = nullc = countOfList(mine)while (i < c && n == null) {if (isAlive(mine[i]) && mine[i].local == local) { n = mine[i] }i = i + 1}return n}// the copy this browser (`by`, run `since`) already made of note `id` when it lost a conflict, or nullstatic copyOfRun = (String userId, String id, String by, Number since) => {if (by == null || by == '') { return null }mine = notesTable.find('owner', userId)let i = 0let n = nullc = countOfList(mine)while (i < c && n == null) {x = mine[i]if (isAlive(x) && x.copyOf == id && x.by == by && x.since == since) { n = x }i = i + 1}return n}// does a save (`by`, `base`) continue the run of saves that made the note's current version?static continues = (Hybrid n, String by, Number base) => { return n.edited == base || (by != null && by != '' && n.by == by && n.since == base) }static answerOf = (Hybrid n, Hybrid copy, String by, Number at) => {acceptDraft(n.owner, n.id, by, at)return { id = n.id edited = n.edited text = n.text copy = copy == null ? null : { id = copy.id edited = copy.edited text = copy.text } }}// A SAVE from a browser: { id (server id or ''), local (the browser's own id of a never-synced note), text,// base (the `edited` the browser last saw, 0 = never), at (when the browser made the edit), by (its login tag, '' = none) }// → { id, edited, text, copy? }: `text` is what the note is NOW; `copy` = { id, edited, text } is the version that// lost a conflict, kept as a note of its own — nothing is ever lost.static pushNote = (String userId, String id, String local, String text, Number base, Number at, String by) => {bad = textError(text)if (bad != null) {owned = id != '' ? noteOf(userId, id) : noteOfLocal(userId, local)if (owned != null) {why = keepDraft(userId, owned.id, by, text, base, at)if (why != null) { return { error = why } }}return { error = bad }}let n = nullif (id != '') { n = noteOf(userId, id) }if (n == null) { n = noteOfLocal(userId, local) }if (n == null) {// a new note (or one deleted elsewhere while this browser edited it: the edit becomes a note again)nid = newNote(userId, text, local, at > 0 ? at : now(), by, 0, '')if (nid == null) { return { error = 'could not store the note: ' + notesTable.lastError() } }return answerOf(notesTable.fetch(nid), null, by, at)}if (n.text == text) { return answerOf(n, null, by, at) }// this browser lost a conflict on this note before: its run goes on in its copyhad = copyOfRun(userId, n.id, by, base)if (had != null) {notesTable.update(had.id, recordOf(had, text + copyMark, stampOf(at, had.edited), 0, by, base))return answerOf(n, notesTable.fetch(had.id), by, at)}if (continues(n, by, base)) {notesTable.update(n.id, recordOf(n, text, stampOf(at, n.edited), 0, by, base))after = notesTable.fetch(n.id)if (after == null || after.text != text) { return { error = 'could not store the note: ' + notesTable.lastError() } }return answerOf(after, null, by, at)}// CONFLICT: changed here AND there. The later edit wins (last write wins); the other version becomes a copy.if (at > n.edited) {cid = newNote(userId, n.text + copyMark, '', n.edited, n.by == null ? '' : n.by, n.since == null ? 0 : n.since, n.id)notesTable.update(n.id, recordOf(n, text, stampOf(at, n.edited), 0, by, base))return answerOf(notesTable.fetch(n.id), cid == null ? null : notesTable.fetch(cid), by, at)}cid = newNote(userId, text + copyMark, '', at, by, base, n.id)return answerOf(n, cid == null ? null : notesTable.fetch(cid), by, at)}// A DELETE from a browser. Deleted elsewhere already = fine. Changed elsewhere since the browser's `base` (and not by// this browser's own run) = NOT deleted (the changed note is kept; the browser gets it back). A note the browser made// and never had a server id for is found by `local`. Deleted = a tombstone (text '', gone = now).static removeNote = (String userId, id, Number base, String local, String by) => {let raw = id == null || hlTypeName(id) != 'String' || id == '' ? null : notesTable.fetch(id)if (raw == null && local != null && local != '') { raw = noteOfLocal(userId, local) }if (raw == null || raw.owner != userId) { return { ok = true } }if (!isAlive(raw)) { return { ok = true } }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 } }deleteDrafts(userId, raw.id)notesTable.update(raw.id, recordOf(raw, '', raw.edited, now(), raw.by == null ? '' : raw.by, raw.since == null ? 0 : raw.since))return { ok = true }}
Branches
Latest commits
- 9cc92bdfKeep conflict and history fixes while awaiting queued save supportAtlas
- a5c27cb6Allow real edits back to earlier text and honor configured identAtlas
- 15c46ced|||PR Preserve note edits, conflict bases and visible historyAtlas
- 4c6e0bf6Merge branch 'notes13-day61' into mainAtlas
- eb51864f|||PR Back/Forward via navigate, server drafts, Hybriel day61Atlas
- 3537cf7eMerge branch 'vendor-day59' into mainAtlas
- c13d188b|||PR Vendor Hybriel day59Atlas
- 24c95433Merge branch 'notes17-ghost-note' into mainAtlas
- ae1075b2Preserve rejected drafts during note ID adoptionAtlas
- 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