tickets
All repositories: gitoria
11.0 KB
// lib/events.hl — THE HISTORY OF A TICKET: an append-only event per change. Statics only, the server realm.// eventsTable pk @id index @ticket APPEND ONLY: { ticket (ticket @id), seq// (1, 2, … per ticket), kind, author, user?, text, from, to, created, oldId? }// kind = 'created' | 'comment' | 'state' | 'edit' | 'link' | 'assign'// 'assign' (ticket #20): { assignee (users @id, '' = unassigned), previous (users @id, '') }// 'link' (ticket #4, mission 017): { linkKind ('parent' | 'blockedBy'), action// ('set' | 'removed'), other, otherProject, otherNumber, previous?, previousProject?,// previousNumber? } — on the CHILD / the BLOCKED ticket (the one whose relation changed).// 'edit' (ticket #8, mission 014): { oldSubject, oldSummary, subject, summary } —// the values before AND after; the ticket record then carries the new ones.// `user` (users @id, users.hl) since ticket #7: the event's author is that// user's CURRENT display name; `author` keeps the name at writing time.// 'comment' (mission 016): the event keeps the FIRST text; edits, a deletion and likes are kept by// lib/comments.hl, and the rows here show the comment as it is now (commentStateOf). Its files// (tickets#27): lib/attachments.hl (the row's `attachments`).// tickets.hl writes the events (putEvent) together with the ticket record; this file shapes and reads them.import { MPackDB } from 'hl:mpackdb'import { storageDir, countOf, merged, localStamp, ticketHref } from './util.hl'import { nameOfUser } from './users.hl'import { projectSlugOf } from './projects.hl'import { slugOf } from './tickets-helpers.hl'import { markdownBlocks } from './markdown.hl'import { commentStateOf, likersOf } from './comments.hl'import { attachmentsOf } from './attachments.hl'static eventsTable = new MPackDB(file = storageDir + '/events.db', primaryKey = '@id', indexes = ['@ticket'])// ---- writing -------------------------------------------------------------------------------------// appends one event record (built field by field by tickets.hl); answers its id, or null (eventsError says why)static putEvent = (rec) => { return eventsTable.put(rec) }static eventsError = () => { return eventsTable.lastError() }// one stored event record, or null (the event stream numbers it, lib/stream.hl)static eventRecord = (eventId) => { return eventId == null ? null : eventsTable.fetch(eventId) }// the API row of a stored eventstatic eventRowAt = (eventId, t) => { return eventRowOf(eventsTable.fetch(eventId), t) }// ---- the reads and writes of the #20 migration (lib/migrate.hl calls these; the events table is written only here) ----// every event record, in the table's orderstatic eventRecords = () => { return eventsTable.find(null, null) }// the event records of one ticket (ticket @id)static eventRecordsOf = (ticketId) => { return eventsTable.find('ticket', ticketId) }// a state event gets the new state names (the one exception to "append only": old names renamed in place)static renameStates = (e, nf, nt) => {eventsTable.update(e.id, merged(e, { from = nf to = nt }))}// ---- reading -------------------------------------------------------------------------------------// the history of a ticket (a record), oldest first: by stored time, then seqstatic historyOf = (t) => {out = []all = eventsTable.find('ticket', t.id)if (countOf(all) == 0) { return out }for (e of all) { if (e.sourceOnly != true) { out.push(eventRowOf(e, t)) } }let i = 1while (i < out.length) {cur = out[i]let j = i - 1while (j >= 0 && (out[j].createdMs > cur.createdMs || (out[j].createdMs == cur.createdMs && out[j].seq > cur.seq))) {out[j + 1] = out[j]j = j - 1}out[j + 1] = curi = i + 1}return out}// a ticket from before the login has no createdBy: the free-text author of its 'created' eventstatic creatorNameOf = (t) => {all = eventsTable.find('ticket', t.id)if (countOf(all) == 0) { return '' }for (e of all) { if (e.kind == 'created') { return e.author != null ? e.author : '' } }return ''}// ---- the shapes ----------------------------------------------------------------------------------static eventRowOf = (e, t) => {let kind = e.kind// computed BEFORE the literal: inside a literal that has an `id` entry, a called function's// parameter named `id` read that entry instead (mission 011, reported as a Hybriel issue)who = e.user != null ? nameOfUser(e.user) : e.authorlet label = kind == 'created' ? 'opened the ticket' : (kind == 'state' ? 'changed the state' : (kind == 'edit' ? 'edited the ticket' : 'commented'))if (kind == 'move') { label = e.text }if (kind == 'link') { label = linkLabelOf(e) }let assignedTo = ''let assignedWas = ''if (kind == 'assign') {assignedTo = e.assignee != null && e.assignee != '' ? nameOfUser(e.assignee) : ''assignedWas = e.previous != null && e.previous != '' ? nameOfUser(e.previous) : ''label = assignedTo != '' ? 'assigned the ticket to ' + assignedTo : 'unassigned the ticket' + (assignedWas != '' ? ' (was ' + assignedWas + ')' : '')}row = {id = e.idticket = e.ticketproject = t != null ? projectSlugOf(t) : ''number = t != null ? t.number : 0seq = e.seq != null ? e.seq : 0kind = kindauthor = whouserId = e.user != null ? e.user : ''text = e.text != null ? e.text : ''hasText = e.text != null && e.text != ''from = e.from != null ? e.from : ''to = e.to != null ? e.to : ''toSlug = slugOf(e.to)isState = kind == 'state'isComment = kind == 'comment'isCreated = kind == 'created'label = labelwhen = localStamp(e.created)createdMs = e.created}// ticket #8: only an edit event carries these (the JSON of every other event is unchanged)if (kind == 'edit') {row.isEdit = truerow.oldSubject = e.oldSubject != null ? e.oldSubject : ''row.oldSummary = e.oldSummary != null ? e.oldSummary : ''row.newSubject = e.subject != null ? e.subject : ''row.newSummary = e.summary != null ? e.summary : ''row.subjectChanged = row.oldSubject != row.newSubjectrow.summaryChanged = row.oldSummary != row.newSummary}// ticket #4: only a link event carries theseif (kind == 'link') {row.isLink = truerow.linkKind = e.linkKindrow.linkAction = e.actionrow.otherRef = e.otherProject + ' #' + e.otherNumberrow.otherHref = ticketHref(e.otherProject, e.otherNumber)row.previousRef = e.previous != null ? e.previousProject + ' #' + e.previousNumber : ''}// mission 016: a comment as it is NOW (lib/comments.hl). Only a changed comment carries the extra keys: an edited one// `edited`, `editedWhen`, `earlier` ([{ when, text }], the versions before, oldest first) and the newest `text`; a// deleted one `deleted`, `deletedWhen`, no text, the label "commented (deleted …)"; a liked one `likes` (the count).// Every other comment row has exactly the keys it had before.// 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// only loses its text (the state line stays as it was)if (kind == 'state' && e.text != null && e.text != '') {ns = commentStateOf(e)row.text = ns.textrow.hasText = ns.text != ''if (ns.edited) {row.edited = truerow.editedWhen = localStamp(ns.editedMs)row.earlier = versionsOf(ns.earlier)}if (ns.likes > 0) { row.likes = ns.likes }}if (kind == 'comment') {cs = commentStateOf(e)if (cs.edited) {row.text = cs.textrow.hasText = cs.text != ''row.edited = truerow.editedWhen = localStamp(cs.editedMs)row.earlier = versionsOf(cs.earlier)}if (cs.deleted) {row.text = ''row.hasText = falserow.label = 'commented (deleted ' + localStamp(cs.deletedMs) + ')'row.deleted = truerow.deletedWhen = localStamp(cs.deletedMs)}if (cs.likes > 0) { row.likes = cs.likes }// tickets#27: only a comment with files carries `attachments` ([{ id, name, type, size, image, url }]; a deleted one has none)if (!cs.deleted) {files = attachmentsOf(e.id)if (files.length > 0) { row.attachments = files }}}// ticket #20: only an assign event carries theseif (kind == 'assign') {row.isAssign = truerow.assignee = assignedTorow.previousAssignee = assignedWas}return row}static versionsOf = (list) => {out = []for (v of list) { out.push({ when = v.when text = v.text }) }return out}// "set the parent to ident.worldapi.org #1", "removed the blocker tickets.worldapi.org #6", …static linkLabelOf = (e) => {other = e.otherProject + ' #' + e.otherNumberif (e.linkKind == 'parent') {if (e.action == 'removed') { return 'removed the parent ' + other }was = e.previous != null ? ' (was ' + e.previousProject + ' #' + e.previousNumber + ')' : ''return 'set the parent to ' + other + was}if (e.action == 'removed') { return 'removed the blocker ' + other }return 'marked it blocked by ' + other}// THE PAGE'S EVENT ROW (ticket #6): the API row + its text as Markdown blocks (`md`), and for an// edit the previous summary's blocks (`oldSummaryMd`) and the new one's (`summaryMd`, the ticket// page takes it over live). Only pages and pushes carry these — the JSON API does not.static pageEventOf = (ev) => {out = {}for (k of ev.keys()) { out[k] = ev[k] }out.md = markdownBlocks(ev.text)// mission 016: who likes a comment (users @ids) — the page works out `liked` for the user lookingif (ev.isComment == true || (ev.isState == true && ev.hasText == true)) { out.likedBy = likersOf(ev.id) }if (ev.isEdit == true) {out.oldSummaryMd = markdownBlocks(ev.oldSummary)out.summaryMd = markdownBlocks(ev.newSummary)}return out}static pageEventsOf = (evs) => {out = []for (ev of evs) { out.push(pageEventOf(ev)) }return out}// ---- the one-time purge of #94, identity rule (lib/migrate.hl purgeTicket94): the writes of the events table -------------// the @ids of every event of a ticketstatic eventIdsOf = (ticketId) => {out = []found = eventsTable.find('ticket', ticketId)if (countOf(found) > 0) { for (e of found) { out.push(e.id) } }return out}// the link events of OTHER tickets that name `ticketId` as the other side or as the previous one: [{ id, owner (that ticket's @id) }]static linkEventsNaming = (ticketId) => {out = []all = eventsTable.find(null, null)if (countOf(all) == 0) { return out }for (e of all) {if (e.kind == 'link' && e.ticket != ticketId && (e.other == ticketId || e.previous == ticketId)) { out.push({ id = e.id owner = e.ticket }) }}return out}static removeEvent = (id) => { eventsTable.delete(id) }static compactEvents = () => { eventsTable.compactNow() }// A project keeps its own move journal even after the ticket leaves it.static projectMoves = (String projectId) => {out = []for (e of eventsTable.find(null, null)) {if (e.kind == 'move' && e.projectId == projectId) {out.push({ id = e.id text = e.text href = e.href when = localStamp(e.created) })}}return out}
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