gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitef9f9feeef9f9feere-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)mreef9f9fee/lib/api.hl

4.7 KB

  1. // lib/api.hl — THE FUNCTION ROUTES (notes mission 002, code order): thin wrappers — check the input and the session,
  2. // call the topic (lib/notes.hl, lib/users.hl), answer.
  3. //
  4. // THE LOGIN BUTTON'S RETURN (ident README "How apps use ident"): /login/callback?ident_code=&next= → the user
  5. // (lib/users.hl userOfLoginCode) → the session is signed in → back to `next` (lib/api-helpers.hl safePath). A FAILED
  6. // LOGIN is a page (components/loginfailed.hl): the reason is parked in the session, then → /login/failed.
  7. // These two write the session: project.hl hands them the request and the session store BY REFERENCE (&req, &sessions);
  8. // a by-value copy would lose the write (tickets mission 010, gitoria mission 002).
  9. //
  10. // THE JSON API (ticket notes#2; since notes#4 the page syncs through its faces, components/notes.hl). JSON, the session cookie is the login.
  11. // GET /api/me → { user } (an opaque id of the signed-in user, or null)
  12. // GET /api/notes → { notes: [{ id, edited, gone }] } every note of the user, tombstones included
  13. // GET /api/notes?id=<id> → { id, text, edited, created }
  14. // POST /api/notes { op: 'save', id, local, text, base, at } → { id, edited, text, copy? }
  15. // POST /api/notes { op: 'delete', id, base } → { ok } | { kept, id, edited, text }
  16. // Invalid JSON is refused by lib/jsoncheck.hl before JSON.parse sees it (hybriel #6); unknown/mistyped fields → 400.
  17. import { Response } from 'hl:http1'
  18. import { randomBytes } from 'hl:crypto'
  19. import { userOfLoginCode, userIdOfSession } from './users.hl'
  20. import { metaRows, noteView, pushNote, removeNote } from './notes.hl'
  21. import { jsonErrorAt } from './jsoncheck.hl'
  22. import { reply, isText, isTime, safePath } from './api-helpers.hl'
  23. static loginFailed = (&req, &sessions, why) => {
  24. let s = req.session
  25. fresh = s == null
  26. if (fresh) { s = sessions.mint() }
  27. s.data.loginError = why
  28. sessions.save(s)
  29. res = new Response('login failed: ' + why, { status = 302 headers = { 'Location' = '/login/failed' 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' } })
  30. if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }
  31. return res
  32. }
  33. // the function route gets the cookie's session as req.session (hybriel #11); none yet → minted here
  34. static loginCallback = (route, &req, &sessions) => {
  35. if (req.method != 'GET') { return loginFailed(&req, &sessions, 'GET only') }
  36. q = req.query != null ? req.query : {}
  37. code = q.ident_code
  38. if (code == null || code == '') { return loginFailed(&req, &sessions, 'ident sent no login code') }
  39. x = userOfLoginCode(code)
  40. if (x.error != null) { return loginFailed(&req, &sessions, x.error) }
  41. let s = req.session
  42. fresh = s == null
  43. if (fresh) { s = sessions.mint() }
  44. s.user = { id = x.user.id }
  45. s.data.tag = randomBytes(16)
  46. s.data.loginError = null
  47. sessions.save(s)
  48. res = new Response('logged in', { status = 302 headers = { 'Location' = safePath(q.next) 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' } })
  49. if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }
  50. return res
  51. }
  52. static apiMe = (route, req) => {
  53. u = userIdOfSession(req.session)
  54. return reply(200, { user = u })
  55. }
  56. static apiNotes = (route, req) => {
  57. u = userIdOfSession(req.session)
  58. if (u == null) { return reply(401, { error = 'log in with ident first' }) }
  59. q = req.query != null ? req.query : {}
  60. if (req.method == 'GET') {
  61. if (q.id != null) {
  62. v = noteView(u, q.id)
  63. if (v == null) { return reply(404, { error = 'no such note' }) }
  64. return reply(200, v)
  65. }
  66. return reply(200, { notes = metaRows(u) })
  67. }
  68. if (req.method != 'POST') { return reply(405, { error = 'GET or POST only' }) }
  69. if (req.body == null || req.body == '') { return reply(400, { error = 'a JSON body is needed' }) }
  70. at = jsonErrorAt(req.body)
  71. if (at >= 0) { return reply(400, { error = 'invalid JSON at character ' + at }) }
  72. b = JSON.parse(req.body)
  73. if (b == null || hlTypeName(b) != 'Hybrid') { return reply(400, { error = 'the body must be a JSON object' }) }
  74. if (b.op == 'save') {
  75. if (!isText(b.id) || !isText(b.local) || !isText(b.text) || !isTime(b.base) || !isTime(b.at)) { return reply(400, { error = 'save needs id, local, text (strings) and base, at (numbers)' }) }
  76. if (b.local.length > 64 || b.id.length > 64) { return reply(400, { error = 'id / local too long' }) }
  77. r = pushNote(u, b.id, b.local, b.text, b.base, b.at, '')
  78. if (r.error != null) { return reply(400, r) }
  79. return reply(200, r)
  80. }
  81. if (b.op == 'delete') {
  82. if (!isText(b.id) || !isTime(b.base)) { return reply(400, { error = 'delete needs id (string) and base (number)' }) }
  83. return reply(200, removeNote(u, b.id, b.base, '', ''))
  84. }
  85. return reply(400, { error = "op must be 'save' or 'delete'" })
  86. }

Branches

Latest commits

  • ef9f9feere-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
  • 5485467cdocker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 86ea4a85deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • edeab20fnotes mission 015: mission, report, LOG, STATUSmre
  • 542a0383notes mission 015: e2e-chrome E2E_HOST (own cookie jar in the shared Chrome)mre
  • 8b30f510notes mission 015: main-menu from layouts 0bcc629, current = true kept (route marks only /), server-HTML marker checksmre
  • 84b574c4notes mission 015: re-vendor plugins from hybriel f9614007 (incl. web, #117)mre
  • 447da53dnotes mission 014: report — remaining gates and lisbeth E2E proofmre
  • fd9f92c3notes mission 014: report (partial), LOG, STATUSmre
  • b2501360notes mission 014: open() sets asking=false before flush(0) (hybriel#196)mre
  • 9414ce59notes mission 014: merge theme 76f99f6, components 3794c59, strict single-press openAsk (fails on old open())mre
  • c08c655anotes 013: theme re-copied from layouts 76f99f6 (menu marks the current page), current = true on the menu item, e2e marker checks, reportmre
  • 21c8f65dnotes mission 012: STATUSmre
  • 0bbb3dd9notes mission 012: delete section of the browser gate de-flaked (openAsk: calm wait, press again when the question closes by itself); report 012mre
  • f431bdf8notes mission 011: notes on the layouts theme (ListFirst shell), styles cut, check-theme gate, E2E extended; reportmre
  • 1a8603d1notes mission 011 WIP: deploy.sh gate summary also matches check-theme, browser.mjs logout via the user menumre
  • dae2e508notes mission 011 WIP: theme dbe3de7, shell on AppHeader/MainMenu/UserMenu/Sidebar + ListFirst, styles cut to the app's own, check-theme gate, e2e auditmre
  • fd36da27notes mission 010: the note editor is the Hybriel MdEditor block (components.hybriel 1442e58, verbatim); md-editor.js, its route, script tag and offlineFiles retired; gate 66/0, realdata-offline 12/0, round-trip and Chrome E2E tests; report 010mre
  • efa92170notes tests/identkit.mjs: refuse a taken ident port, cold-wait deadline, emit, identities; mission 009 reportmre
  • f3dec0f1Merge branch 'w7'mre