Repository Public
1// SP Mail Next — socle commun des routes /api/next (session d'appareil : cookie web ou jeton natif ; jamais de clé d'API).2import type { FastifyRequest } from "fastify"3import { sql } from "../db.js"4import type { Mbx } from "../mailjmap.js"5import { requireUser, type Auth } from "../session.js"6import { bad, rateLimit, zUuid } from "../util.js"78export type OwnedMailbox = Mbx & { can_send: boolean; owner: boolean }910/** Session exigée + limite de débit par session pour le groupe de routes. */11export async function nextUser(req: FastifyRequest, group = "next", perMinute = 600): Promise<Auth> {12 const a = await requireUser(req)13 rateLimit(`${group}:${a.sessionId}`, perMinute, 60_000)14 return a15}1617/** Boîtes lisibles par l'utilisateur : les siennes et celles partagées en lecture (mailbox_grants), dans l'ordre d'affichage. */18export async function userMailboxes(userId: string): Promise<OwnedMailbox[]> {19 return (await sql`select m.id, m.user_id, m.address, m.display_name, m.stalwart_id, m.secret_enc,20 (m.user_id = ${userId} or coalesce(g.can_send, false)) as can_send, (m.user_id = ${userId}) as owner21 from mailboxes m left join mailbox_grants g on g.mailbox_id = m.id and g.user_id = ${userId}22 where m.user_id = ${userId} or g.can_read order by m.position, m.created_at`) as unknown as OwnedMailbox[]23}2425/** Une boîte de l'utilisateur ; celle d'autrui est indiscernable d'une boîte inexistante (404). */26export async function userMailbox(userId: string, mailboxId: string): Promise<OwnedMailbox> {27 if (!zUuid.safeParse(mailboxId).success) throw bad("not_found", 404)28 const mb = (await userMailboxes(userId)).find((m) => m.id === mailboxId)29 if (!mb) throw bad("not_found", 404)30 return mb31}32