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
- 378bd432ident#34: the identity picture look is a static in styles.hl (Style.pAvatar); sizes stay with each listmre
- 1cf79e7eident#34: the wide page column is a static in styles.hl (Style.pWide), not the class "wide"mre
- ddc7d135Merge branch 'f7-hooks' into mainIdent
- eee0b513|||PR ident#34 part: test-hook classes replaced by name/element/attribute selectors (tests adjusted), the shared lead/message/notice looks are statics in styles.hlmre
- ccd6d0c4ident#34: browser gate: rowButton finds the row's button by namemre
- 24dd414eident#34: the shared lead, message and notice looks are statics in components/styles.hl, imported by the components (lib/looks.hl gone)mre
- 03ed26c7ident#34: access.hl: the remaining lead classes droppedmre
- 1d066c42ident#34: access.hl drops its unstyled lead class (the shared lead rule would otherwise mute it)mre
- 1e1dd7b4ident#34: the access page keeps its lead class string (the page has no ident-card, so its lead line was never muted; a shared rule would change the look)mre
- 89ac6553ident#34: the lead, message and notice lines are named with Style.pLead / pMessage / pNotice (shared bodies in lib/looks.hl) instead of class stringsmre
- e84be3deident#34: browser gate lists the Edit/Delete buttons by namemre
- fb7111faident#34: access page: unstyled test-hook classes (idrow, never, who, whoname, appname, neverrevoke) dropped; the tests use element/name selectorsmre
- 9f2ad803ident#34: notification switches: push/email by name=..., locked by aria-disabled, the switch look is the role=switch button (no switch/push/email/locked classes)mre
- e0f0ddaaident#34: inbox: the Mark read/unread button is styled as the button of its note-foot, the icon is found as the row's img (no toggle/icon classes)mre
- 16b49756ident#34: unused row classes (choice, usedrow, kind, shortid) droppedmre
- f9d6fb38ident#34: apps and identity rows: the Edit/New secret/Delete buttons carry name=..., the API key is found by its element; unused row classes (app, identity, shortid) droppedmre
- b323265aMerge branch 'f5-styles' into mainIdent
- 9ced53c3|||PR ident#33 ident#35 every component's styles live in its own Style block; styles.hl keeps the page-wide rest; the account bar is phone first (min-width: 60rem block); the avatar preview is hidden with the hidden attribute (ident#34 in part)mre
- f28333e4ident#33 ident#35: styles.hl keeps only the accent, the page column and the shared patterns; the account bar is written phone first (base rule + one min-width: 60rem block) instead of a max-width overridemre
- 5b4541fcident#33: the selector sign-in frame's rules move into embed.hl's Stylemre