gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchm32-move772e0696|||PR Allow project admins to move tickets between their projectsmrem32-move/lib/events.hl

11.0 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) { if (e.sourceOnly != true) { 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 == 'move') { label = e.text }
  78. if (kind == 'link') { label = linkLabelOf(e) }
  79. let assignedTo = ''
  80. let assignedWas = ''
  81. if (kind == 'assign') {
  82. assignedTo = e.assignee != null && e.assignee != '' ? nameOfUser(e.assignee) : ''
  83. assignedWas = e.previous != null && e.previous != '' ? nameOfUser(e.previous) : ''
  84. label = assignedTo != '' ? 'assigned the ticket to ' + assignedTo : 'unassigned the ticket' + (assignedWas != '' ? ' (was ' + assignedWas + ')' : '')
  85. }
  86. row = {
  87. id = e.id
  88. ticket = e.ticket
  89. project = t != null ? projectSlugOf(t) : ''
  90. number = t != null ? t.number : 0
  91. seq = e.seq != null ? e.seq : 0
  92. kind = kind
  93. author = who
  94. userId = e.user != null ? e.user : ''
  95. text = e.text != null ? e.text : ''
  96. hasText = e.text != null && e.text != ''
  97. from = e.from != null ? e.from : ''
  98. to = e.to != null ? e.to : ''
  99. toSlug = slugOf(e.to)
  100. isState = kind == 'state'
  101. isComment = kind == 'comment'
  102. isCreated = kind == 'created'
  103. label = label
  104. when = localStamp(e.created)
  105. createdMs = e.created
  106. }
  107. // ticket #8: only an edit event carries these (the JSON of every other event is unchanged)
  108. if (kind == 'edit') {
  109. row.isEdit = true
  110. row.oldSubject = e.oldSubject != null ? e.oldSubject : ''
  111. row.oldSummary = e.oldSummary != null ? e.oldSummary : ''
  112. row.newSubject = e.subject != null ? e.subject : ''
  113. row.newSummary = e.summary != null ? e.summary : ''
  114. row.subjectChanged = row.oldSubject != row.newSubject
  115. row.summaryChanged = row.oldSummary != row.newSummary
  116. }
  117. // ticket #4: only a link event carries these
  118. if (kind == 'link') {
  119. row.isLink = true
  120. row.linkKind = e.linkKind
  121. row.linkAction = e.action
  122. row.otherRef = e.otherProject + ' #' + e.otherNumber
  123. row.otherHref = ticketHref(e.otherProject, e.otherNumber)
  124. row.previousRef = e.previous != null ? e.previousProject + ' #' + e.previousNumber : ''
  125. }
  126. // mission 016: a comment as it is NOW (lib/comments.hl). Only a changed comment carries the extra keys: an edited one
  127. // `edited`, `editedWhen`, `earlier` ([{ when, text }], the versions before, oldest first) and the newest `text`; a
  128. // deleted one `deleted`, `deletedWhen`, no text, the label "commented (deleted …)"; a liked one `likes` (the count).
  129. // Every other comment row has exactly the keys it had before.
  130. // 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
  131. // only loses its text (the state line stays as it was)
  132. if (kind == 'state' && e.text != null && e.text != '') {
  133. ns = commentStateOf(e)
  134. row.text = ns.text
  135. row.hasText = ns.text != ''
  136. if (ns.edited) {
  137. row.edited = true
  138. row.editedWhen = localStamp(ns.editedMs)
  139. row.earlier = versionsOf(ns.earlier)
  140. }
  141. if (ns.likes > 0) { row.likes = ns.likes }
  142. }
  143. if (kind == 'comment') {
  144. cs = commentStateOf(e)
  145. if (cs.edited) {
  146. row.text = cs.text
  147. row.hasText = cs.text != ''
  148. row.edited = true
  149. row.editedWhen = localStamp(cs.editedMs)
  150. row.earlier = versionsOf(cs.earlier)
  151. }
  152. if (cs.deleted) {
  153. row.text = ''
  154. row.hasText = false
  155. row.label = 'commented (deleted ' + localStamp(cs.deletedMs) + ')'
  156. row.deleted = true
  157. row.deletedWhen = localStamp(cs.deletedMs)
  158. }
  159. if (cs.likes > 0) { row.likes = cs.likes }
  160. // tickets#27: only a comment with files carries `attachments` ([{ id, name, type, size, image, url }]; a deleted one has none)
  161. if (!cs.deleted) {
  162. files = attachmentsOf(e.id)
  163. if (files.length > 0) { row.attachments = files }
  164. }
  165. }
  166. // ticket #20: only an assign event carries these
  167. if (kind == 'assign') {
  168. row.isAssign = true
  169. row.assignee = assignedTo
  170. row.previousAssignee = assignedWas
  171. }
  172. return row
  173. }
  174. static versionsOf = (list) => {
  175. out = []
  176. for (v of list) { out.push({ when = v.when text = v.text }) }
  177. return out
  178. }
  179. // "set the parent to ident.worldapi.org #1", "removed the blocker tickets.worldapi.org #6", …
  180. static linkLabelOf = (e) => {
  181. other = e.otherProject + ' #' + e.otherNumber
  182. if (e.linkKind == 'parent') {
  183. if (e.action == 'removed') { return 'removed the parent ' + other }
  184. was = e.previous != null ? ' (was ' + e.previousProject + ' #' + e.previousNumber + ')' : ''
  185. return 'set the parent to ' + other + was
  186. }
  187. if (e.action == 'removed') { return 'removed the blocker ' + other }
  188. return 'marked it blocked by ' + other
  189. }
  190. // THE PAGE'S EVENT ROW (ticket #6): the API row + its text as Markdown blocks (`md`), and for an
  191. // edit the previous summary's blocks (`oldSummaryMd`) and the new one's (`summaryMd`, the ticket
  192. // page takes it over live). Only pages and pushes carry these — the JSON API does not.
  193. static pageEventOf = (ev) => {
  194. out = {}
  195. for (k of ev.keys()) { out[k] = ev[k] }
  196. out.md = markdownBlocks(ev.text)
  197. // mission 016: who likes a comment (users @ids) — the page works out `liked` for the user looking
  198. if (ev.isComment == true || (ev.isState == true && ev.hasText == true)) { out.likedBy = likersOf(ev.id) }
  199. if (ev.isEdit == true) {
  200. out.oldSummaryMd = markdownBlocks(ev.oldSummary)
  201. out.summaryMd = markdownBlocks(ev.newSummary)
  202. }
  203. return out
  204. }
  205. static pageEventsOf = (evs) => {
  206. out = []
  207. for (ev of evs) { out.push(pageEventOf(ev)) }
  208. return out
  209. }
  210. // ---- the one-time purge of #94, identity rule (lib/migrate.hl purgeTicket94): the writes of the events table -------------
  211. // the @ids of every event of a ticket
  212. static eventIdsOf = (ticketId) => {
  213. out = []
  214. found = eventsTable.find('ticket', ticketId)
  215. if (countOf(found) > 0) { for (e of found) { out.push(e.id) } }
  216. return out
  217. }
  218. // the link events of OTHER tickets that name `ticketId` as the other side or as the previous one: [{ id, owner (that ticket's @id) }]
  219. static linkEventsNaming = (ticketId) => {
  220. out = []
  221. all = eventsTable.find(null, null)
  222. if (countOf(all) == 0) { return out }
  223. for (e of all) {
  224. if (e.kind == 'link' && e.ticket != ticketId && (e.other == ticketId || e.previous == ticketId)) { out.push({ id = e.id owner = e.ticket }) }
  225. }
  226. return out
  227. }
  228. static removeEvent = (id) => { eventsTable.delete(id) }
  229. static compactEvents = () => { eventsTable.compactNow() }
  230. // A project keeps its own move journal even after the ticket leaves it.
  231. static projectMoves = (String projectId) => {
  232. out = []
  233. for (e of eventsTable.find(null, null)) {
  234. if (e.kind == 'move' && e.projectId == projectId) {
  235. out.push({ id = e.id text = e.text href = e.href when = localStamp(e.created) })
  236. }
  237. }
  238. return out
  239. }

Branches

Latest commits

  • 772e0696|||PR Allow project admins to move tickets between their projectsmre
  • be92a560Merge branch 'records' into mainTickets
  • 0daf2ea8|||PR records: PR-only workflow, #50 donemre
  • 0185d0b6Log PR 1 deploymre
  • f5bf16e2Mission 031: deploy PR 1mre
  • c69cd5f3Merge branch 'pr-flow' into mainTickets
  • cc56798cLog gitoria PR-only requestmre
  • d876d0b2|||PR README: changes land as PRsmre
  • fd83ac1fconductor: caramboleyo is the user, not owner/creatorApollo
  • 3b858c91Log backup hard-link fixmre
  • 7f6b37a0deploy.sh backup: tar --hard-dereference (hard links broke mpackdb sidecars on restore)mre
  • e93711d9Log E30 livemre
  • 59c02e4dMerge e30: preserve edited state note labels in Markdownmre
  • 30970796Log E30 deploy GOmre
  • 7c8e6d83Limit edited state note fix to Markdown renderingmre
  • 910536d0Log E30 reviewmre
  • 36984386Preserve state transitions when their notes are editedmre
  • d75cb946Log E30 startmre
  • 0bf3fefaMission 030: edited state note labelmre
  • 691565c6Log neutral-ticket rule checkmre