gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc28bddeec28bddeeMerge branch 'f2-shell' into mainIdentc28bddee/lib/delivery.hl

4.7 KB

  1. // lib/delivery.hl — DELIVERY (piece 5, ticket ident#7; CONCEPT.md "Notifications"): THE DAILY MAIL at 17:00 in the
  2. // user's time zone with every pending default-channel notification, and URGENT ones at once. The zone rules are
  3. // Hybriel's (hl:time `local`, hybriel#5) — ident ships none. Mail goes through lib/mail.hl (IDENT_MAIL_SINK in tests).
  4. // Statics + one clock: project.hl constructs this file once (`delivery = new Delivery()`), as lib/mail.hl.
  5. //
  6. // digestsTable pk @id index !account { account, lastDate ('YYYY-MM-DD', the user's LOCAL date of the last
  7. // daily round), sentAt, count (notifications in that mail, 0 = none) }
  8. //
  9. // THE DAILY ROUND (every IDENT_DIGEST_EVERY seconds, default 60): for every account whose local time is 17:00 or
  10. // later and whose lastDate is not today's local date: today's date is stored FIRST (so a crash or restart cannot
  11. // mail twice), then ONE mail lists the pending ones (lib/notify.hl pendingDaily: mail = 'daily', not mailed, email
  12. // still on) and they are marked mailed. Nothing pending = no mail, the date is still stored: what arrives after
  13. // 17:00 goes into tomorrow's mail. A server that was down at 17:00 sends at its first round after it (same day).
  14. // A DST day needs nothing of ours: `local()` answers the wall clock of that instant.
  15. // THE ZONE: the account's (lib/accounts.hl zoneOf: none or unknown = UTC).
  16. // URGENT (lib/api.hl apiNotify → deliverUrgent): the channels were fixed when it arrived (lib/notify.hl
  17. // sendNotification, the user's switches); mail = 'now' → mailed at once. PUSH (ident#8) hooks in at `pushHooks`.
  18. import { MPackDB } from 'hl:mpackdb'
  19. import { local, every } from 'hl:time'
  20. import { dir, envNumber, first, merged } from './util.hl'
  21. import { allAccounts, accountById } from './accounts.hl'
  22. import { pendingDaily, deliveryOf, markMailed } from './notify.hl'
  23. import { sendNotificationMail } from './mail.hl'
  24. import { clockNow, dateOf, digestSubject, digestText, urgentSubject, urgentText } from './delivery-helpers.hl'
  25. static digestsTable = new MPackDB(file = dir + '/digests.db', primaryKey = '@id', indexes = ['!account'])
  26. static digestHour = 17
  27. static maxPerMail = 200 // more stay pending for the next day's mail
  28. // ---- THE DAILY MAIL -------------------------------------------------------------------------
  29. static digestOf = (accountId) => { return first(digestsTable.find('account', accountId)) }
  30. static rememberRound = (accountId, date, at, count) => {
  31. d = digestOf(accountId)
  32. rec = { account = accountId lastDate = date sentAt = at count = count }
  33. if (d == null) { digestsTable.put(rec) } else { digestsTable.update(d.id, merged(d, rec)) }
  34. return null
  35. }
  36. // one account's round at instant `at`: answers the number of notifications mailed (0 = no mail)
  37. static digestFor = (a, at) => {
  38. l = local(at, a.timeZone)
  39. if (l.hour < digestHour) { return 0 }
  40. date = dateOf(l)
  41. d = digestOf(a.id)
  42. if (d != null && d.lastDate == date) { return 0 }
  43. rows = pendingDaily(a.id, maxPerMail)
  44. rememberRound(a.id, date, at, rows.length)
  45. if (rows.length == 0) { return 0 }
  46. sendNotificationMail(a.email, digestSubject(rows, date), digestText(rows, date, a.timeZone))
  47. ids = []
  48. for (r of rows) { ids.push(r.id) }
  49. markMailed(ids, at)
  50. return rows.length
  51. }
  52. static runDigests = (at) => {
  53. let mails = 0
  54. for (a of allAccounts()) {
  55. if (digestFor(a, at) > 0) { mails = mails + 1 }
  56. }
  57. if (mails > 0) { console.log('delivery: ' + mails + ' daily mails') }
  58. return mails
  59. }
  60. // ---- URGENT, AT ONCE --------------------------------------------------------------------------
  61. // PUSH (ident#8): set pushHooks.push = (accountId, row) => … once push devices exist; lib/notify.hl hasPushDevice
  62. // then decides at arrival that an urgent one is pushed (push = true, mail = 'none') instead of mailed.
  63. static pushHooks = { push = null }
  64. static deliverUrgent = (id) => {
  65. n = deliveryOf(id)
  66. if (n == null || !n.row.urgent) { return null }
  67. if (n.push && !n.pushSent && pushHooks.push != null) { pushHooks.push(n.account, n.row) }
  68. if (n.mail != 'now' || n.mailSent) { return null }
  69. a = accountById(n.account)
  70. if (a == null) { return null }
  71. sendNotificationMail(a.email, urgentSubject(n.row), urgentText(n.row))
  72. markMailed([id], clockNow())
  73. return null
  74. }
  75. // ---- THE CLOCK: one round every IDENT_DIGEST_EVERY seconds (the gates: 0.2) --------------------
  76. static ticker = every(envNumber('IDENT_DIGEST_EVERY', 60))
  77. // THE STANDBY (deploys without a gap, ident#28): the second process on port 45012 runs with IDENT_STANDBY=1 and does not run the round, so the
  78. // daily mail can never be sent twice while both processes are up. Mail caused by a request (a sign-in code, an urgent one) is not a background job.
  79. static standby = envNumber('IDENT_STANDBY', 0) == 1
  80. on ticker.tick(t) { if (!standby) { runDigests(clockNow()) } }

Branches

Latest commits

  • c28bddeeMerge branch 'f2-shell' into mainIdent
  • c3a40ff6|||PR ident#40 one shell for the app and /start (account part off on /start, #40.1); startshell.hl removed; unused signOut faces gone (#40.4)mre
  • 060f3870ident#40.4: use the shell sign-out faces in regression gatesmre
  • 9b94aa6aMerge branch 'f1b-cleanup' into mainIdent
  • edcfce3f|||PR ident#41 ident#43 ident#44 login error page as a component (same status codes and text, pixel-identical), declarations and route wiring cleaned upmre
  • 3ac85995ident#43 ident#44: plain declarations for never-reassigned lets, one envString helper, named route functions and audience rule out of project.hlmre
  • c7eef14aMerge branch 'nv1-navigate' into mainIdent
  • 644f9775|||PR ident#36 navigate() instead of window.location in the shell, start shell and homemre
  • 4ca2f921deploy.sh: tar --hard-dereference so backups hold no hard linksmre
  • 23971fb7ident#49: media queries in the language's @media formmre
  • 73f4f97dident: re-vendor to hybriel day38b (mpackdb store-open fix)mre
  • aa056c03ident: a startup error prints its message before exit 1 (hybriel#247)mre
  • ee37a861ident#G48: gitoria's registration gets the wildcard origin of its repo addresses at load (gate tests/gitoria-origin.mjs)mre
  • dc6d486fident#27: the docs gate expects the new Access entry in the main menumre
  • d277633cident#27: README: identity access and the introspect endpointmre
  • e999b0d3ident#27: the Access tab — one key per identity for all its apps: give access in three steps, the overview matrix, a cell's sheet (since, last use, level, rotate, revoke), the app's check POST /api/introspect, agent identities hidden from the login picker, an inbox icon in the header's icon style (gate: tests/access.mjs)mre
  • 436206fcident#47: deploy.sh runs the sign-in gate on the deploy's own port slot, like the sessionwrites and docs gates (the default 8700 can be taken by another process)mre
  • a4887ea3ident#47: the selector's sign-in frame no longer has a Continue button; the identity row itself is the link (gate tests/signin.mjs fails on the old code)mre
  • d8ec703dident#45: /docs shows docs/IDENT.md (hl:markdown, vendored from ac1ed9da), one Docs entry in the main menu, README for developers only, gates tests/docs.mjs and tests/sessionwrites.mjs in deploy.shmre
  • ec6aad35Merge branch 'a28g-amp-callsites' into d45-docsmre