gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain5d144817Merge branch 'records-t40' into mainTicketsmain/lib/subscriptions.hl

5.2 KB

  1. // Ticket #51: durable subscription cursors over the existing stream, no second journal.
  2. import { MPackDB } from 'hl:mpackdb'
  3. import { now } from 'hl:time'
  4. import { storageDir, countOf, plainError } from './util.hl'
  5. import { projectRecord, isAdminOf } from './projects.hl'
  6. import { userRecord } from './users.hl'
  7. import { streamTable } from './stream.hl'
  8. import { pushUrlError, sealPushSecret } from './subscription-push.hl'
  9. import { deltaTickets, deltaMarkdown } from './subscriptions-format.hl'
  10. static subscriptionsTable = new MPackDB(file = storageDir + '/subscriptions.db', primaryKey = '@id', indexes = ['owner'])
  11. static subscriptionRecords = () => {
  12. out = []
  13. rows = subscriptionsTable.find(null, null)
  14. if (countOf(rows) > 0) { for (s of rows) { out.push(s) } }
  15. return out
  16. }
  17. static administers = (s, user) => {
  18. if (s.projects == 'all') { return false }
  19. for (id of s.projects) { if (!isAdminOf(id, user)) { return false } }
  20. return true
  21. }
  22. static canManage = (s, user) => { return s.owner == user.id || administers(s, user) }
  23. // Explicit public shape: no secret (even encrypted), signing body or internal fields can escape.
  24. static subscriptionView = (s) => {
  25. 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 }
  26. }
  27. static subscriptionsFor = (user) => {
  28. out = []
  29. for (s of subscriptionRecords()) { if (canManage(s, user)) { out.push(subscriptionView(s)) } }
  30. return { subscriptions = out }
  31. }
  32. static createSubscription = (user, body) => {
  33. if (body == null || hlTypeName(body) != 'Hybrid' || body.length != null) { return { error = 'body must be an object' } }
  34. for (k of body.keys()) { if (!['owner', 'projects', 'label', 'pushUrl', 'secret'].includes(k)) { return { error = 'unknown field: ' + k } } }
  35. owner = body.owner == null ? user.id : body.owner
  36. if (hlTypeName(owner) != 'String' || userRecord(owner) == null) { return { error = 'owner must be a tickets user id' } }
  37. projects = body.projects
  38. if (projects != 'all') {
  39. 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)' } }
  40. for (id of projects) { if (hlTypeName(id) != 'String' || projectRecord(id) == null) { return { error = 'unknown project id' } } }
  41. }
  42. label = body.label == null ? '' : body.label
  43. bad = plainError(label, 100, 'label')
  44. if (bad != null) { return { error = bad } }
  45. scope = { owner = owner projects = projects }
  46. 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 } }
  47. url = body.pushUrl == null ? '' : body.pushUrl
  48. urlError = pushUrlError(url)
  49. if (urlError != null) { return { error = urlError field = 'pushUrl' } }
  50. let sealed = null
  51. if (url != '') {
  52. if (hlTypeName(body.secret) != 'String' || body.secret == '' || body.secret.length > 1024) { return { error = 'secret must be nonempty text (at most 1024 characters)' field = 'secret' } }
  53. sealed = sealPushSecret(body.secret, url)
  54. if (sealed == null) { return { error = 'push secret encryption key is not configured' } }
  55. } else if (body.secret != null) { return { error = 'secret requires pushUrl' field = 'secret' } }
  56. id = subscriptionsTable.put({ secret = sealed pushUrl = url owner = owner projects = projects label = label cursor = streamTable.count() wakePending = false created = now() wakeStatus = 'idle' })
  57. if (id == null) { return { error = 'could not store subscription' } }
  58. return { subscription = subscriptionView(subscriptionsTable.fetch(id)) }
  59. }
  60. static removeSubscription = (id, user) => {
  61. s = subscriptionsTable.fetch(id)
  62. if (s == null) { return { error = 'no such subscription' status = 404 } }
  63. if (!canManage(s, user)) { return { error = 'this subscription belongs to another user' forbidden = true } }
  64. subscriptionsTable.delete(id)
  65. return { removed = id }
  66. }
  67. static matchesSubscription = (s, r) => {
  68. return r != null && r.kind != 'purged' && r.actor != s.owner && (s.projects == 'all' || s.projects.includes(r.project))
  69. }
  70. static hasDelta = (s) => {
  71. last = streamTable.count()
  72. let n = s.cursor + 1
  73. while (n <= last) {
  74. if (matchesSubscription(s, streamTable.fetch(n))) { return true }
  75. n = n + 1
  76. }
  77. return false
  78. }
  79. // Snapshot, render and mark in one event-loop call. No outgoing fetch or timer wait inside this function.
  80. static subscriptionDelta = (id, user, peek, markdown) => {
  81. s = subscriptionsTable.fetch(id)
  82. if (s == null) { return { error = 'no such subscription' status = 404 } }
  83. if (s.owner != user.id) { return { error = 'only the owner can read this delta' forbidden = true } }
  84. upTo = streamTable.count()
  85. rows = []
  86. let n = s.cursor + 1
  87. while (n <= upTo) {
  88. r = streamTable.fetch(n)
  89. if (matchesSubscription(s, r)) { rows.push(r) }
  90. n = n + 1
  91. }
  92. tickets = deltaTickets(rows, user)
  93. data = { subscription = s.id from = s.cursor upTo = upTo tickets = tickets more = false }
  94. text = markdown ? deltaMarkdown(tickets) : ''
  95. if (!peek) {
  96. s.cursor = upTo
  97. s.wakePending = false
  98. s.wakeStatus = 'idle'
  99. s.delivery = ''
  100. subscriptionsTable.update(s.id, s)
  101. }
  102. return { data = data text = text }
  103. }

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