Repository Public
1// SP Mail Next — notifications push : appareils et préférences (/api/next/push), crochet du veilleur (mise en file dans sa2// transaction), travailleur d'envoi APNs (baux, recul, pastille calculée à l'envoi, jetons morts supprimés, purge).3// Sans réglage APNs, rien n'est mis en file et /api/next/push/status l'annonce : aucune remise n'est jamais simulée.4import type { FastifyInstance } from "fastify"5import { z } from "zod"6import { config } from "../config.js"7import { sql, type Sql } from "../db.js"8import { folders } from "../mailjmap.js"9import { bad, parse, zUuid } from "../util.js"10import { APNS_HOSTS, buildHeaders, buildPayload, genericText, inQuietHours, isPrioritySender, isValidTimeZone, previewText, pushConfigError, pushConfigured, sendApns, type Notice, type Preview, type PushItem, type PushKind, type QuietHours } from "./apns.js"11import { nextUser, userMailboxes } from "./common.js"12import { onInbound, type InboundBatch } from "./hooks.js"13import type { NextWorker } from "./index.js"1415// ------- préférences16export type PushPrefs = { enabled: boolean; preview: Preview; quiet_hours: QuietHours | null; priority_senders: string[]; priority_only: boolean; mailboxes: Record<string, boolean>; badge: "inbox" | "none" }17const DEFAULT_PREFS: PushPrefs = { enabled: true, preview: "sender", quiet_hours: null, priority_senders: [], priority_only: false, mailboxes: {}, badge: "inbox" }18type DevicePrefs = { mailboxes?: Record<string, boolean>; sound?: boolean }1920const zHHMM = z.string().regex(/^([01]\d|2[0-3]):[0-5]\d$/)21const zMailboxFlags = z.record(zUuid, z.boolean())22const zSender = z.string().trim().toLowerCase().max(254).regex(/^(@[a-z0-9.-]+\.[a-z0-9-]+|[^@\s]+@[a-z0-9.-]+\.[a-z0-9-]+)$/)23const zPrefs = z.object({24 enabled: z.boolean(),25 preview: z.enum(["none", "sender", "full"]),26 quiet_hours: z.object({ start: zHHMM, end: zHHMM, tz: z.string().max(64).refine(isValidTimeZone, "unknown time zone") }).nullable(),27 priority_senders: z.array(zSender).max(200),28 priority_only: z.boolean(),29 mailboxes: zMailboxFlags,30 badge: z.enum(["inbox", "none"]),31}).partial()32const zDevicePatch = z.object({ enabled: z.boolean().optional(), prefs: z.object({ mailboxes: zMailboxFlags.optional(), sound: z.boolean().optional() }).optional() })3334const prefsOfRow = (r: Record<string, any> | undefined): PushPrefs => r35 ? { enabled: r.enabled, preview: r.preview, quiet_hours: r.quiet_hours ?? null, priority_senders: r.priority_senders ?? [], priority_only: r.priority_only, mailboxes: r.mailboxes ?? {}, badge: r.badge }36 : { ...DEFAULT_PREFS }37async function loadPrefs(q: Sql, userId: string): Promise<PushPrefs> {38 const [r] = await q`select * from push_prefs where user_id = ${userId}`39 return prefsOfRow(r)40}41/** Ne garde que les boîtes lisibles par l'utilisateur : un identifiant étranger ou périmé n'est pas conservé. */42async function ownFlags(userId: string, flags: Record<string, boolean>): Promise<Record<string, boolean>> {43 const mine = new Set((await userMailboxes(userId)).map((m) => m.id))44 return Object.fromEntries(Object.entries(flags).filter(([id]) => mine.has(id)))45}46const iso = (d: unknown) => (d ? new Date(d as string).toISOString() : null)4748// ------- routes49export function pushRoutes(app: FastifyInstance) {50 app.get("/api/next/push/devices", async (req) => {51 const a = await nextUser(req)52 const rows = await sql`select id, session_id, device_name, bundle_id, environment, enabled, prefs, locale, app_version, created_at, last_registered_at, last_push_at, last_error53 from push_devices where user_id = ${a.userId} order by created_at`54 return { data: rows.map((d) => ({55 id: d.id, session_id: d.session_id, device_name: d.device_name, bundle_id: d.bundle_id, environment: d.environment, enabled: d.enabled, current: d.session_id === a.sessionId,56 prefs: d.prefs ?? {}, locale: d.locale, app_version: d.app_version, created_at: iso(d.created_at), last_registered_at: iso(d.last_registered_at), last_push_at: iso(d.last_push_at), last_error: d.last_error,57 })) }58 })5960 app.patch("/api/next/push/devices/:id", async (req) => {61 const a = await nextUser(req)62 const { id } = req.params as { id: string }63 if (!zUuid.safeParse(id).success) throw bad("not_found", 404)64 const b = parse(zDevicePatch, req.body)65 const [d] = await sql`select id, enabled, prefs from push_devices where id = ${id} and user_id = ${a.userId}`66 if (!d) throw bad("not_found", 404)67 const prefs: DevicePrefs = { ...(d.prefs as DevicePrefs) }68 if (b.prefs?.mailboxes !== undefined) prefs.mailboxes = await ownFlags(a.userId, b.prefs.mailboxes)69 if (b.prefs?.sound !== undefined) prefs.sound = b.prefs.sound70 const enabled = b.enabled ?? (d.enabled as boolean)71 await sql`update push_devices set enabled = ${enabled}, prefs = ${sql.json(prefs as never)} where id = ${id}`72 // un appareil coupé ne reçoit pas ce qui attendait encore73 if (!enabled) await sql`update push_outbox set status = 'dropped', last_error = 'device disabled', locked_until = null where device_id = ${id} and status = 'pending'`74 return { ok: true, id, enabled, prefs }75 })7677 app.delete("/api/next/push/devices/:id", async (req) => {78 const a = await nextUser(req)79 const { id } = req.params as { id: string }80 if (!zUuid.safeParse(id).success) throw bad("not_found", 404)81 const r = await sql`delete from push_devices where id = ${id} and user_id = ${a.userId} returning 1`82 if (!r.length) throw bad("not_found", 404)83 return { ok: true }84 })8586 app.get("/api/next/push/prefs", async (req) => {87 const a = await nextUser(req)88 return loadPrefs(sql, a.userId)89 })9091 app.put("/api/next/push/prefs", async (req) => {92 const a = await nextUser(req)93 const b = parse(zPrefs, req.body)94 // Seules les clés présentes dans le corps sont appliquées : un client plus ancien n'efface pas un réglage qu'il ignore.95 const raw = (req.body ?? {}) as Record<string, unknown>96 const next: PushPrefs = await loadPrefs(sql, a.userId)97 if ("enabled" in raw && b.enabled !== undefined) next.enabled = b.enabled98 if ("preview" in raw && b.preview !== undefined) next.preview = b.preview99 if ("quiet_hours" in raw && b.quiet_hours !== undefined) next.quiet_hours = b.quiet_hours100 if ("priority_senders" in raw && b.priority_senders !== undefined) next.priority_senders = [...new Set(b.priority_senders)]101 if ("priority_only" in raw && b.priority_only !== undefined) next.priority_only = b.priority_only102 if ("mailboxes" in raw && b.mailboxes !== undefined) next.mailboxes = await ownFlags(a.userId, b.mailboxes)103 if ("badge" in raw && b.badge !== undefined) next.badge = b.badge104 await sql`insert into push_prefs (user_id, enabled, preview, quiet_hours, priority_senders, priority_only, mailboxes, badge)105 values (${a.userId}, ${next.enabled}, ${next.preview}, ${next.quiet_hours ? sql.json(next.quiet_hours as never) : null}, ${next.priority_senders}, ${next.priority_only}, ${sql.json(next.mailboxes as never)}, ${next.badge})106 on conflict (user_id) do update set enabled = excluded.enabled, preview = excluded.preview, quiet_hours = excluded.quiet_hours, priority_senders = excluded.priority_senders,107 priority_only = excluded.priority_only, mailboxes = excluded.mailboxes, badge = excluded.badge, updated_at = now()`108 return next109 })110111 app.get("/api/next/push/status", async (req) => {112 const a = await nextUser(req)113 const [c] = await sql`select count(*) filter (where status = 'pending')::int as pending,114 count(*) filter (where status = 'failed' and created_at > now() - interval '24 hours')::int as failed_24h,115 count(*) filter (where status = 'sent' and sent_at > now() - interval '24 hours')::int as sent_24h116 from push_outbox where user_id = ${a.userId}`117 const [e] = await sql`select last_error from push_devices where user_id = ${a.userId} and last_error is not null order by coalesce(last_push_at, last_registered_at) desc limit 1`118 const configured = pushConfigured()119 return {120 configured,121 environment_hosts: { production: config.apnsHostOverride || APNS_HOSTS.production, sandbox: config.apnsHostOverride || APNS_HOSTS.sandbox },122 queue: { pending: c.pending, failed_24h: c.failed_24h, sent_24h: c.sent_24h },123 last_error: configured ? ((e?.last_error as string | undefined) ?? null) : pushConfigError(),124 }125 })126127 app.post("/api/next/push/test", async (req) => {128 const a = await nextUser(req, "push-test", 20)129 const b = parse(z.object({ device_id: zUuid.optional() }), req.body ?? {})130 if (b.device_id) {131 const [d] = await sql`select 1 from push_devices where id = ${b.device_id} and user_id = ${a.userId}`132 if (!d) throw bad("not_found", 404)133 }134 const queued = await enqueueFor(sql, a.userId, { kind: "test", title: "SP Mail", body: "", dedup: crypto.randomUUID() }, b.device_id)135 return { queued }136 })137}138139// ------- mise en file pour les autres modules (mises en veille, rappels, alertes d'exploitation)140export type UserPush = { kind: "snooze" | "reminder" | "alert" | "test"; title: string; body: string; mailboxId?: string; emailId?: string; threadId?: string; dedup: string; category?: string; timeSensitive?: boolean }141type LiveDevice = { id: string; user_id: string; locale: string | null; prefs: DevicePrefs }142/** Appareils actifs dont la session est encore valide (une session révoquée ou expirée ne reçoit plus rien). */143async function liveDevices(q: Sql, userIds: string[]): Promise<LiveDevice[]> {144 if (!userIds.length) return []145 return (await q`select d.id, d.user_id, d.locale, d.prefs from push_devices d146 join sessions s on s.id = d.session_id and s.revoked_at is null and s.expires_at > now()147 join users u on u.id = d.user_id and u.status = 'active'148 where d.user_id = any(${userIds}::uuid[]) and d.enabled`) as unknown as LiveDevice[]149}150async function insertRow(q: Sql, d: LiveDevice, item: { kind: PushKind; mailboxId?: string | null; emailId?: string | null; threadId?: string | null; notice: Notice; dedup: string }): Promise<number> {151 const r = await q`insert into push_outbox (device_id, user_id, mailbox_id, email_id, thread_id, kind, payload, dedup_key)152 values (${d.id}, ${d.user_id}, ${item.mailboxId ?? null}, ${item.emailId ?? null}, ${item.threadId ?? null}, ${item.kind}, ${q.json(item.notice as never)}, ${item.dedup})153 on conflict (dedup_key) do nothing returning 1`154 return r.length155}156async function enqueueFor(q: Sql, userId: string, n: UserPush, deviceId?: string): Promise<number> {157 if (!pushConfigured()) return 0158 const prefs = await loadPrefs(q, userId)159 if (!prefs.enabled) return 0160 let queued = 0161 for (const d of await liveDevices(q, [userId])) {162 if (deviceId && d.id !== deviceId) continue163 if (n.mailboxId && (prefs.mailboxes[n.mailboxId] === false || d.prefs?.mailboxes?.[n.mailboxId] === false)) continue164 // Le texte suit la préférence d'aperçu : « none » ne dit rien du message, « sender » ne garde que le titre.165 const mail = n.kind === "snooze" || n.kind === "reminder"166 const generic = genericText(n.kind, d.locale)167 const notice: Notice = mail && prefs.preview === "none" ? { title: "SP Mail", body: generic }168 : mail && prefs.preview === "sender" ? { title: n.title || "SP Mail", body: generic }169 : { title: n.title || "SP Mail", body: n.body || generic }170 queued += await insertRow(q, d, {171 kind: n.kind, mailboxId: n.mailboxId, emailId: n.emailId, threadId: n.threadId, dedup: `${n.kind}:${d.id}:${n.dedup}`,172 notice: { ...notice, ...(n.category ? { category: n.category } : {}), ...(n.timeSensitive ? { timeSensitive: true } : {}), ...(d.prefs?.sound === false ? { sound: false } : {}) },173 })174 }175 return queued176}177/**178 * Met en file une notification pour chaque appareil actif de l'utilisateur ; rend le nombre de lignes créées (0 si le push179 * n'est pas configuré, si l'utilisateur l'a coupé, ou si `dedup` a déjà servi). `q` peut être une transaction.180 */181export async function enqueueUserPush(q: Sql, userId: string, n: UserPush): Promise<number> {182 return enqueueFor(q, userId, n)183}184185// ------- crochet du veilleur : écritures en base seulement, dans sa transaction186const MAX_AGE_MS = 24 * 3600_000 // un message ancien (import, veilleur resté arrêté) ne déclenche plus d'alerte187let contactsReady = false188/** Utilisateurs pour qui l'expéditeur est un contact « vip » (tables du module contacts, si elles existent). */189async function vipFor(tx: Sql, userIds: string[], email: string): Promise<Set<string>> {190 const none = new Set<string>()191 if (!email || !userIds.length) return none192 try {193 if (!contactsReady) {194 const [r] = await tx`select (to_regclass('contacts') is not null and to_regclass('contact_emails') is not null) as ok`195 if (!r?.ok) return none196 contactsReady = true197 }198 // Point de sauvegarde : une erreur ici (schéma différent) ne doit pas condamner la transaction du veilleur.199 type Vip = { user_id: string }[]200 const sp = (tx as unknown as { savepoint?: (cb: (s: Sql) => Promise<Vip>) => Promise<Vip> }).savepoint201 if (!sp) return none202 const rows = await sp.call(tx, (s) => s`select distinct c.user_id from contacts c join contact_emails e on e.contact_id = c.id203 where c.user_id = any(${userIds}::uuid[]) and c.vip and lower(e.email) = ${email}` as unknown as Promise<Vip>)204 return new Set(rows.map((r) => r.user_id))205 } catch {206 return none207 }208}209210async function inboundPush(tx: Sql, batch: InboundBatch) {211 if (!pushConfigured()) return212 const mb = batch.mb213 const now = Date.now()214 const fresh = batch.received.filter((r) => r.roles.includes("inbox") && !r.email.keywords?.$seen && !r.email.keywords?.$draft215 && now - new Date(r.email.receivedAt ?? now).getTime() < MAX_AGE_MS)216 // La pastille peut avoir bougé : message lu / non lu, sorti de la boîte de réception, supprimé. Les faux positifs sont217 // sans effet : le travailleur n'émet pas une pastille identique à la dernière envoyée.218 let badgeMoved = false219 const touched = [...batch.updated.map((u) => u.email.id), ...batch.destroyed]220 if (touched.length) {221 const known = new Map((await tx`select email_id, seen from watched_messages where mailbox_id = ${mb.id} and email_id = any(${touched})`).map((w) => [w.email_id as string, w.seen as boolean]))222 badgeMoved = batch.destroyed.some((id) => known.get(id) !== true) || batch.updated.some((u) => {223 const seen = !!u.email.keywords?.$seen, was = known.get(u.email.id)224 return !(was !== undefined && was === seen && (seen || u.roles.includes("inbox")))225 })226 }227 if (!fresh.length && !badgeMoved) return228229 const readers = (await tx`select ${mb.user_id}::uuid as user_id union select user_id from mailbox_grants where mailbox_id = ${mb.id} and can_read`).map((r) => r.user_id as string)230 const devices = await liveDevices(tx, readers)231 if (!devices.length) return232 const prefs = new Map((await tx`select * from push_prefs where user_id = any(${readers}::uuid[])`).map((r) => [r.user_id as string, prefsOfRow(r)]))233 const alerted = new Set<string>()234 for (const r of fresh) {235 const e = r.email236 const from = (e.from ?? [])[0] as { name?: string | null; email?: string | null } | undefined237 const fromEmail = (from?.email ?? "").toLowerCase()238 const vip = await vipFor(tx, readers, fromEmail)239 for (const d of devices) {240 const p = prefs.get(d.user_id) ?? DEFAULT_PREFS241 if (!p.enabled || p.mailboxes[mb.id] === false || d.prefs?.mailboxes?.[mb.id] === false) continue242 const priority = isPrioritySender(p.priority_senders, fromEmail) || vip.has(d.user_id)243 // Heures calmes ou « prioritaires seulement » : la ligne existe quand même, en mise à jour silencieuse de la pastille.244 const silent = !priority && (p.priority_only || inQuietHours(p.quiet_hours, new Date(now)))245 const notice: Notice = silent ? { silent: true }246 : { ...previewText(p.preview, { fromName: from?.name, fromEmail: from?.email, subject: e.subject, preview: e.preview }, d.locale), ...(priority ? { timeSensitive: true } : {}), ...(d.prefs?.sound === false ? { sound: false } : {}) }247 if (silent && p.badge === "none") continue248 await insertRow(tx, d, { kind: "new", mailboxId: mb.id, emailId: e.id, threadId: e.threadId ?? null, notice, dedup: `new:${d.id}:${mb.id}:${e.id}` })249 alerted.add(d.id)250 }251 }252 if (!badgeMoved) return253 for (const d of devices) {254 const p = prefs.get(d.user_id) ?? DEFAULT_PREFS255 if (alerted.has(d.id) || !p.enabled || p.badge === "none" || p.mailboxes[mb.id] === false) continue256 // Une seule ligne par appareil, réarmée à chaque rafale : dix messages lus d'un coup = une mise à jour silencieuse.257 await tx`insert into push_outbox as o (device_id, user_id, kind, payload, dedup_key, next_attempt_at)258 values (${d.id}, ${d.user_id}, 'badge', '{}', ${`badge:${d.id}`}, now() + make_interval(secs => ${BADGE_DEBOUNCE_S}))259 on conflict (dedup_key) do update set user_id = excluded.user_id,260 next_attempt_at = case when o.status = 'pending' and o.locked_until is null then o.next_attempt_at else excluded.next_attempt_at end,261 attempts = case when o.status = 'pending' and o.locked_until is null then o.attempts else 0 end,262 created_at = case when o.status = 'pending' then o.created_at else now() end,263 status = 'pending', locked_until = null, sent_at = null`264 }265}266const BADGE_DEBOUNCE_S = 2267onInbound("push", inboundPush)268269// ------- travailleur d'envoi270type OutboxRow = { id: string; device_id: string; user_id: string; mailbox_id: string | null; email_id: string | null; thread_id: string | null; kind: PushKind; payload: Notice; dedup_key: string; attempts: number; expired: boolean }271const MAX_ATTEMPTS = 6272273/** Pastille : total des non lus des boîtes de réception lisibles, hors boîtes coupées par l'utilisateur. Nul si indisponible. */274async function badgeOf(userId: string, muted: Record<string, boolean>): Promise<number | null> {275 try {276 let n = 0277 for (const mb of await userMailboxes(userId)) {278 if (muted[mb.id] === false) continue279 n += (await folders(mb, true)).find((f) => f.role === "inbox")?.unreadEmails ?? 0280 }281 return n282 } catch {283 return null284 }285}286287async function deliverOne(row: OutboxRow, badges: Map<string, Promise<number | null>>) {288 // Chaque écriture finale est gardée par `attempts` : si le crochet a réarmé la ligne pendant l'envoi, elle repartira.289 const settle = (status: "sent" | "failed" | "dropped", r: { status?: number; reason?: string; apnsId?: string | null } = {}) =>290 sql`update push_outbox set status = ${status}, locked_until = null, last_status = ${r.status ?? null}, last_error = ${status === "sent" ? null : (r.reason ?? null)}, apns_id = ${r.apnsId ?? null},291 sent_at = ${status === "sent" ? sql`now()` : null} where id = ${row.id} and attempts = ${row.attempts}`292 const [d] = await sql`select d.id, d.user_id, d.apns_token, d.environment, d.bundle_id, d.enabled, d.last_badge,293 (s.revoked_at is null and s.expires_at > now() and u.status = 'active') as live, coalesce(p.enabled, true) as user_enabled, coalesce(p.badge, 'inbox') as badge_pref, coalesce(p.mailboxes, '{}'::jsonb) as muted294 from push_devices d join sessions s on s.id = d.session_id join users u on u.id = d.user_id left join push_prefs p on p.user_id = d.user_id where d.id = ${row.device_id}`295 if (!d) return296 // Session révoquée ou expirée (y compris par la ligne de commande) : l'appareil ne doit plus rien recevoir.297 if (!d.live) { await sql`delete from push_devices where id = ${d.id}`; return }298 if (d.user_id !== row.user_id) return void (await settle("dropped", { reason: "device now belongs to another user" }))299 if (!d.enabled || !d.user_enabled) return void (await settle("dropped", { reason: "notifications disabled" }))300 if (row.expired) return void (await settle("failed", { reason: "expired after 24 hours" }))301302 let badge: number | null = null303 if (d.badge_pref !== "none") {304 if (!badges.has(d.user_id)) badges.set(d.user_id, badgeOf(d.user_id, d.muted as Record<string, boolean>))305 badge = await badges.get(d.user_id)!306 }307 const item: PushItem = { kind: row.kind, mailboxId: row.mailbox_id, emailId: row.email_id, threadId: row.thread_id, notice: row.payload ?? {}, dedupKey: row.dedup_key }308 const retry = (reason: string, status = 0) => row.attempts >= MAX_ATTEMPTS309 ? settle("failed", { status, reason: `gave up after ${row.attempts} attempts: ${reason}` })310 : sql`update push_outbox set locked_until = null, last_status = ${status}, last_error = ${reason},311 next_attempt_at = now() + make_interval(secs => ${config.pushBackoff[Math.min(row.attempts - 1, config.pushBackoff.length - 1)] ?? 60}) where id = ${row.id} and attempts = ${row.attempts}`312 if (row.kind === "badge") {313 if (d.badge_pref === "none") return void (await settle("dropped", { reason: "badge disabled" }))314 if (badge === null) return void (await retry("unread count unavailable"))315 if (badge === d.last_badge) return void (await settle("dropped", { reason: "badge unchanged" }))316 }317 const res = await sendApns({ apns_token: d.apns_token, environment: d.environment, bundle_id: d.bundle_id }, buildHeaders(item, d.bundle_id), buildPayload(item, badge))318 const label = `${res.status} ${res.reason}`.trim()319 if (res.outcome === "ok") {320 await settle("sent", res)321 await sql`update push_devices set last_push_at = now(), last_error = null, failure_count = 0, last_badge = coalesce(${badge}, last_badge) where id = ${d.id}`322 } else if (res.outcome === "drop-token") {323 // Jeton mort (410, BadDeviceToken…) : l'appareil est retiré, ses lignes en attente partent avec lui.324 await sql`delete from push_devices where id = ${d.id}`325 } else {326 if (res.outcome === "retry") await retry(label, res.status)327 else await settle("failed", { status: res.status, reason: label })328 await sql`update push_devices set last_error = ${label}, failure_count = failure_count + 1 where id = ${d.id}`329 }330}331332let lastPrune = 0333async function deliverPush() {334 if (!pushConfigured()) return335 const rows = (await sql`update push_outbox set locked_until = now() + interval '1 minute', attempts = attempts + 1336 where id in (select id from push_outbox where status = 'pending' and next_attempt_at <= now() and (locked_until is null or locked_until < now())337 order by next_attempt_at limit 25 for update skip locked)338 returning id, device_id, user_id, mailbox_id, email_id, thread_id, kind, payload, dedup_key, attempts, (created_at < now() - interval '24 hours') as expired`) as unknown as OutboxRow[]339 const badges = new Map<string, Promise<number | null>>()340 // En cas d'erreur inattendue, le bail (1 min) fait office de délai avant la reprise.341 await Promise.all(rows.map((r) => deliverOne(r, badges).catch(() => {})))342 if (Date.now() - lastPrune > 3600_000) {343 lastPrune = Date.now()344 await sql`delete from push_outbox where status <> 'pending' and created_at < now() - interval '7 days'`345 }346}347348export function pushWorkers(): NextWorker[] {349 return [{ name: "push dispatcher", intervalMs: config.workerTickMs, run: deliverPush }]350}351