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
- 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
- 716e14ffident mission 013: browser gate — Bob signs out everywhere (no second code request)mre
- 5885f1c9ident mission 013 step 2: stopgap reload removed (in-place sign-in/out reach the shell via emit, notices back, route-marked main menu), gates updatedmre
- 734ac0a9ident mission 013 step 1: re-vendor Hybriel ce6f2e0a (plugins whole; bin/ is git-ignored)mre
- b37875ccident mission 012 part 3: real-Chrome fixes (brand 44 px, identity form width), e2e walk, report, logmre
- f989c2ebident mission 012: stopgap reload after in-place sign-in/sign-out (until hybriel#193), gates wait for cold start, welcome end reloadsmre
- 443291d4ident mission 012: /start on its own shell (no account), browser gate 152/0, reportmre
- 021dd0fcident mission 012: browser gate waits cold start (IDENT_COLD_MS), header selectors, e2e checks; shell events from a page do not reach it (probe), current not marked, account-bar link 44 pxmre
- 97f77b07ident mission 012 (WIP): onto the layouts theme dbe3de7, shell on AppHeader/MainMenu/UserMenu/Sidebar + ContentFirst, styles cut, check-theme gatemre
- 8e4f435bpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre
- fa288f63ident mission 011: report wording (Intl zone names)mre
- 961fae01ident mission 011: report, LOG, STATUSmre