gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite843ff7ce843ff7cOne-time purge of miserit-anthar#94 (#94, identity rule): idempotent step in lib/migrate.hl, fixture gate tests/purge94.mjsmree843ff7c/lib/events.hl

10.6 KB

  1. // lib/events.hl — THE HISTORY OF A TICKET: an append-only event per change. Statics only, the server realm.
  2. // eventsTable pk @id index @ticket APPEND ONLY: { ticket (ticket @id), seq
  3. // (1, 2, … per ticket), kind, author, user?, text, from, to, created, oldId? }
  4. // kind = 'created' | 'comment' | 'state' | 'edit' | 'link' | 'assign'
  5. // 'assign' (ticket #20): { assignee (users @id, '' = unassigned), previous (users @id, '') }
  6. // 'link' (ticket #4, mission 017): { linkKind ('parent' | 'blockedBy'), action
  7. // ('set' | 'removed'), other, otherProject, otherNumber, previous?, previousProject?,
  8. // previousNumber? } — on the CHILD / the BLOCKED ticket (the one whose relation changed).
  9. // 'edit' (ticket #8, mission 014): { oldSubject, oldSummary, subject, summary } —
  10. // the values before AND after; the ticket record then carries the new ones.
  11. // `user` (users @id, users.hl) since ticket #7: the event's author is that
  12. // user's CURRENT display name; `author` keeps the name at writing time.
  13. // 'comment' (mission 016): the event keeps the FIRST text; edits, a deletion and likes are kept by
  14. // lib/comments.hl, and the rows here show the comment as it is now (commentStateOf). Its files
  15. // (tickets#27): lib/attachments.hl (the row's `attachments`).
  16. // tickets.hl writes the events (putEvent) together with the ticket record; this file shapes and reads them.
  17. import { MPackDB } from 'hl:mpackdb'
  18. import { storageDir, countOf, merged, localStamp, ticketHref } from './util.hl'
  19. import { nameOfUser } from './users.hl'
  20. import { projectSlugOf } from './projects.hl'
  21. import { slugOf } from './tickets-helpers.hl'
  22. import { markdownBlocks } from './markdown.hl'
  23. import { commentStateOf, likersOf } from './comments.hl'
  24. import { attachmentsOf } from './attachments.hl'
  25. static eventsTable = new MPackDB(file = storageDir + '/events.db', primaryKey = '@id', indexes = ['@ticket'])
  26. // ---- writing -------------------------------------------------------------------------------------
  27. // appends one event record (built field by field by tickets.hl); answers its id, or null (eventsError says why)
  28. static putEvent = (rec) => { return eventsTable.put(rec) }
  29. static eventsError = () => { return eventsTable.lastError() }
  30. // one stored event record, or null (the event stream numbers it, lib/stream.hl)
  31. static eventRecord = (eventId) => { return eventId == null ? null : eventsTable.fetch(eventId) }
  32. // the API row of a stored event
  33. static eventRowAt = (eventId, t) => { return eventRowOf(eventsTable.fetch(eventId), t) }
  34. // ---- the reads and writes of the #20 migration (lib/migrate.hl calls these; the events table is written only here) ----
  35. // every event record, in the table's order
  36. static eventRecords = () => { return eventsTable.find(null, null) }
  37. // the event records of one ticket (ticket @id)
  38. static eventRecordsOf = (ticketId) => { return eventsTable.find('ticket', ticketId) }
  39. // a state event gets the new state names (the one exception to "append only": old names renamed in place)
  40. static renameStates = (e, nf, nt) => {
  41. eventsTable.update(e.id, merged(e, { from = nf to = nt }))
  42. }
  43. // ---- reading -------------------------------------------------------------------------------------
  44. // the history of a ticket (a record), oldest first: by stored time, then seq
  45. static historyOf = (t) => {
  46. out = []
  47. all = eventsTable.find('ticket', t.id)
  48. if (countOf(all) == 0) { return out }
  49. for (e of all) { out.push(eventRowOf(e, t)) }
  50. let i = 1
  51. while (i < out.length) {
  52. cur = out[i]
  53. let j = i - 1
  54. while (j >= 0 && (out[j].createdMs > cur.createdMs || (out[j].createdMs == cur.createdMs && out[j].seq > cur.seq))) {
  55. out[j + 1] = out[j]
  56. j = j - 1
  57. }
  58. out[j + 1] = cur
  59. i = i + 1
  60. }
  61. return out
  62. }
  63. // a ticket from before the login has no createdBy: the free-text author of its 'created' event
  64. static creatorNameOf = (t) => {
  65. all = eventsTable.find('ticket', t.id)
  66. if (countOf(all) == 0) { return '' }
  67. for (e of all) { if (e.kind == 'created') { return e.author != null ? e.author : '' } }
  68. return ''
  69. }
  70. // ---- the shapes ----------------------------------------------------------------------------------
  71. static eventRowOf = (e, t) => {
  72. let kind = e.kind
  73. // computed BEFORE the literal: inside a literal that has an `id` entry, a called function's
  74. // parameter named `id` read that entry instead (mission 011, reported as a Hybriel issue)
  75. who = e.user != null ? nameOfUser(e.user) : e.author
  76. let label = kind == 'created' ? 'opened the ticket' : (kind == 'state' ? 'changed the state' : (kind == 'edit' ? 'edited the ticket' : 'commented'))
  77. if (kind == 'link') { label = linkLabelOf(e) }
  78. let assignedTo = ''
  79. let assignedWas = ''
  80. if (kind == 'assign') {
  81. assignedTo = e.assignee != null && e.assignee != '' ? nameOfUser(e.assignee) : ''
  82. assignedWas = e.previous != null && e.previous != '' ? nameOfUser(e.previous) : ''
  83. label = assignedTo != '' ? 'assigned the ticket to ' + assignedTo : 'unassigned the ticket' + (assignedWas != '' ? ' (was ' + assignedWas + ')' : '')
  84. }
  85. row = {
  86. id = e.id
  87. ticket = e.ticket
  88. project = t != null ? projectSlugOf(t) : ''
  89. number = t != null ? t.number : 0
  90. seq = e.seq != null ? e.seq : 0
  91. kind = kind
  92. author = who
  93. userId = e.user != null ? e.user : ''
  94. text = e.text != null ? e.text : ''
  95. hasText = e.text != null && e.text != ''
  96. from = e.from != null ? e.from : ''
  97. to = e.to != null ? e.to : ''
  98. toSlug = slugOf(e.to)
  99. isState = kind == 'state'
  100. isComment = kind == 'comment'
  101. isCreated = kind == 'created'
  102. label = label
  103. when = localStamp(e.created)
  104. createdMs = e.created
  105. }
  106. // ticket #8: only an edit event carries these (the JSON of every other event is unchanged)
  107. if (kind == 'edit') {
  108. row.isEdit = true
  109. row.oldSubject = e.oldSubject != null ? e.oldSubject : ''
  110. row.oldSummary = e.oldSummary != null ? e.oldSummary : ''
  111. row.newSubject = e.subject != null ? e.subject : ''
  112. row.newSummary = e.summary != null ? e.summary : ''
  113. row.subjectChanged = row.oldSubject != row.newSubject
  114. row.summaryChanged = row.oldSummary != row.newSummary
  115. }
  116. // ticket #4: only a link event carries these
  117. if (kind == 'link') {
  118. row.isLink = true
  119. row.linkKind = e.linkKind
  120. row.linkAction = e.action
  121. row.otherRef = e.otherProject + ' #' + e.otherNumber
  122. row.otherHref = ticketHref(e.otherProject, e.otherNumber)
  123. row.previousRef = e.previous != null ? e.previousProject + ' #' + e.previousNumber : ''
  124. }
  125. // mission 016: a comment as it is NOW (lib/comments.hl). Only a changed comment carries the extra keys: an edited one
  126. // `edited`, `editedWhen`, `earlier` ([{ when, text }], the versions before, oldest first) and the newest `text`; a
  127. // deleted one `deleted`, `deletedWhen`, no text, the label "commented (deleted …)"; a liked one `likes` (the count).
  128. // Every other comment row has exactly the keys it had before.
  129. // a state change with a note is shown as a comment card too (tickets#33): it gets the same extra keys, except that a deleted note
  130. // only loses its text (the state line stays as it was)
  131. if (kind == 'state' && e.text != null && e.text != '') {
  132. ns = commentStateOf(e)
  133. row.text = ns.text
  134. row.hasText = ns.text != ''
  135. if (ns.edited) {
  136. row.edited = true
  137. row.editedWhen = localStamp(ns.editedMs)
  138. row.earlier = versionsOf(ns.earlier)
  139. }
  140. if (ns.likes > 0) { row.likes = ns.likes }
  141. }
  142. if (kind == 'comment') {
  143. cs = commentStateOf(e)
  144. if (cs.edited) {
  145. row.text = cs.text
  146. row.hasText = cs.text != ''
  147. row.edited = true
  148. row.editedWhen = localStamp(cs.editedMs)
  149. row.earlier = versionsOf(cs.earlier)
  150. }
  151. if (cs.deleted) {
  152. row.text = ''
  153. row.hasText = false
  154. row.label = 'commented (deleted ' + localStamp(cs.deletedMs) + ')'
  155. row.deleted = true
  156. row.deletedWhen = localStamp(cs.deletedMs)
  157. }
  158. if (cs.likes > 0) { row.likes = cs.likes }
  159. // tickets#27: only a comment with files carries `attachments` ([{ id, name, type, size, image, url }]; a deleted one has none)
  160. if (!cs.deleted) {
  161. files = attachmentsOf(e.id)
  162. if (files.length > 0) { row.attachments = files }
  163. }
  164. }
  165. // ticket #20: only an assign event carries these
  166. if (kind == 'assign') {
  167. row.isAssign = true
  168. row.assignee = assignedTo
  169. row.previousAssignee = assignedWas
  170. }
  171. return row
  172. }
  173. static versionsOf = (list) => {
  174. out = []
  175. for (v of list) { out.push({ when = v.when text = v.text }) }
  176. return out
  177. }
  178. // "set the parent to ident.worldapi.org #1", "removed the blocker tickets.worldapi.org #6", …
  179. static linkLabelOf = (e) => {
  180. other = e.otherProject + ' #' + e.otherNumber
  181. if (e.linkKind == 'parent') {
  182. if (e.action == 'removed') { return 'removed the parent ' + other }
  183. was = e.previous != null ? ' (was ' + e.previousProject + ' #' + e.previousNumber + ')' : ''
  184. return 'set the parent to ' + other + was
  185. }
  186. if (e.action == 'removed') { return 'removed the blocker ' + other }
  187. return 'marked it blocked by ' + other
  188. }
  189. // THE PAGE'S EVENT ROW (ticket #6): the API row + its text as Markdown blocks (`md`), and for an
  190. // edit the previous summary's blocks (`oldSummaryMd`) and the new one's (`summaryMd`, the ticket
  191. // page takes it over live). Only pages and pushes carry these — the JSON API does not.
  192. static pageEventOf = (ev) => {
  193. out = {}
  194. for (k of ev.keys()) { out[k] = ev[k] }
  195. out.md = markdownBlocks(ev.text)
  196. // mission 016: who likes a comment (users @ids) — the page works out `liked` for the user looking
  197. if (ev.isComment == true || (ev.isState == true && ev.hasText == true)) { out.likedBy = likersOf(ev.id) }
  198. if (ev.isEdit == true) {
  199. out.oldSummaryMd = markdownBlocks(ev.oldSummary)
  200. out.summaryMd = markdownBlocks(ev.newSummary)
  201. }
  202. return out
  203. }
  204. static pageEventsOf = (evs) => {
  205. out = []
  206. for (ev of evs) { out.push(pageEventOf(ev)) }
  207. return out
  208. }
  209. // ---- the one-time purge of #94, identity rule (lib/migrate.hl purgeTicket94): the writes of the events table -------------
  210. // the @ids of every event of a ticket
  211. static eventIdsOf = (ticketId) => {
  212. out = []
  213. found = eventsTable.find('ticket', ticketId)
  214. if (countOf(found) > 0) { for (e of found) { out.push(e.id) } }
  215. return out
  216. }
  217. // the link events of OTHER tickets that name `ticketId` as the other side or as the previous one: [{ id, owner (that ticket's @id) }]
  218. static linkEventsNaming = (ticketId) => {
  219. out = []
  220. all = eventsTable.find(null, null)
  221. if (countOf(all) == 0) { return out }
  222. for (e of all) {
  223. if (e.kind == 'link' && e.ticket != ticketId && (e.other == ticketId || e.previous == ticketId)) { out.push({ id = e.id owner = e.ticket }) }
  224. }
  225. return out
  226. }
  227. static removeEvent = (id) => { eventsTable.delete(id) }
  228. static compactEvents = () => { eventsTable.compactNow() }

Branches

Latest commits

  • e843ff7cOne-time purge of miserit-anthar#94 (#94, identity rule): idempotent step in lib/migrate.hl, fixture gate tests/purge94.mjsmre
  • 506a4efdmission 028: #94, identity rulemre
  • 78c7f48eRefine mpackdb holdmre
  • 3d7d89c8Record mpackdb hold until day38mre
  • f1bf7195#51 done; my subscription idmre
  • 9870b945status: #51 proof greenmre
  • e92173c6log: #51 live push proofmre
  • 72d87041log: Hermes push livemre
  • 1d69bf7dTickets conductor: park until Hermes pushmre
  • 80d8cfd1Tickets conductor: #51 livemre
  • 61641dd9Complete subscriptions deployment evidencemre
  • c980856cRecord successful subscriptions deployment and live checksmre
  • 4b4710adMerge branch 's51-subscriptions'mre
  • a731ef7eTickets conductor: S51 reviewed, deploy stepmre
  • b487b05aRecord verified direct push protocol handoffmre
  • 74e34cc3Verify encrypted push secrets and direct push retry semanticsmre
  • a1ac5cd0Push subscription notices directly to signed HyperConductor chat endpointsmre
  • d662ad27Remove superseded waker service for direct chat push protocolmre
  • c0ac927aTickets conductor: S51 redo for push protocolmre
  • ff2e8701Finalize subscription build verification and handoff reportmre