gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfd23726afd23726aAdd subscription delta API and signed wake bridgemrefd23726a/lib/subscriptions.hl

4.1 KB

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

Branches

Latest commits

  • fd23726aAdd subscription delta API and signed wake bridgemre
  • c5d28fb5Tickets conductor: S51 startedmre
  • fd6ca1e6Tickets conductor: brief 027 waker design, push-test projectmre
  • e448e656Tickets conductor: STATUS #51mre
  • 1c10857bTickets conductor: P51 eval, brief 027mre
  • e725969dTickets conductor: #51 directionmre
  • 1b5c63ddTickets conductor: P51 startedmre
  • 9f13fbdaTickets conductor: brief 026 prioritymre
  • acc477d9Tickets conductor: brief 026 + Vinzenz recommendationsmre
  • 9233a734Tickets conductor: brief 026 completemre
  • 5da589fdTickets conductor: brief 026 + Gitoria's answermre
  • a3cd5329Tickets conductor: tickets#51 opened, brief 026 draftmre
  • 6f6e5797Tickets conductor: STATUS wordingmre
  • 957de51fTickets conductor: T36 live, #36 review, #50 donemre
  • bc2fde32Merge branch 'main' into t36-stylemre
  • b7ca093atests: wait for the new row and normalize editor marker spacesmre
  • 41da3525Tickets conductor: flake cause, T36 deploy stepmre
  • 64c477e6Tickets conductor: deploy waits for Arachnemre
  • 467d7286Tickets conductor: T36 done, deploy questionmre
  • fed5ca81ticket_list: remove repeated selected-state coloursmre