notes
All repositories: gitoria
4.6 KB
// lib/api.hl — THE FUNCTION ROUTES (notes mission 002, code order): thin wrappers — check the input and the session,// call the topic (lib/notes.hl, lib/users.hl), answer.//// THE LOGIN BUTTON'S RETURN (ident README "How apps use ident"): /login/callback?ident_code=&next= → the user// (lib/users.hl userOfLoginCode) → the session is signed in → back to `next` (lib/api-helpers.hl safePath). A FAILED// LOGIN is a page (components/loginfailed.hl): the reason is parked in the session, then → /login/failed.// These two write the session: project.hl hands them the request and the session store BY REFERENCE (&req, &sessions);// a by-value copy would lose the write (tickets mission 010, gitoria mission 002).//// THE SYNC API (ticket notes#2): what the browser's offline copy talks to. JSON, the session cookie is the login.// GET /api/me → { user } (an opaque id of the signed-in user, or null)// GET /api/notes → { notes: [{ id, edited, gone }] } every note of the user, tombstones included// GET /api/notes?id=<id> → { id, text, edited, created }// POST /api/notes { op: 'save', id, local, text, base, at } → { id, edited, text, copy? }// POST /api/notes { op: 'delete', id, base } → { ok } | { kept, id, edited, text }// Invalid JSON is refused by lib/jsoncheck.hl before JSON.parse sees it (hybriel #6); unknown/mistyped fields → 400.import { Response } from 'hl:http1'import { randomBytes } from 'hl:crypto'import { userOfLoginCode, userIdOfSession } from './users.hl'import { metaRows, noteView, pushNote, removeNote } from './notes.hl'import { jsonErrorAt } from './jsoncheck.hl'import { reply, isText, isTime, safePath } from './api-helpers.hl'static loginFailed = (&req, &sessions, why) => {let s = req.sessionfresh = s == nullif (fresh) { s = sessions.mint() }s.data.loginError = whysessions.save(s)res = new Response('login failed: ' + why, { status = 302 headers = { 'Location' = '/login/failed' 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' } })if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }return res}// the function route gets the cookie's session as req.session (hybriel #11); none yet → minted herestatic loginCallback = (route, &req, &sessions) => {if (req.method != 'GET') { return loginFailed(&req, &sessions, 'GET only') }q = req.query != null ? req.query : {}code = q.ident_codeif (code == null || code == '') { return loginFailed(&req, &sessions, 'ident sent no login code') }x = userOfLoginCode(code)if (x.error != null) { return loginFailed(&req, &sessions, x.error) }let s = req.sessionfresh = s == nullif (fresh) { s = sessions.mint() }s.user = { id = x.user.id }s.data.tag = randomBytes(16)s.data.loginError = nullsessions.save(s)res = new Response('logged in', { status = 302 headers = { 'Location' = safePath(q.next) 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' } })if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }return res}static apiMe = (route, req) => {u = userIdOfSession(req.session)return reply(200, { user = u })}static apiNotes = (route, req) => {u = userIdOfSession(req.session)if (u == null) { return reply(401, { error = 'log in with ident first' }) }q = req.query != null ? req.query : {}if (req.method == 'GET') {if (q.id != null) {v = noteView(u, q.id)if (v == null) { return reply(404, { error = 'no such note' }) }return reply(200, v)}return reply(200, { notes = metaRows(u) })}if (req.method != 'POST') { return reply(405, { error = 'GET or POST only' }) }if (req.body == null || req.body == '') { return reply(400, { error = 'a JSON body is needed' }) }at = jsonErrorAt(req.body)if (at >= 0) { return reply(400, { error = 'invalid JSON at character ' + at }) }b = JSON.parse(req.body)if (b == null || hlTypeName(b) != 'Hybrid') { return reply(400, { error = 'the body must be a JSON object' }) }if (b.op == 'save') {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)' }) }if (b.local.length > 64 || b.id.length > 64) { return reply(400, { error = 'id / local too long' }) }r = pushNote(u, b.id, b.local, b.text, b.base, b.at)if (r.error != null) { return reply(400, r) }return reply(200, r)}if (b.op == 'delete') {if (!isText(b.id) || !isTime(b.base)) { return reply(400, { error = 'delete needs id (string) and base (number)' }) }return reply(200, removeNote(u, b.id, b.base))}return reply(400, { error = "op must be 'save' or 'delete'" })}
Branches
- mainmain branch
Latest commits
- 47db4fc1notes mission 002 (3/4): code order — let only where reassigned (58 dropped; 34 left: 19 reassigned, 15 loop-bound); gates 50/0 + 18/0, live-data run = step 2mre
- 3f8cb383notes mission 002 (2/4): code order — topics, map, thin wrappers: lib/util.hl, lib/notes(-helpers).hl, lib/users.hl (+userOfLoginCode, tagOf), lib/api(-helpers).hl; project.hl = map; login routes take &req/&sessions (failed-login reason now kept); dead notes#1 functions removed; gates 50/0 + 18/0mre
- 8cda5412notes mission 002 (1/4): code order — files moved: lib/notes.hl, lib/users.hl, lib/jsoncheck.hl, components/styles.hl (imports only); gates 50/0 + 18/0, live-data run identicalmre
- 47dad68bnotes: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); gates 50/0 + 18/0mre
- a4a2b2aeantcolony#40: tracker missions moved too — references to them in missions/reports/LOG.md updatedmre
- 3dea0ef2notes: Hybriel master 190aa11d (fc838894 GC correctness, #126 closure scopes, #127); gate 50/0mre
- e27c7d71notes: Hybriel master 8efba065 (#126 GC by bytes, #48 lambda params copy; audit: nothing to fix; gate 50/0)mre
- 124613b6antcolony#40: mission references point to the moved missionsmre
- 1ca2f34dantcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
- 2bebebdanotes: Hybriel master ff51cf46 (re-vendor round)mre
- 3eff126dnotes#3: installable app (manifest + own icon/favicon; notes' own sw.js kept)mre
- 9883c540deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
- eee693b8deploy.sh: never send .git or .gitignore to Byrodinmre
- c8904061State of 2026-09-27, before the move to gitoriamre