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
- 5d144817Merge branch 'records-t40' into mainTickets
- 44ee6f06|||PR records: #43 #45 livemre
- b8fdeee9Merge branch 'p39-routes' into mainTickets
- a0187a0a|||PR Remove redundant route-order loopmre
- 369e4b9cMerge branch 'k40-lists' into mainTickets
- 9af8e780|||PR Use keyed list mutations for live ticket updatesmre
- 610d8e8aMerge branch 'records-t39' into mainTickets
- 32bcdf24|||PR records: GO P39 K40mre
- 4123c6d1Merge branch 'records-t38' into mainTickets
- cd3641a1|||PR records: day72 deploy live, tickets closedmre
- 7d44d852Merge branch 'records-t37' into mainTickets
- 926f7061|||PR records: merges, D36 deploymre
- bf58534eMerge branch 'l33-final' into mainTickets
- 759a9ac3|||PR thin login (tickets#46, #45): merge main, route entry cleanupmre
- 239436d8Merge remote-tracking branch 'origin/main' into l33-finalmre
- 5822cb3aMerge branch 'o34-clean' into mainTickets
- 0870653c|||PR offline note from __hlOnline, no polling (tickets#41)mre
- 14cf96b4Merge branch 'records-t36' into mainTickets
- 07749fa1|||PR records: day72 + #40 mergedmre
- ecd7e226|||PR Consolidate login and move handler logic into topic libraries (partial)mre