SPB Git

Repository Public

inbound.ts 8.4 KB 119 lines typescript

1// Veilleur de réception : suit Email/changes de chaque boîte à partir d'un état persistant. Émet message.received pour2// tout message arrivé, puis message.read / message.deleted pour ces mêmes messages (au mieux). Après une resynchronisation3// (état inconnu du serveur), repart de l'état courant SANS réémettre les anciens messages.4import { sql } from "../db.js"5import { folders, jmap, jmapOk, type Mbx } from "../mailjmap.js"6import { deliveredTo } from "../v1/context.js"7import { msgId, threadId } from "../v1/ids.js"8import { emitEvent, type EventInput } from "../webhooks.js"9import { runInboundHooks, type InboundBatch } from "../next/hooks.js"1011const PROPS = ["threadId", "mailboxIds", "keywords", "from", "to", "cc", "subject", "preview", "receivedAt", "size", "hasAttachment", "header:Delivered-To:asText:all",12  // SP Mail Next : fil de réponse, expéditeur de liste et identifiant de message, pour les crochets (push groupé, règles, contacts)13  "messageId", "inReplyTo", "replyTo", "bcc", "sentAt", "header:List-Id:asText", "header:List-Unsubscribe:asRaw", "header:Auto-Submitted:asText", "header:Precedence:asText"]14type E = Record<string, any> & { id: string }1516async function currentState(mb: Mbx): Promise<string> {17  const [r] = await jmapOk(mb, [["Email/get", { ids: [] }]])18  return r.state as string19}2021async function watchOne(mb: Mbx, state: string | null) {22  if (!state) {23    await sql`update mailbox_sync set email_state = ${await currentState(mb)}, updated_at = now() where mailbox_id = ${mb.id}`24    return25  }26  for (let round = 0; round < 20; round++) {27    const res = await jmap(mb, [["Email/changes", { sinceState: state, maxChanges: 200 }, "c"]])28    if (res[0][0] === "error") {29      if (!["cannotCalculateChanges", "invalidArguments"].includes(res[0][1].type)) throw new Error(`Email/changes: ${res[0][1].type}`)30      await sql`update mailbox_sync set email_state = ${await currentState(mb)}, resyncs = resyncs + 1, updated_at = now() where mailbox_id = ${mb.id}`31      return32    }33    const ch = res[0][1] as { created: string[]; updated: string[]; destroyed: string[]; newState: string; hasMoreChanges: boolean }34    if (!ch.created.length && !ch.updated.length && !ch.destroyed.length) {35      // rien de neuf : au plus l'état à avancer (cas le plus fréquent, aucune autre lecture)36      if (ch.newState !== state) await sql`update mailbox_sync set email_state = ${ch.newState}, updated_at = now() where mailbox_id = ${mb.id}`37      return38    }39    const ids = [...new Set([...ch.created, ...ch.updated])]40    const byId = new Map<string, E>()41    if (ids.length) {42      const [g] = await jmapOk(mb, [["Email/get", { ids, properties: PROPS }]])43      for (const e of g.list as E[]) byId.set(e.id, e)44    }45    const fl = await folders(mb, false, [...byId.values()].flatMap((e) => Object.keys(e.mailboxIds ?? {})))46    const roleOf = (id: string) => fl.find((f) => f.id === id)?.role ?? null47    const known = new Set((await sql`select address from addresses where mailbox_id = ${mb.id}`).map((a) => a.address as string))48    const events: EventInput[] = []49    const upserts: { id: string; address: string; seen: boolean; trashed: boolean }[] = []50    const removals: string[] = []51    const batch: InboundBatch = { mb, received: [], createdOther: [], updated: [], destroyed: ch.destroyed }52    const watched = new Map((ids.length || ch.destroyed.length ? await sql`select email_id, address, seen, trashed from watched_messages where mailbox_id = ${mb.id} and email_id = any(${[...ids, ...ch.destroyed]})` : []).map((w) => [w.email_id as string, w]))53    const meta = (e: E, address: string) => ({54      message_id: msgId(mb.id, e.id), thread_id: threadId(mb.id, e.threadId), address, from: (e.from ?? []).map((a: { email: string }) => a.email), to: (e.to ?? []).map((a: { email: string }) => a.email),55      cc: (e.cc ?? []).map((a: { email: string }) => a.email), delivered_to: deliveredTo(e), received_at: e.receivedAt, size: e.size ?? 0, has_attachment: !!e.hasAttachment,56      folders: Object.keys(e.mailboxIds ?? {}).map(roleOf).filter(Boolean),57    })58    for (const id of ch.created) {59      const e = byId.get(id)60      if (!e || watched.has(id)) continue61      const roles = Object.keys(e.mailboxIds ?? {}).map(roleOf)62      // Brouillons et copies « Envoyés » ne sont pas des réceptions.63      if (e.keywords?.$draft || (roles.length > 0 && roles.every((r) => r === "drafts" || r === "sent"))) { batch.createdOther.push({ email: e, roles }); continue }64      const address = deliveredTo(e).find((a) => known.has(a)) ?? mb.address65      const trashed = roles.includes("trash")66      events.push({ type: "message.received", address, mailboxId: mb.id, data: meta(e, address), content: { subject: e.subject ?? "", preview: e.preview ?? "" } })67      upserts.push({ id, address, seen: !!e.keywords?.$seen, trashed })68      batch.received.push({ email: e, address, roles })69      watched.set(id, { email_id: id, address, seen: !!e.keywords?.$seen, trashed })70    }71    for (const id of ch.updated) {72      const e = byId.get(id), w = watched.get(id)73      if (e) batch.updated.push({ email: e, roles: Object.keys(e.mailboxIds ?? {}).map(roleOf) })74      if (!e || !w) continue75      const seen = !!e.keywords?.$seen76      const trashed = Object.keys(e.mailboxIds ?? {}).map(roleOf).includes("trash")77      if (seen && !w.seen) events.push({ type: "message.read", address: w.address, mailboxId: mb.id, data: { message_id: msgId(mb.id, id), thread_id: threadId(mb.id, e.threadId), address: w.address } })78      if (trashed && !w.trashed) events.push({ type: "message.deleted", address: w.address, mailboxId: mb.id, data: { message_id: msgId(mb.id, id), address: w.address, permanent: false } })79      if (seen !== w.seen || trashed !== w.trashed) upserts.push({ id, address: w.address, seen, trashed })80    }81    for (const id of ch.destroyed) {82      const w = watched.get(id)83      if (!w) continue84      events.push({ type: "message.deleted", address: w.address, mailboxId: mb.id, data: { message_id: msgId(mb.id, id), address: w.address, permanent: true } })85      removals.push(id)86    }87    // Événements, suivi et nouvel état sont écrits ensemble : ni perte ni doublon après un arrêt brutal.88    await sql.begin(async (tx) => {89      for (const ev of events) await emitEvent(ev, tx as never)90      await runInboundHooks(tx as never, batch)91      for (const u of upserts) await tx`insert into watched_messages (mailbox_id, email_id, address, seen, trashed) values (${mb.id}, ${u.id}, ${u.address}, ${u.seen}, ${u.trashed})92        on conflict (mailbox_id, email_id) do update set seen = excluded.seen, trashed = excluded.trashed`93      if (removals.length) await tx`delete from watched_messages where mailbox_id = ${mb.id} and email_id = any(${removals})`94      await tx`update mailbox_sync set email_state = ${ch.newState}, updated_at = now() where mailbox_id = ${mb.id}`95    })96    state = ch.newState97    if (!ch.hasMoreChanges) return98  }99}100101/** Échecs consécutifs du veilleur par boîte (mémoire du processus) : une boîte qui échoue sans cesse ne reçoit plus ni push, ni règles, ni webhooks. */102export const watcherFailures = new Map<string, { address: string; failures: number; since: number; lastError: string }>()103104export async function watchInbound() {105  await sql`insert into mailbox_sync (mailbox_id) select id from mailboxes on conflict do nothing`106  const rows = await sql`update mailbox_sync set locked_until = now() + interval '2 minutes'107    where mailbox_id in (select mailbox_id from mailbox_sync where locked_until is null or locked_until < now() for update skip locked)108    returning mailbox_id, email_state`109  const boxes = rows.length ? ((await sql`select id, user_id, address, display_name, stalwart_id, secret_enc from mailboxes where id = any(${rows.map((r) => r.mailbox_id)}::uuid[])`) as unknown as Mbx[]) : []110  await Promise.all(boxes.map(async (mb) => {111    // Boîte ou serveur indisponible : prochain tour. L'échec n'est plus muet : il est compté par boîte (contrôle de santé « watcher », alerte).112    try { await watchOne(mb, rows.find((r) => r.mailbox_id === mb.id)!.email_state); watcherFailures.delete(mb.id) } catch (e) {113      const f = watcherFailures.get(mb.id)114      watcherFailures.set(mb.id, { address: mb.address, failures: (f?.failures ?? 0) + 1, since: f?.since ?? Date.now(), lastError: String((e as Error)?.message ?? e).slice(0, 200) })115    }116    await sql`update mailbox_sync set locked_until = null where mailbox_id = ${mb.id}`117  }))118}119
Files main
Menu

Theme