gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfa4ed25bfa4ed25bMerge branch 'f11-upsert' into mainIdentfa4ed25b/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

  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre
  • 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre
  • 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
  • 991641d4Merge branch 'f10-access' into mainIdent
  • e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
  • df7696dbident#34: tests: access ticks selector quoting fixmre
  • 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
  • 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
  • dc702419Merge branch 'f9-choose' into mainIdent
  • 8caec771|||PR ident#34 restore .choose hookmre
  • 0dbff960Merge branch 'f8-shared' into mainIdent
  • d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
  • 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
  • eb039073ident#34: tests: signin and migration find the choice without the classmre
  • 4b94a594ident#34: tests: fix selector of the identity choicemre
  • bebc88e4ident#34: main.hl drops the unstyled classes inboxlink and whomre
  • b4e7ad04ident#34: the identity choice link/button has no class (rule on #chooselist a)mre
  • 3c33d6eaident#34: the connection link in inbox has no class (rule on #conns a)mre
  • dc97ec0dident#34: the Notif. chip and the Used in link share chipLook (Style.pChip) instead of the classes chip/used/notifmre