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
- 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
- e36f4516Merge branch 'f14-online' into mainIdent
- 2c5eac91|||PR ident#38 online state without probe timermre
- bf1980f2Merge branch 'v56-day56' into mainIdent
- 62ae0adf|||PR ident: vendor Hybriel day56mre
- 0d18abc4Merge branch 'f13-avatar' into mainIdent
- 2a99730a|||PR ident#37 avatar in hlmre
- 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