tickets
All repositories: gitoria
5.2 KB
// Ticket #51: durable subscription cursors over the existing stream, no second journal.import { MPackDB } from 'hl:mpackdb'import { now } from 'hl:time'import { storageDir, countOf, plainError } from './util.hl'import { projectRecord, isAdminOf } from './projects.hl'import { userRecord } from './users.hl'import { streamTable } from './stream.hl'import { pushUrlError, sealPushSecret } from './subscription-push.hl'import { deltaTickets, deltaMarkdown } from './subscriptions-format.hl'static subscriptionsTable = new MPackDB(file = storageDir + '/subscriptions.db', primaryKey = '@id', indexes = ['owner'])static subscriptionRecords = () => {out = []rows = subscriptionsTable.find(null, null)if (countOf(rows) > 0) { for (s of rows) { out.push(s) } }return out}static administers = (s, user) => {if (s.projects == 'all') { return false }for (id of s.projects) { if (!isAdminOf(id, user)) { return false } }return true}static canManage = (s, user) => { return s.owner == user.id || administers(s, user) }// Explicit public shape: no secret (even encrypted), signing body or internal fields can escape.static subscriptionView = (s) => {return { id = s.id owner = s.owner projects = s.projects label = s.label cursor = s.cursor wakePending = s.wakePending created = s.created pushUrl = s.pushUrl != null ? s.pushUrl : '' wakeStatus = s.wakeStatus wakeAttempts = s.wakeAttempts nextWake = s.nextWake }}static subscriptionsFor = (user) => {out = []for (s of subscriptionRecords()) { if (canManage(s, user)) { out.push(subscriptionView(s)) } }return { subscriptions = out }}static createSubscription = (user, body) => {if (body == null || hlTypeName(body) != 'Hybrid' || body.length != null) { return { error = 'body must be an object' } }for (k of body.keys()) { if (!['owner', 'projects', 'label', 'pushUrl', 'secret'].includes(k)) { return { error = 'unknown field: ' + k } } }owner = body.owner == null ? user.id : body.ownerif (hlTypeName(owner) != 'String' || userRecord(owner) == null) { return { error = 'owner must be a tickets user id' } }projects = body.projectsif (projects != 'all') {if (hlTypeName(projects) != 'Hybrid' || countOf(projects) == 0 || countOf(projects) > 100) { return { error = 'projects must be all or a nonempty list of project ids (at most 100)' } }for (id of projects) { if (hlTypeName(id) != 'String' || projectRecord(id) == null) { return { error = 'unknown project id' } } }}label = body.label == null ? '' : body.labelbad = plainError(label, 100, 'label')if (bad != null) { return { error = bad } }scope = { owner = owner projects = projects }if (owner != user.id && !administers(scope, user)) { return { error = 'only an admin of every listed project may subscribe another user; all is self only' forbidden = true } }url = body.pushUrl == null ? '' : body.pushUrlurlError = pushUrlError(url)if (urlError != null) { return { error = urlError field = 'pushUrl' } }let sealed = nullif (url != '') {if (hlTypeName(body.secret) != 'String' || body.secret == '' || body.secret.length > 1024) { return { error = 'secret must be nonempty text (at most 1024 characters)' field = 'secret' } }sealed = sealPushSecret(body.secret, url)if (sealed == null) { return { error = 'push secret encryption key is not configured' } }} else if (body.secret != null) { return { error = 'secret requires pushUrl' field = 'secret' } }id = subscriptionsTable.put({ secret = sealed pushUrl = url owner = owner projects = projects label = label cursor = streamTable.count() wakePending = false created = now() wakeStatus = 'idle' })if (id == null) { return { error = 'could not store subscription' } }return { subscription = subscriptionView(subscriptionsTable.fetch(id)) }}static removeSubscription = (id, user) => {s = subscriptionsTable.fetch(id)if (s == null) { return { error = 'no such subscription' status = 404 } }if (!canManage(s, user)) { return { error = 'this subscription belongs to another user' forbidden = true } }subscriptionsTable.delete(id)return { removed = id }}static matchesSubscription = (s, r) => {return r != null && r.kind != 'purged' && r.actor != s.owner && (s.projects == 'all' || s.projects.includes(r.project))}static hasDelta = (s) => {last = streamTable.count()let n = s.cursor + 1while (n <= last) {if (matchesSubscription(s, streamTable.fetch(n))) { return true }n = n + 1}return false}// Snapshot, render and mark in one event-loop call. No outgoing fetch or timer wait inside this function.static subscriptionDelta = (id, user, peek, markdown) => {s = subscriptionsTable.fetch(id)if (s == null) { return { error = 'no such subscription' status = 404 } }if (s.owner != user.id) { return { error = 'only the owner can read this delta' forbidden = true } }upTo = streamTable.count()rows = []let n = s.cursor + 1while (n <= upTo) {r = streamTable.fetch(n)if (matchesSubscription(s, r)) { rows.push(r) }n = n + 1}tickets = deltaTickets(rows, user)data = { subscription = s.id from = s.cursor upTo = upTo tickets = tickets more = false }text = markdown ? deltaMarkdown(tickets) : ''if (!peek) {s.cursor = upTos.wakePending = falses.wakeStatus = 'idle's.delivery = ''subscriptionsTable.update(s.id, s)}return { data = data text = text }}
Branches
Latest commits
- 11dc8dea|||PR Document offline shell and keyed ordering framework gapsmre
- ed091d44Merge branch 'records' into mainTickets
- 180c0cdb|||PR records: ticket move live (M32), hybriel#439mre
- 497cae7bMerge branch 'm32-move' into mainTickets
- 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