ident
All repositories: gitoria
4.7 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))// 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// 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.static standby = envNumber('IDENT_STANDBY', 0) == 1on ticker.tick(t) { if (!standby) { runDigests(clockNow()) } }
Branches
Latest commits
- a4887ea3ident#47: the selector's sign-in frame no longer has a Continue button; the identity row itself is the link (gate tests/signin.mjs fails on the old code)mre
- d8ec703dident#45: /docs shows docs/IDENT.md (hl:markdown, vendored from ac1ed9da), one Docs entry in the main menu, README for developers only, gates tests/docs.mjs and tests/sessionwrites.mjs in deploy.shmre
- ec6aad35Merge branch 'a28g-amp-callsites' into d45-docsmre
- 6bfbd3f2ident#45: docs/IDENT.md, what ident is and how it works; CONCEPT.md marks one id per app as droppedmre
- c0edca26ident#28 A28G: write & at every call that hands a session to an &-parameter; gate tests/sessionwrites.mjs pins that sign-in, pending address, embed sign-in and selector land in the session filesmre
- 80965c08ident#28: README says the deploy goes through the standbymre
- 2f41670cident#28: ident-standby service on port 45012 (no background round in the standby) and deploy.sh hands over through standby-swap.shmre
- a544b261ident#28: README names the new vendored Hybrielmre
- 716860b0ident#28: gates follow Hybriel ac1ed9da (lazy sessions, meta attribute order, JSON key order, default X-Frame-Options switched off for the embed page)mre
- 889e00fbident#28: Hybriel master ac1ed9da (sessions reload a newer file, a replayed reconnect carries the server session); a startup fault exits 1 (on Error)mre
- f5a55f8aident#11: apps can ask for nickname, firstname and lastname (fields list on /api/exchange, new POST /api/profile); empty fields come back null; gate checks, READMEmre
- d8be66ebident: report and log line for the widget cache and frame height changemre
- 2068f61aident#31: browsers revalidate the widget scripts on every load; the sign-in frame is exactly as tall as the current step (the frame page reports its height, selector.js accepts it from ident only)mre
- a22d2daaident#31: the sign-in frame is no taller than the tallest step needs (plain CSS height per width), gate check for itmre
- a77d1b22ident#31: the welcome form keeps editId a plain value (browser gate), README, wider rows in the framemre
- 5c481bfdident#31: sign in to ident inside the selector's panel (email, code, identity in an iframe; a new address goes to ident in the same tab and comes back)mre
- 061abfe2ident: the apps gate waits until the test app's login button stops moving before it clicks (the page grows after load and the click missed about every second run)mre
- 45f7e4daident: the user menu's Sign out waits for the server before it reloads (the apps gate failed because the session could still be signed in)mre
- f07658c7ident: home page shows the account once (account bar removed, the user menu carries email, Sign out, Sign out everywhere); wildcard origins https://*.domain for apps on many subdomains; gates follow (WIP: gates not re-run after the last edits)mre
- f2c43bbbtheme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre