Files
mesh-catalog/modules/mailu/index.ts
T
jschoubben 7b06a7a408 Event names are local now, in the manifests and in the code
Every module named its events the way the old bus spelled a routing key —
`module.<module>.<verb>`. Design 29 says a module names an event locally and the
mesh works out where it lands, so all 37 were stale against a rule already
decided. On the new bus that derives into a namespace belonging to a module
called "module", so no cross-module subscription in the mesh matched anything:
nothing failed, nothing reacted (novox/hq 04-ISSUES/127).

36 manifests converted, and 43 files of module code with them. The code mattered
as much as the manifests: the runtime builds the subject from what `emit()` is
handed, so a converted manifest with unconverted code would have had the
permission and the subject disagree.

Three things the new check found on the way:

- `photos` emitted an event its manifest never declared, which the new bus refuses
  outright. Declared.
- `showcase` waited for an event nothing emits, so its demo could never be
  triggered — only `showcase` may publish under its own name. It emits both halves
  now.
- `distribution` declared an event named after a different module. It emits
  `image.pushed` under its own name. An event about a *role* belongs on the seat,
  where the name outlives whoever holds it, but the sdk has no way to publish on a
  seat yet, so that stays recorded rather than declared.

The audit logger's "everything" pattern is `**` rather than the old bus's `#`.
2026-09-27 14:42:28 +02:00

62 lines
2.6 KiB
TypeScript

// mailu's events. The tool runtime imports this once the broker is bound. It watches the mail
// server's accounts and announces what changed, so the rest of the mesh can react to a mailbox
// appearing or an alias being pointed somewhere new.
//
// Emits (novox/hq ADR 0041/0042):
// module.mailu.user.created / .deleted — a mailbox appeared or was removed
// module.mailu.alias.created / .deleted — an alias was added or removed
//
// Consumes: nothing. Mail accounts are not something the mesh should mutate in the background off
// another module's event — a wrong reaction here silently loses mail. mailu observes and announces;
// it does not act on what others do. If a real consumer is ever wanted, it is a deliberate addition.
//
// Detection is by polling the admin API and diffing, exactly as plex diffs its sessions: the change
// may have come from the web admin as easily as from a tool, and a diff catches both. The first
// look primes silently, or a restart would re-announce every existing account as freshly created.
import { emit } from "@novox/mesh-sdk/events";
import { MailuClient } from "./client.js";
const mailu = MailuClient.fromEnv();
// A generic diff over a keyed set: emit `created` for keys that appeared, `deleted` for keys that
// went away, and stay silent until primed. Users and aliases are the same shape of watch.
function watcher(created: string, deleted: string): (keys: string[]) => Promise<void> {
const known = new Set<string>();
let primed = false;
return async (keys: string[]) => {
const now = new Set(keys);
if (primed) {
for (const key of now) if (!known.has(key)) await emit(created, { email: key });
for (const key of known) if (!now.has(key)) await emit(deleted, { email: key });
}
known.clear();
for (const key of now) known.add(key);
primed = true;
};
}
const watchUsers = watcher("user.created", "user.deleted");
const watchAliases = watcher("alias.created", "alias.deleted");
async function pollUsers(): Promise<void> {
await watchUsers((await mailu.listUsers()).map((u) => u.email));
}
async function pollAliases(): Promise<void> {
await watchAliases((await mailu.listAliases()).map((a) => a.email));
}
const tick = (fn: () => Promise<void>, everyMs: number): void => {
const runOnce = (): void => void fn().catch((err) => console.error(`[mailu] ${err}`));
setInterval(runOnce, everyMs);
runOnce();
};
// Accounts and aliases change on human time, not machine time — a minute's latency is fine, and
// polling the admin API harder buys immediacy nobody asked for.
tick(pollUsers, 60_000);
tick(pollAliases, 60_000);
console.log("[mailu] watching users and aliases");