SPB Git

Repository Public

dashboard.ts 12.2 KB 166 lines typescript

1// SP Mail Next — centre de commande : agrégats bon marché calculés en parallèle (réception, attention, envois, pièces jointes, contacts,2// stockage, synchronisation), avec un court cache par utilisateur. Chaque bloc échoue seul et se déclare dans `partial` : une panne partielle ne vide pas le tableau.3import type { FastifyInstance } from "fastify"4import { config } from "../config.js"5import { sql } from "../db.js"6import { folders, jmapOk } from "../mailjmap.js"7import { presentOutbound, type OutboundRow } from "../outbound.js"8import { getAll } from "../stalwart.js"9import { nextUser, userMailboxes, type OwnedMailbox } from "./common.js"10import { topContacts } from "./contacts.js"11import { listAttachments, presentSummary, type MessageSummary } from "./search.js"1213const CACHE_MS = 15_00014const cache = new Map<string, { at: number; data: Awaited<ReturnType<typeof build>> }>()15/** Délai de garde d'un résultat partiel : assez pour amortir une rafale, trop court pour afficher longtemps un bloc en panne. */16const PARTIAL_CACHE_MS = 2_00017const PENDING_SCAN = 30 // fils examinés par boîte pour « réponses attendues »18const PENDING_MAX = 1019const tableExists = async (name: string) => !!(await sql`select to_regclass(${`public.${name}`}) as t`)[0]?.t2021// ------------------------------------------------------------------ blocs22/** Compteurs de la boîte de réception de chaque boîte ; dit aussi si le serveur de courrier a répondu. */23async function inboxCounts(boxes: OwnedMailbox[]) {24  let reached = 0, failed = 025  const data = (await Promise.all(boxes.map(async (mb) => {26    try {27      const inbox = (await folders(mb, true)).find((f) => f.role === "inbox") // relu : les compteurs du cache de dossiers peuvent dater de 20 s28      reached++29      return { mailbox_id: mb.id, address: mb.address, unread: inbox?.unreadEmails ?? 0, total: inbox?.totalEmails ?? 0 }30    } catch { failed++; return null }31  }))).filter((x) => x !== null)32  return { data, reached, failed }33}3435async function flaggedCount(boxes: OwnedMailbox[]): Promise<number> {36  const totals = await Promise.all(boxes.map(async (mb) => {37    const hidden = (await folders(mb)).filter((f) => f.role === "trash" || f.role === "junk").map((f) => f.id)38    const filter = hidden.length ? { operator: "AND", conditions: [{ hasKeyword: "$flagged" }, { inMailboxOtherThan: hidden }] } : { hasKeyword: "$flagged" }39    const [r] = await jmapOk(mb, [["Email/query", { filter, limit: 1, calculateTotal: true }]])40    return Number(r.total ?? 0)41  }))42  return totals.reduce((a, b) => a + b, 0)43}4445/**46 * Fils de la réception qui attendent une réponse : dernier message d'autrui, pas de réponse ($answered absent), non lu ou marqué.47 * Borné à la source : au plus PENDING_SCAN fils lus par boîte ; la réponse n'en porte que PENDING_MAX.48 */49async function pendingReplies(userId: string, boxes: OwnedMailbox[]): Promise<MessageSummary[]> {50  const own = new Set((await sql`select a.address from addresses a join mailboxes m on m.id = a.mailbox_id where m.user_id = ${userId} or m.id = any(${boxes.map((b) => b.id)}::uuid[])`).map((r) => String(r.address).toLowerCase()))51  const fromOwn = (e: Record<string, any> | undefined) => ((e?.from ?? []) as { email?: string }[]).some((x) => own.has(String(x.email ?? "").toLowerCase()))52  const parts = await Promise.all(boxes.map(async (mb) => {53    const fl = await folders(mb)54    const inbox = fl.find((f) => f.role === "inbox")55    if (!inbox) return []56    const filter = { operator: "AND", conditions: [{ inMailbox: inbox.id }, { notKeyword: "$answered" }, { operator: "OR", conditions: [{ notKeyword: "$seen" }, { hasKeyword: "$flagged" }] }] }57    const res = await jmapOk(mb, [58      ["Email/query", { filter, sort: [{ property: "receivedAt", isAscending: false }], collapseThreads: true, limit: PENDING_SCAN }, "q"],59      ["Email/get", { "#ids": { resultOf: "q", name: "Email/query", path: "/ids" }, properties: ["threadId", "mailboxIds", "keywords", "from", "to", "subject", "preview", "receivedAt", "size", "hasAttachment"] }, "g"],60      ["Thread/get", { "#ids": { resultOf: "g", name: "Email/get", path: "/list/*/threadId" } }, "t"],61    ])62    const list = res[1].list as (Record<string, any> & { id: string })[]63    const lastOf = new Map((res[2].list as { id: string; emailIds: string[] }[]).map((t) => [t.id, t.emailIds[t.emailIds.length - 1]]))64    const others = [...new Set(list.map((e) => lastOf.get(e.threadId)).filter((id): id is string => !!id && !list.some((e) => e.id === id)))]65    const extra = new Map<string, Record<string, any>>()66    if (others.length) for (const e of (await jmapOk(mb, [["Email/get", { ids: others, properties: ["from"] }]]))[0].list as (Record<string, any> & { id: string })[]) extra.set(e.id, e)67    return list.filter((e) => {68      const lastId = lastOf.get(e.threadId)69      const last = lastId === e.id || !lastId ? e : extra.get(lastId)70      return !fromOwn(e) && !fromOwn(last)71    }).map((e) => presentSummary(mb, e, fl))72  }))73  return parts.flat().sort((a, b) => Date.parse(b.received_at) - Date.parse(a.received_at))74}7576/** Sujet des envois de la file, lu dans le brouillon ou le message conservé (null s'il a disparu ou si la boîte ne répond pas). */77async function withSubjects(rows: OutboundRow[], boxes: OwnedMailbox[], failed: () => void) {78  const subjects = new Map<string, string | null>()79  const byBox = new Map<string, string[]>()80  for (const o of rows) if (o.email_id) byBox.set(o.mailbox_id, [...(byBox.get(o.mailbox_id) ?? []), o.email_id])81  await Promise.all([...byBox].map(async ([mailboxId, ids]) => {82    const mb = boxes.find((b) => b.id === mailboxId)83    if (!mb) return84    try {85      const [r] = await jmapOk(mb, [["Email/get", { ids, properties: ["subject"] }]], 10_000)86      for (const e of r.list as { id: string; subject?: string | null }[]) subjects.set(`${mailboxId}/${e.id}`, e.subject ?? null)87    } catch { failed() /* sujets inconnus : null, les lignes restent, le bloc est déclaré partiel */ }88  }))89  return rows.map((o) => presentOutbound(o, { subject: (o.email_id ? subjects.get(`${o.mailbox_id}/${o.email_id}`) : null) ?? null }))90}9192async function storage(userId: string, boxes: OwnedMailbox[]) {93  // Même source que GET /api/usage : la gestion du serveur de courrier (Quota/get est vide sans quota défini).94  const accts = await getAll("Account", ["usedDiskQuota"]).catch(() => [] as Record<string, any>[])95  const mine = boxes.filter((b) => b.user_id === userId)96  const quotas = new Map((mine.length ? await sql`select mailbox_id, quota_bytes from addresses where address = any(${mine.map((b) => b.address)}::text[])` : []).map((r) => [r.mailbox_id as string, (r.quota_bytes ?? null) as number | null]))97  return mine.map((b) => ({ mailbox_id: b.id, used_bytes: (accts.find((x) => x.id === b.stalwart_id)?.usedDiskQuota ?? null) as number | null, quota_bytes: quotas.get(b.id) ?? null }))98}99100/**101 * Retard du veilleur : 0 quand l'état JMAP enregistré est l'état courant de chaque boîte ; sinon le temps écoulé depuis sa dernière102 * avancée sur la boîte la plus en retard (borne supérieure : `mailbox_sync.updated_at` ne bouge que lorsqu'il y a du nouveau).103 */104async function watcherLag(boxes: OwnedMailbox[]): Promise<number | null> {105  if (!boxes.length) return 0106  const rows = await sql`select mailbox_id, email_state, updated_at from mailbox_sync where mailbox_id = any(${boxes.map((b) => b.id)}::uuid[])`107  const lags = await Promise.all(boxes.map(async (mb) => {108    const row = rows.find((r) => r.mailbox_id === mb.id)109    if (!row?.email_state) return null110    const [r] = await jmapOk(mb, [["Email/get", { ids: [] }]], 10_000)111    return r.state === row.email_state ? 0 : Math.max(0, Date.now() - new Date(row.updated_at).getTime())112  }))113  const known = lags.filter((l): l is number => l !== null)114  return known.length ? Math.max(...known) : null115}116117async function build(userId: string) {118  const boxes = await userMailboxes(userId)119  // Chaque bloc échoue seul, mais jamais en silence : son nom est rendu dans `partial` (le client affiche « indisponible », pas un faux zéro).120  const partial = new Set<string>()121  const section = <T>(names: string[], p: Promise<T>, fallback: T): Promise<T> => p.catch(() => { for (const n of names) partial.add(n); return fallback })122  const outbound = (name: string) => (rows: unknown) => withSubjects(rows as OutboundRow[], boxes, () => partial.add(name))123  const [inbox, flagged, remindersDue, snoozedToday, pending, scheduled, issues, attachments, contacts, store, lag] = await Promise.all([124    inboxCounts(boxes),125    section(["attention", "attention.flagged"], flaggedCount(boxes), 0),126    // Rappels et mises en veille appartiennent à un autre module : sans ses tables, les compteurs valent 0.127    section(["attention", "attention.reminders_due"], (async () => (await tableExists("reminders")) ? Number((await sql`select count(*)::int as n from reminders where user_id = ${userId} and status = 'due'`)[0].n) : 0)(), 0),128    section(["attention", "attention.snoozed_today"], (async () => (await tableExists("snoozes")) ? Number((await sql`select count(*)::int as n from snoozes where user_id = ${userId} and status = 'active' and until <= now() + interval '24 hours'`)[0].n) : 0)(), 0),129    section(["attention", "attention.unanswered", "pending_replies"], pendingReplies(userId, boxes), [] as MessageSummary[]),130    section(["scheduled"], sql`select * from outbound_messages where user_id = ${userId} and status = 'scheduled' order by send_at nulls last, created_at limit 20`.then(outbound("scheduled")), []),131    section(["delivery_issues"], sql`select * from outbound_messages where user_id = ${userId} and status in ('bounced', 'failed', 'deferred') and updated_at > now() - interval '7 days' order by updated_at desc limit 20`.then(outbound("delivery_issues")), []),132    // Un même fichier figure dans « Envoyés » de l'expéditeur et dans la réception du destinataire quand les deux boîtes sont à l'utilisateur :133    // une seule ligne par contenu (même blob, ou même nom et même taille), la plus récente.134    section(["recent_attachments"], listAttachments(userId, { limit: 24 }).then((r) => { const seen = new Set<string>(); return r.data.filter((a) => { const k = `${a.name}\u0000${a.size}`; if (seen.has(k)) return false; seen.add(k); return true }).slice(0, 8) }), []),135    section(["top_contacts"], topContacts(userId, 8), []),136    section(["storage"], storage(userId, boxes), []),137    section(["sync"], watcherLag(boxes), null),138  ])139  if (inbox.failed) partial.add("inbox")140  // Sans boîte, la joignabilité se lit comme /api/health/ready (401 sans identifiants = joignable).141  const reachable = boxes.length ? inbox.reached > 0 : !!(await fetch(`${config.stalwartUrl}/jmap/session`, { signal: AbortSignal.timeout(4000) }).catch(() => null))142  return {143    inbox: inbox.data,144    attention: { reminders_due: remindersDue, flagged, snoozed_today: snoozedToday, unanswered: pending.length },145    scheduled, pending_replies: pending.slice(0, PENDING_MAX), recent_attachments: attachments, top_contacts: contacts, storage: store, delivery_issues: issues,146    sync: { watcher_lag_ms: lag, mail_server: reachable ? "reachable" as const : "unreachable" as const, push: (config as { apnsKeyFile?: string }).apnsKeyFile ? "configured" as const : "disabled" as const },147    /** Blocs qui n'ont pas pu être calculés (clés de la réponse ; `attention.<compteur>` précise le compteur) : leurs valeurs sont des replis, pas des mesures. */148    partial: [...partial].sort(),149    generated_at: new Date().toISOString(),150  }151}152153export function dashboardRoutes(app: FastifyInstance) {154  // `fresh=1` contourne le cache (après une action de l'utilisateur) ; la limite de débit de la session s'applique.155  app.get("/api/next/dashboard", async (req) => {156    const a = await nextUser(req)157    const hit = cache.get(a.userId)158    // Un résultat partiel n'est gardé que PARTIAL_CACHE_MS : le bloc en panne est recalculé presque aussitôt.159    if ((req.query as { fresh?: string }).fresh !== "1" && hit && Date.now() - hit.at < (hit.data.partial.length ? PARTIAL_CACHE_MS : CACHE_MS)) return hit.data160    const data = await build(a.userId)161    if (cache.size > 500) for (const [k, v] of cache) if (Date.now() - v.at > CACHE_MS) cache.delete(k)162    cache.set(a.userId, { at: Date.now(), data })163    return data164  })165}166
Files main
Menu

Theme