gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf767b24df767b24dtickets: 111 needless let became plain declarations (3 in ticket.hl, ticket_list.hl, comments.hl and 108 never-reassigned, not captured loop-body ones); the 212 really reassigned lets stay (tickets#44)mref767b24d/lib/stream.hl

8.6 KB

  1. // lib/stream.hl — THE EVENT STREAM FOR PROGRAMS (ticket #26): every change of a ticket, live, as Server-Sent Events.
  2. // Statics only, the server realm. `GET /api/events` (lib/api.hl getEvents) checks the token and calls openStream.
  3. //
  4. // streamTable pk *n (the table's counter: 1, 2, 3, … — THE EVENT ID a program resumes from)
  5. // { event (events @id), ticket (tickets @id), project (projects @id), number, kind, actor (users @id
  6. // or ''), actorName, state (the ticket's state after the write), assignee (users @id, ''),
  7. // previous (an 'assign' event: who had it, ''), text (≤ 300 characters), created (epoch ms) }
  8. // One row per history event, written by tickets.hl (recordEvent) right after the write is complete,
  9. // so `state` / `assignee` are the ticket's NEW ones. Rows exist from the first write after ticket #26
  10. // went live; older history is not numbered (a program reads it from GET /api/projects/<p>/tickets/<n>).
  11. //
  12. // THE WIRE (text/event-stream, hl:http1's native SSE: `sseStart` on the request's respond handle,
  13. // `sseSend` per frame; hl:http1 writes ": connected" first and a ":" comment every 5 s — the keep-alive):
  14. // data: {"id":42,"at":"…","project":"routger","ticket":"…","ref":"#5","number":5,"kind":"comment",
  15. // "actor":"Apollo","state":"open","assignee":"","text":"…","href":"https://…/routger/5"}
  16. // id: 42
  17. // A reconnect with `Last-Event-ID: 42` (EventSource does it itself) or `?after=42` first gets every row after 42
  18. // that passes the filters, oldest first, then the live ones — nothing twice, nothing missed (one event loop: no
  19. // write can slip between the replay and the registration).
  20. //
  21. // WHO SEES WHAT: reading tickets is public (CONCEPT.md point 1), so a user's token sees every project; a project
  22. // key (tktc_, ticket #21) only its own project. Filters (query): project (slugs or ids, comma separated),
  23. // assignee (a name, a users id, or `me`), notMine (skip what the token's user wrote; a user token only).
  24. import { timestamp } from 'hl:time'
  25. import { sseStart, sseSend, sseAlive } from 'hl:http1'
  26. import { MPackDB } from 'hl:mpackdb'
  27. import { storageDir, countOf, clean, isDigits, ticketHref } from './util.hl'
  28. import { publicUrl } from './users.hl'
  29. import { projectRecord, projectByRef, userByRef } from './projects.hl'
  30. import { eventRecord, eventRowOf } from './events.hl'
  31. static streamTable = new MPackDB(file = storageDir + '/stream.db', primaryKey = '*n', indexes = [])
  32. static textMax = 300
  33. // THE TRANSPORT: hl:http1's native SSE, imported by name (hybriel#144): sseStart turns a request into a stream (a
  34. // connection id, or null), sseSend writes one frame (true / false = that stream is gone), sseAlive asks without
  35. // sending. hl:http1 writes ": connected" first and a ":" comment every 5 s (the keep-alive) and reaps a closed peer at
  36. // once. Plain HTTP only (on hl:http1's own TLS sseStart answers null; on Byrodin TLS ends in nginx). The pages' browser
  37. // build binds these to a refusal that throws only when called — and the browser never calls them.
  38. // the live subscribers: { conn (hl:http1 SSE connection id), projects (ids or null = all), assignee, notMine }
  39. static subscribers = []
  40. // ---- writing: one row per history event ---------------------------------------------------------
  41. // `t` = the ticket record AFTER the write. Answers the new event id (n), or null.
  42. static recordEvent = (eventId, t) => {
  43. if (eventId == null || t == null) { return null }
  44. e = eventRecord(eventId)
  45. if (e == null) { return null }
  46. row = eventRowOf(e, t)
  47. let text = row.text
  48. if (e.kind == 'created') { text = t.summary != null && t.summary != '' ? t.summary : row.text }
  49. if (e.kind == 'edit') { text = e.summary != null ? e.summary : '' }
  50. if (e.kind == 'link' || e.kind == 'assign') { text = row.label }
  51. short = text.length > textMax ? text.slice(0, textMax) : text
  52. n = streamTable.put({ event = e.id ticket = t.id project = t.project number = t.number kind = e.kind actor = e.user != null ? e.user : '' actorName = row.author state = t.state assignee = t.assignee != null ? t.assignee : '' previous = e.kind == 'assign' && e.previous != null ? e.previous : '' text = short created = e.created })
  53. if (n == null) {
  54. console.log('stream: could not number event ' + eventId + ': ' + streamTable.lastError())
  55. return null
  56. }
  57. publish(streamTable.fetch(n))
  58. return n
  59. }
  60. // ---- the payload a program gets ----------------------------------------------------------------------
  61. static payloadOf = (r) => {
  62. p = projectRecord(r.project)
  63. slug = p != null ? p.slug : ''
  64. who = r.assignee != '' ? userByRef(r.assignee, null) : null
  65. return { id = r.n at = timestamp(r.created) project = slug ticket = r.ticket ref = '#' + r.number number = r.number kind = r.kind actor = r.actorName state = r.state assignee = who != null && who.user != null ? who.user.name : '' text = r.text href = publicUrl + ticketHref(slug, r.number) }
  66. }
  67. // one SSE message: the data line, then its id line (hl:http1 writes "data: " before and the blank line after)
  68. static frameOf = (r) => { return JSON.stringify(payloadOf(r)) + newline + 'id: ' + r.n }
  69. // the language has no escapes: a newline is written as itself
  70. static newline = "
  71. "
  72. // ---- filters --------------------------------------------------------------------------------------
  73. static passes = (sub, r) => {
  74. if (sub.projects != null && !sub.projects.includes(r.project)) { return false }
  75. if (sub.assignee != null && r.assignee != sub.assignee && r.previous != sub.assignee) { return false }
  76. if (sub.notMine != null && r.actor == sub.notMine) { return false }
  77. return true
  78. }
  79. // the query of GET /api/events → { sub } or { error, field, forbidden? }.
  80. // `who` = { user } (a user's token) or { project } (a project key: that project only); `lastId` = the
  81. // Last-Event-ID header or null.
  82. static subscriptionOf = (q, lastId, who) => {
  83. let projects = null
  84. if (who.project != null) { projects = [who.project] }
  85. want = clean(q.project)
  86. if (want != '') {
  87. ids = []
  88. for (part of want.split(',')) {
  89. ref = part.trim()
  90. if (ref != '') {
  91. p = projectByRef(ref)
  92. if (p == null) { return { error = 'no such project: ' + ref field = 'project' } }
  93. if (who.project != null && p.id != who.project) { return { error = 'this key reaches only its own project, not ' + ref field = 'project' forbidden = true } }
  94. ids.push(p.id)
  95. }
  96. }
  97. if (ids.length > 0) { projects = ids }
  98. }
  99. let assignee = null
  100. a = clean(q.assignee)
  101. if (a != '') {
  102. if (a == 'me') {
  103. if (who.user == null) { return { error = 'assignee=me needs a user token' field = 'assignee' } }
  104. assignee = who.user.id
  105. } else {
  106. f = userByRef(a, null)
  107. if (f.error != null) { return { error = f.error field = 'assignee' } }
  108. assignee = f.user.id
  109. }
  110. }
  111. let notMine = null
  112. nm = clean(q.notMine).toLowerCase()
  113. if (nm != '' && nm != '0' && nm != 'false' && nm != 'no') {
  114. if (who.user == null) { return { error = 'notMine needs a user token (a project key writes for many people)' field = 'notMine' } }
  115. notMine = who.user.id
  116. }
  117. let after = null
  118. since = clean(lastId) != '' ? clean(lastId) : clean(q.after)
  119. if (since != '') {
  120. if (!isDigits(since)) { return { error = 'after / Last-Event-ID must be an event id (digits)' field = 'after' } }
  121. after = toNumber(since)
  122. }
  123. return { sub = { projects = projects assignee = assignee notMine = notMine after = after } }
  124. }
  125. // ---- the stream ------------------------------------------------------------------------------------
  126. // turns the request into the event stream (`respond` = req.respond), sends what was missed since
  127. // `sub.after`, then registers it for the live ones. Answers the SSE connection id, or null.
  128. static openStream = (respond, sub) => {
  129. conn = sseStart(respond)
  130. if (conn == null) { return null }
  131. if (sub.after != null) {
  132. let n = sub.after + 1
  133. last = streamTable.count()
  134. while (n <= last) {
  135. r = streamTable.fetch(n)
  136. if (r != null && passes(sub, r)) { sseSend(conn, frameOf(r)) }
  137. n = n + 1
  138. }
  139. }
  140. subscribers.push({ conn = conn projects = sub.projects assignee = sub.assignee notMine = sub.notMine })
  141. return conn
  142. }
  143. // a new row to every live subscriber whose filters it passes; a closed stream drops out here
  144. static publish = (r) => {
  145. if (r == null || subscribers.length == 0) { return 0 }
  146. let sent = 0
  147. let gone = false
  148. frame = frameOf(r)
  149. for (s of subscribers) {
  150. if (passes(s, r)) {
  151. if (sseSend(s.conn, frame)) { sent = sent + 1 } else { gone = true }
  152. } else if (!sseAlive(s.conn)) { gone = true }
  153. }
  154. if (gone) { prune() }
  155. return sent
  156. }
  157. static prune = () => {
  158. keep = []
  159. for (s of subscribers) { if (sseAlive(s.conn)) { keep.push(s) } }
  160. while (subscribers.length > 0) { subscribers.pop() }
  161. for (s of keep) { subscribers.push(s) }
  162. }

Branches

Latest commits

  • f767b24dtickets: 111 needless let became plain declarations (3 in ticket.hl, ticket_list.hl, comments.hl and 108 never-reassigned, not captured loop-body ones); the 212 really reassigned lets stay (tickets#44)mre
  • 16c0d3b5tickets: the modal window is phone first: the full-screen sheet is the base rule, the centred window sits in the min-width 60rem block (tickets#39)mre
  • 090ee809tickets: every component's styles in its own nested Style {} block; styles.hl keeps only the theme inherit, the accent and the site-wide rules (tickets#36)mre
  • d395763ftickets: ident-access gate ports 8769/8768 (8797 is taken on loreana)mre
  • afb9a403tickets: an ident access key (Authorization: Bearer idt_…) works in the API (ident#27 app side): lib/ident-access.hl copied from components, read keys GET only, ident down 503; gate tests/ident-access-tickets.mjsmre
  • 48c1ba79tickets: md-paste-files gate with the file manager casesmre
  • f04afc43tickets: MdEditor from components 59704ce (a file manager copy still attaches, pipe rows, tickets#49)mre
  • 06599fa0tickets: MdEditor from components b0f1ec2 (a clipboard with text always pastes the text, tickets#49); real Ctrl+V gatemre
  • 2e1b116ftickets: re-vendor Hybriel master acaceaf3 (framework ec8d6c1d): plugins/web patch queue while a page is built, anonymous session cookies refresh; gate waits for the comment editors to draw (W174) and clicks the form editor when it stands still; security gate: main's code runs on its own binary, lazy-session cookie checksmre
  • 361bc0fetickets: re-vendor Hybriel 7a684920 (WIP, not deployed); on Error exits 1 at startup; migrate passes its log by reference; gate checks follow lazy sessions and the 413 refusalmre
  • 8a69999cident#28: /login/callback never signs out the session it finds; a valid code puts the user on the same sessionmre
  • 337e9b21tickets#37: a comment's files are open to everyone who can read the commentmre
  • 1800600ftickets: write the standby service out in full so the boot gate can read the main servicemre
  • 8179d287tickets: standby service on its port plus 10 and a deploy that hands over through it without a gapmre
  • 0398145ctickets: the table grid rule is marked functional for check-thememre
  • 40fddc20tickets takes components e54f83a: tables get thin grey lines from the theme, desktop toolbar stays one row with the Table button, F4 check counts 11 buttonsmre
  • c1b77d61tickets#35: text typed before the editor is built is taken in once by the build (the input handler no longer writes it to the field first, which doubled the first letters); the gate failure shows the surfacemre
  • 32bf1873tickets#35: the gate types right after the load with the CPU slowed, no waitingmre
  • 64ea9ca2tickets#35: text typed into the reply editor before the page has finished starting is kept; the gate types right after loadmre
  • ccd3a327tickets: the security gate expects frame-src to be exactly the ident address (the sign-in frame), still no wildcard or selfmre