gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit5561a0cb5561a0cbreport I4-mobilemre5561a0cb/lib/delivery.hl

4.4 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. on ticker.tick(t) { runDigests(clockNow()) }

Branches

Latest commits

  • 5561a0cbreport I4-mobilemre
  • 4ca2ed20404 page in the shell (viewport, status 404, way home); phone: account actions live in the user menu so the open menu covers nothing; mobile gate; signoutall waits for a cold startmre
  • 9baad745tests: browser gate waits for the reloaded document (offline reload race under CPU throttle); HL_CPU_THROTTLE knobmre
  • b96796bdtests: wait for hydration before real clicks after a navigation (H2)mre
  • 9722964fre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • c9b196c9tests/iplimit: wait for the welcome form itself after B's first login (identities list renders before it); mission 017 reportmre
  • 72e0f587ident#26: identity page with "Used in" (app, since, last use, Notif. chip, empty state); lastLogin recorded at exchange; gates + lisbeth E2E; mission 016 reportmre
  • 94576fbcident#26 WIP: identity page /identity/:iid with "Used in" (app, since, last use, Notif. chip), lastLogin recorded at exchange, "Used in…" link on the identities list; apps gate 4b, e2e-usedin.mjsmre
  • 349cb502tests/invites: wait for hydration on the code page before entering the code (native GET submit raced the welcome form); mission 015 reportmre
  • d1bf4720tests: no hard-coded ports — every gate's ports and fixture origins come from <GATE>_PORT / _CHROME env (defaults unchanged; Bob/fixture origins follow, selector cookie name too)mre
  • 6280c92fre-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 147744bftests/migration: the fixture app origin follows MIGRATION_GATE_TESTAPP_PORT (oldstore-009 OLD_TESTAPP)mre
  • 48cd36bctests/migration: ports from env (MIGRATION_GATE_PORT, MIGRATION_GATE_TESTAPP_PORT); 8395/8396 were taken by a foreign servermre
  • 3d9b2fd8docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 19b7e3c3deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • c8a596f4re-vendor Hybriel 66c3db71 (web batch 4); e2e: main menu marked after in-place sign-in is a normal check; report 014mre
  • 8f3dc1ectests: delivery allow-list for the stopped server's network errors uses the gate port, not 8700mre
  • 9cc8eddftests: notify and iplimit gate Chrome ports from env (NOTIFY_GATE_CHROME, IPLIMIT_GATE_CHROME_A/B)mre
  • d6da2d02tests: delivery and migration gate ports from env (DELIVERY_GATE_PORT, DELIVERY_GATE_CHROME, MIGRATION_GATE_CHROME) — hard-coded 8700-8719 collided with a worker during deploymre
  • 7276a008ident mission 013: re-vendor Hybriel master f9614007 (#200, #202 mpackdb copy fix); gates and e2e rerunmre