gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain0f6d1592Merge branch 'f18b-shell-session' into mainIdentmain/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

  • 0f6d1592Merge branch 'f18b-shell-session' into mainIdent
  • 19868218|||PR ident#39 shell reads its session member instead of a cross-file emitmre
  • 5d1b3fedMerge branch 'v61-day61' into mainIdent
  • 83faeb15|||PR ident: vendor Hybriel day61mre
  • b2572b34Merge branch 'v59-day59' into mainIdent
  • 67cf016d|||PR ident: vendor Hybriel day59mre
  • 600c043dMerge branch 'v58-day58' into mainIdent
  • 365f8c80|||PR ident: vendor Hybriel day58mre
  • 585126e5Merge branch 'f16-setf' into mainIdent
  • 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
  • a1419d9cMerge branch 'f15-navigate' into mainIdent
  • 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
  • e36f4516Merge branch 'f14-online' into mainIdent
  • 2c5eac91|||PR ident#38 online state without probe timermre
  • bf1980f2Merge branch 'v56-day56' into mainIdent
  • 62ae0adf|||PR ident: vendor Hybriel day56mre
  • 0d18abc4Merge branch 'f13-avatar' into mainIdent
  • 2a99730a|||PR ident#37 avatar in hlmre
  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre