ident
All repositories: gitoria
4.4 KB
// lib/delivery.hl — DELIVERY (piece 5, ticket ident#7; CONCEPT.md "Notifications"): THE DAILY MAIL at 17:00 in the// user's time zone with every pending default-channel notification, and URGENT ones at once. The zone rules are// Hybriel's (hl:time `local`, hybriel#5) — ident ships none. Mail goes through lib/mail.hl (IDENT_MAIL_SINK in tests).// Statics + one clock: project.hl constructs this file once (`delivery = new Delivery()`), as lib/mail.hl.//// digestsTable pk @id index !account { account, lastDate ('YYYY-MM-DD', the user's LOCAL date of the last// daily round), sentAt, count (notifications in that mail, 0 = none) }//// THE DAILY ROUND (every IDENT_DIGEST_EVERY seconds, default 60): for every account whose local time is 17:00 or// later and whose lastDate is not today's local date: today's date is stored FIRST (so a crash or restart cannot// mail twice), then ONE mail lists the pending ones (lib/notify.hl pendingDaily: mail = 'daily', not mailed, email// still on) and they are marked mailed. Nothing pending = no mail, the date is still stored: what arrives after// 17:00 goes into tomorrow's mail. A server that was down at 17:00 sends at its first round after it (same day).// A DST day needs nothing of ours: `local()` answers the wall clock of that instant.// THE ZONE: the account's (lib/accounts.hl zoneOf: none or unknown = UTC).// URGENT (lib/api.hl apiNotify → deliverUrgent): the channels were fixed when it arrived (lib/notify.hl// sendNotification, the user's switches); mail = 'now' → mailed at once. PUSH (ident#8) hooks in at `pushHooks`.import { MPackDB } from 'hl:mpackdb'import { local, every } from 'hl:time'import { dir, envNumber, first, merged } from './util.hl'import { allAccounts, accountById } from './accounts.hl'import { pendingDaily, deliveryOf, markMailed } from './notify.hl'import { sendNotificationMail } from './mail.hl'import { clockNow, dateOf, digestSubject, digestText, urgentSubject, urgentText } from './delivery-helpers.hl'static digestsTable = new MPackDB(file = dir + '/digests.db', primaryKey = '@id', indexes = ['!account'])static digestHour = 17static maxPerMail = 200 // more stay pending for the next day's mail// ---- THE DAILY MAIL -------------------------------------------------------------------------static digestOf = (accountId) => { return first(digestsTable.find('account', accountId)) }static rememberRound = (accountId, date, at, count) => {d = digestOf(accountId)rec = { account = accountId lastDate = date sentAt = at count = count }if (d == null) { digestsTable.put(rec) } else { digestsTable.update(d.id, merged(d, rec)) }return null}// one account's round at instant `at`: answers the number of notifications mailed (0 = no mail)static digestFor = (a, at) => {l = local(at, a.timeZone)if (l.hour < digestHour) { return 0 }date = dateOf(l)d = digestOf(a.id)if (d != null && d.lastDate == date) { return 0 }rows = pendingDaily(a.id, maxPerMail)rememberRound(a.id, date, at, rows.length)if (rows.length == 0) { return 0 }sendNotificationMail(a.email, digestSubject(rows, date), digestText(rows, date, a.timeZone))ids = []for (r of rows) { ids.push(r.id) }markMailed(ids, at)return rows.length}static runDigests = (at) => {let mails = 0for (a of allAccounts()) {if (digestFor(a, at) > 0) { mails = mails + 1 }}if (mails > 0) { console.log('delivery: ' + mails + ' daily mails') }return mails}// ---- URGENT, AT ONCE --------------------------------------------------------------------------// PUSH (ident#8): set pushHooks.push = (accountId, row) => … once push devices exist; lib/notify.hl hasPushDevice// then decides at arrival that an urgent one is pushed (push = true, mail = 'none') instead of mailed.static pushHooks = { push = null }static deliverUrgent = (id) => {n = deliveryOf(id)if (n == null || !n.row.urgent) { return null }if (n.push && !n.pushSent && pushHooks.push != null) { pushHooks.push(n.account, n.row) }if (n.mail != 'now' || n.mailSent) { return null }a = accountById(n.account)if (a == null) { return null }sendNotificationMail(a.email, urgentSubject(n.row), urgentText(n.row))markMailed([id], clockNow())return null}// ---- THE CLOCK: one round every IDENT_DIGEST_EVERY seconds (the gates: 0.2) --------------------static ticker = every(envNumber('IDENT_DIGEST_EVERY', 60))on ticker.tick(t) { runDigests(clockNow()) }
Branches
Latest commits
- 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
- e13e30c3ident mission 013: report, mission, repro, LOGmre
- eff734f3ident mission 013: in-place sign-out also closes the code step (no leftover code form); checksmre
- b7a61546ident mission 013: e2e — in-place main-menu mark kept as a known gapmre
- 59b43d81ident mission 013: sign out everywhere keeps this browser's own (signed-out) session so the page goes on in placemre