Repository Public
1// Recherche : « from:alice subject:"rapport final" has:attachment after:2026-01-01 mots libres » ⇄ filtre JMAP.23export interface SearchQuery {4 text: string5 from: string6 to: string7 subject: string8 hasAttachment: boolean9 /** AAAA-MM-JJ, bornes incluses */10 after: string11 before: string12}1314export const emptySearch = (): SearchQuery => ({ text: "", from: "", to: "", subject: "", hasAttachment: false, after: "", before: "" })1516export function isEmptySearch(q: SearchQuery): boolean {17 return !q.text.trim() && !q.from.trim() && !q.to.trim() && !q.subject.trim() && !q.hasAttachment && !q.after && !q.before18}1920const KEYS: Record<string, keyof SearchQuery> = { from: "from", de: "from", to: "to", a: "to", "à": "to", subject: "subject", sujet: "subject", objet: "subject", after: "after", apres: "after", "après": "after", since: "after", depuis: "after", before: "before", avant: "before" }21const DATE_RE = /^\d{4}-\d{2}-\d{2}$/2223export function parseSearch(input: string): SearchQuery {24 const q = emptySearch()25 const free: string[] = []26 const re = /([\p{L}]+):(?:"([^"]*)"|(\S+))|"([^"]*)"|(\S+)/gu27 for (const m of input.matchAll(re)) {28 if (m[1] !== undefined) {29 const key = m[1].toLowerCase()30 const value = (m[2] ?? m[3] ?? "").trim()31 if ((key === "has" || key === "avec") && /^(attachment|attachments|piece|pièce|pj|fichier)$/i.test(value)) q.hasAttachment = true32 else if (KEYS[key]) {33 const k = KEYS[key]34 if (k === "after" || k === "before") {35 if (DATE_RE.test(value)) q[k] = value36 else free.push(m[0])37 } else if (k !== "hasAttachment" && k !== "text") q[k] = q[k] ? `${q[k]} ${value}` : value38 } else free.push(m[0])39 } else if (m[4] !== undefined) free.push(`"${m[4]}"`)40 else free.push(m[5])41 }42 q.text = free.join(" ")43 return q44}4546const quoteIfNeeded = (v: string) => (/\s/.test(v) ? `"${v.replace(/"/g, "")}"` : v)4748export function stringifySearch(q: SearchQuery): string {49 const parts: string[] = []50 if (q.from.trim()) parts.push(`from:${quoteIfNeeded(q.from.trim())}`)51 if (q.to.trim()) parts.push(`to:${quoteIfNeeded(q.to.trim())}`)52 if (q.subject.trim()) parts.push(`subject:${quoteIfNeeded(q.subject.trim())}`)53 if (q.hasAttachment) parts.push("has:attachment")54 if (q.after) parts.push(`after:${q.after}`)55 if (q.before) parts.push(`before:${q.before}`)56 if (q.text.trim()) parts.push(q.text.trim())57 return parts.join(" ")58}5960export interface JmapFilterCondition {61 text?: string62 from?: string63 to?: string64 subject?: string65 hasAttachment?: boolean66 after?: string67 before?: string68}6970/** Conditions JMAP (à combiner en AND). Les dates sont interprétées dans le fuseau local ; `before` inclut la journée indiquée. */71export function searchToConditions(q: SearchQuery): JmapFilterCondition[] {72 const out: JmapFilterCondition[] = []73 if (q.text.trim()) out.push({ text: q.text.trim() })74 if (q.from.trim()) out.push({ from: q.from.trim() })75 if (q.to.trim()) out.push({ to: q.to.trim() })76 if (q.subject.trim()) out.push({ subject: q.subject.trim() })77 if (q.hasAttachment) out.push({ hasAttachment: true })78 if (DATE_RE.test(q.after)) out.push({ after: localDay(q.after, 0).toISOString().replace(/\.\d{3}Z$/, "Z") })79 if (DATE_RE.test(q.before)) out.push({ before: localDay(q.before, 1).toISOString().replace(/\.\d{3}Z$/, "Z") })80 return out81}8283function localDay(d: string, addDays: number): Date {84 const [y, m, day] = d.split("-").map(Number)85 return new Date(y, m - 1, day + addDays, 0, 0, 0, 0)86}87