gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitec6aad35ec6aad35Merge branch 'a28g-amp-callsites' into d45-docsmreec6aad35/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

  • ec6aad35Merge branch 'a28g-amp-callsites' into d45-docsmre
  • 6bfbd3f2ident#45: docs/IDENT.md, what ident is and how it works; CONCEPT.md marks one id per app as droppedmre
  • c0edca26ident#28 A28G: write & at every call that hands a session to an &-parameter; gate tests/sessionwrites.mjs pins that sign-in, pending address, embed sign-in and selector land in the session filesmre
  • 80965c08ident#28: README says the deploy goes through the standbymre
  • 2f41670cident#28: ident-standby service on port 45012 (no background round in the standby) and deploy.sh hands over through standby-swap.shmre
  • a544b261ident#28: README names the new vendored Hybrielmre
  • 716860b0ident#28: gates follow Hybriel ac1ed9da (lazy sessions, meta attribute order, JSON key order, default X-Frame-Options switched off for the embed page)mre
  • 889e00fbident#28: Hybriel master ac1ed9da (sessions reload a newer file, a replayed reconnect carries the server session); a startup fault exits 1 (on Error)mre
  • f5a55f8aident#11: apps can ask for nickname, firstname and lastname (fields list on /api/exchange, new POST /api/profile); empty fields come back null; gate checks, READMEmre
  • d8be66ebident: report and log line for the widget cache and frame height changemre
  • 2068f61aident#31: browsers revalidate the widget scripts on every load; the sign-in frame is exactly as tall as the current step (the frame page reports its height, selector.js accepts it from ident only)mre
  • a22d2daaident#31: the sign-in frame is no taller than the tallest step needs (plain CSS height per width), gate check for itmre
  • a77d1b22ident#31: the welcome form keeps editId a plain value (browser gate), README, wider rows in the framemre
  • 5c481bfdident#31: sign in to ident inside the selector's panel (email, code, identity in an iframe; a new address goes to ident in the same tab and comes back)mre
  • 061abfe2ident: the apps gate waits until the test app's login button stops moving before it clicks (the page grows after load and the click missed about every second run)mre
  • 45f7e4daident: the user menu's Sign out waits for the server before it reloads (the apps gate failed because the session could still be signed in)mre
  • f07658c7ident: home page shows the account once (account bar removed, the user menu carries email, Sign out, Sign out everywhere); wildcard origins https://*.domain for apps on many subdomains; gates follow (WIP: gates not re-run after the last edits)mre
  • f2c43bbbtheme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
  • 6d4f996esessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • 029eb5e5deploy: security gate (tests/security.mjs)mre