diff --git a/app/main.py b/app/main.py index 8c7c8dd..2d90feb 100644 --- a/app/main.py +++ b/app/main.py @@ -196,6 +196,48 @@ def get_mails_periode(jours: int = 7): }) return {"debut": debut, "fin": fin.isoformat(), "count": len(resultats), "mails": resultats} +# --- Lecture d'un mail (texte seul, lecture seule) --- +MAX_CORPS_LECTURE = 200_000 + + +def _entete(en_tetes: Dict[str, Any], nom: str) -> str: + for k, v in (en_tetes or {}).items(): + if str(k).lower() == nom.lower() and v: + return str(v) + return "" + + +@api.get("/mail") +def lire_mail(id: str = Query(..., min_length=1, max_length=400)): + """Contenu d'un mail du miroir : en-têtes utiles, texte et liste des pièces jointes. Aucune écriture, + aucun contenu de mail dans les journaux. Le miroir ne fournit pas de lien vers Outlook.""" + try: + res = mirror_client.get_mail(id) + except Exception: + raise HTTPException(status_code=502, detail="Lecture du mail impossible : miroir indisponible.") + d = res.get("donnees") if isinstance(res, dict) else None + if not isinstance(d, dict) or not d: + raise HTTPException(status_code=404, detail="Mail introuvable.") + eh = d.get("en_tetes") or {} + corps = str(d.get("corps_texte") or "") + pj = [] + for p in d.get("pieces_jointes") or []: + if isinstance(p, dict): + pj.append({ + "nom": str(p.get("filename") or p.get("nom") or "(sans nom)"), + "type": str(p.get("content_type") or p.get("type") or ""), + "taille": int(p.get("size") or p.get("taille") or 0), + }) + return { + "message_id": d.get("message_id") or id, + "dossier": d.get("dossier"), + "de": _entete(eh, "From"), "a": _entete(eh, "To"), "cc": _entete(eh, "Cc"), + "date": _entete(eh, "Date"), "objet": _entete(eh, "Subject"), + "corps": corps[:MAX_CORPS_LECTURE], "tronque": len(corps) > MAX_CORPS_LECTURE, + "pieces_jointes": pj, "taille_octets": d.get("taille_octets"), + } + + # --- Classements --- @api.get("/classements") def get_classements( diff --git a/app/static/app.css b/app/static/app.css index 5667701..6024f08 100644 --- a/app/static/app.css +++ b/app/static/app.css @@ -215,3 +215,52 @@ h1, h2, h3, p { margin: 0; } .sq-ligne { grid-template-columns: 44px minmax(0, 1fr) 138px; } .sq-ligne span:nth-child(3), .sq-ligne span:nth-child(4), .sq-ligne span:nth-child(5) { display: none; } } + +/* ---------- Lecture d'un mail ---------- */ +.lire { display: block; width: 100%; min-height: var(--ny-cible); padding: 0; border: none; border-radius: var(--ny-r-s); background: transparent; color: inherit; font: inherit; text-align: left; cursor: pointer; } +.lire > span { display: block; min-width: 0; } +.lire:hover .objet { color: var(--ny-accent); text-decoration: underline; text-underline-offset: 3px; } +.voile:has(> .boite.lecture) { padding: var(--ny-e6); } +.boite.lecture { max-width: 780px; max-height: 100%; padding: 0; gap: 0; overflow: hidden; display: flex; flex-direction: column; } +.lect-tete { display: flex; align-items: flex-start; gap: var(--ny-e3); padding: var(--ny-e5) var(--ny-e6) var(--ny-e4); border-bottom: 1px solid var(--ny-trait); background: var(--ny-carte); } +.lect-tete h2 { flex: 1; min-width: 0; margin: 0; font: 600 22px/30px var(--ny-police); color: var(--ny-encre); overflow-wrap: anywhere; } +.lect-tete .btn-icone { flex: none; } +.lect-scroll { flex: 1; min-height: 0; overflow-y: auto; padding: var(--ny-e5) var(--ny-e6) var(--ny-e8); } +.lect-badges { display: flex; flex-wrap: wrap; align-items: center; gap: var(--ny-e2); margin-bottom: var(--ny-e4); } +.lect-cat { display: inline-flex; align-items: center; gap: 6px; min-height: 28px; padding: 0 10px; border-radius: var(--ny-r-pastille); border: 1px solid var(--ny-trait); background: var(--ny-carte-2); color: var(--ny-encre); font: 600 13px/18px var(--ny-police); } +.lect-meta { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 6px var(--ny-e4); margin: 0 0 var(--ny-e5); padding-bottom: var(--ny-e5); border-bottom: 1px solid var(--ny-trait); font: 400 15px/22px var(--ny-police); } +.lect-meta dt { color: var(--ny-encre-3); font-weight: 500; } +.lect-meta dd { margin: 0; min-width: 0; overflow-wrap: anywhere; color: var(--ny-encre); } +.lect-meta .qui { display: block; } +.lect-meta .nom { font-weight: 600; } +.lect-meta .adr { color: var(--ny-encre-3); } +.lect-meta .btn-petit.plus { min-height: 32px; padding: 0 var(--ny-e2); margin-left: calc(var(--ny-e2) * -1); } +.lect-texte { max-width: 60ch; font: 400 17px/1.65 var(--ny-police); color: var(--ny-encre); } +.lect-texte p { margin: 0 0 1em; overflow-wrap: anywhere; } +.lect-texte blockquote { margin: 0 0 1em; padding: 2px 0 2px var(--ny-e4); border-left: 3px solid var(--ny-trait); color: var(--ny-encre-2); overflow-wrap: anywhere; } +.lect-texte a { text-decoration: underline; text-underline-offset: 3px; overflow-wrap: anywhere; } +.lect-vide { color: var(--ny-encre-3); font-style: italic; } +.lect-histo { margin-top: var(--ny-e4); padding-top: var(--ny-e2); border-top: 1px solid var(--ny-trait); } +.lect-histo summary { display: flex; align-items: center; min-height: var(--ny-cible); color: var(--ny-accent); font: 600 15px/20px var(--ny-police); cursor: pointer; } +.lect-histo .lect-texte { font-size: 15px; color: var(--ny-encre-2); padding-top: var(--ny-e2); } +.lect-pj { margin-top: var(--ny-e6); padding-top: var(--ny-e4); border-top: 1px solid var(--ny-trait); } +.lect-pj h3 { margin: 0 0 var(--ny-e2); font: 600 15px/20px var(--ny-police); color: var(--ny-encre); } +.lect-pj ul { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--ny-e2); } +.lect-pj li { display: flex; align-items: center; gap: var(--ny-e3); min-height: var(--ny-cible); padding: 6px var(--ny-e3); border: 1px solid var(--ny-trait); border-radius: var(--ny-r-m); background: var(--ny-carte-2); color: var(--ny-encre-2); } +.lect-pj .nom { flex: 1; min-width: 0; font: 500 15px/20px var(--ny-police); color: var(--ny-encre); } +.lect-pj .info { flex: none; font: 400 13px/18px var(--ny-police); color: var(--ny-encre-3); } +.lect-note { margin: var(--ny-e5) 0 0; font: 400 13px/18px var(--ny-police); color: var(--ny-encre-3); } +.lect-etat { display: flex; flex-direction: column; gap: var(--ny-e3); } +.lect-etat p { margin: 0; font: 500 15px/22px var(--ny-police); color: var(--ny-encre-2); } +.lect-sq { display: block; height: 14px; border-radius: var(--ny-r-s); background: var(--ny-trait); } +.lect-sq.court { width: 60%; } +@media (max-width: 760px) { + .voile:has(> .boite.lecture) { padding: 0; } + .boite.lecture { max-width: none; height: 100%; max-height: none; border: none; border-radius: 0; box-shadow: none; } + .lect-tete { padding: var(--ny-e3) var(--ny-e4); } + .lect-tete h2 { font: 600 19px/26px var(--ny-police); padding-top: 9px; } + .lect-scroll { padding: var(--ny-e4) var(--ny-e4) var(--ny-e8); } + .lect-texte { font-size: 17px; } + .lect-meta { grid-template-columns: minmax(0, 1fr); gap: 0; } + .lect-meta dt { margin-top: var(--ny-e2); font-size: 13px; line-height: 18px; } +} diff --git a/app/static/app.js b/app/static/app.js index a342cc5..93ccd36 100644 --- a/app/static/app.js +++ b/app/static/app.js @@ -14,7 +14,7 @@ suivi: 'M12 6v6l4 2|M22 12a10 10 0 1 1-20 0 10 10 0 0 1 20 0', pj: 'm21.44 11.05-9.19 9.19a6 6 0 0 1-8.49-8.49l8.57-8.57A4 4 0 1 1 18 8.84l-8.59 8.57a2 2 0 0 1-2.83-2.83l8.49-8.48', groupes: 'M3 7h7v7H3z|M14 7h7v7h-7z|M3 17h18', - gauche: 'm15 18-6-6 6-6', droite: 'm9 18 6-6-6-6', bas: 'm6 9 6 6 6-6', fleche: 'M5 12h14|m12 5 7 7-7 7' + fermer: 'M18 6 6 18|m6 6 12 12', gauche: 'm15 18-6-6 6-6', droite: 'm9 18 6-6-6-6', bas: 'm6 9 6 6 6-6', fleche: 'M5 12h14|m12 5 7 7-7 7' }; var NAV = [ ['tri', 'Tri du jour', true], ['categories', 'Catégories', true], ['groupes', 'Groupes', true], ['regles', 'Règles', true], @@ -206,7 +206,7 @@ voile.addEventListener('keydown', function (e) { if (e.key === 'Escape') { e.preventDefault(); fermer(); return; } if (e.key === 'Tab') { - var f = boite.querySelectorAll('button,input,select,[href]'); if (!f.length) return; + var f = boite.querySelectorAll('button:not([hidden]),input,select,[href],summary,[tabindex="0"]'); if (!f.length) return; var a = f[0], z = f[f.length - 1]; if (e.shiftKey && document.activeElement === a) { e.preventDefault(); z.focus(); } else if (!e.shiftKey && document.activeElement === z) { e.preventDefault(); a.focus(); } @@ -310,6 +310,132 @@ noms.forEach(function (n) { var o = h('option', { value: n }, n); if (n === m.cat) o.selected = true; sel.appendChild(o); }); return h('div', { class: 'sel' }, pastille(couleur(m.cat, c && c.id)), sel, h('span', { class: 'chev' }, icone('bas'))); } + /* ---------- Lecture d'un mail (texte seul, lecture seule) ---------- */ + var RUPT_FORT = /^\s*(?:-{3,}\s*(?:Original Message|Message d.origine)|Le\s.{5,140}\sa\s+écrit\s?:|On\s.{5,140}\swrote:)/i; + var RUPT_ENTETE = /^\s*(?:De|From)\s?:/i; + var SUITE_ENTETE = /^\s*(?:Envoy[ée]|Sent|Date|Objet|Subject|À|To)\s?:/i; + var URL_RE = /(https?:\/\/[^\s<>"')\]]*[^\s<>"')\].,;:!?])/g; + function debutHistorique(l) { + for (var i = 1; i < l.length; i++) { + if (RUPT_FORT.test(l[i])) return i; + if (RUPT_ENTETE.test(l[i])) { for (var k = i + 1; k <= i + 4 && k < l.length; k++) if (SUITE_ENTETE.test(l[k])) return i; } + } + return -1; + } + function avecLiens(el, ligne) { + ligne.split(URL_RE).forEach(function (t, i) { + if (!t) return; + if (i % 2 === 1) el.appendChild(h('a', { href: t, target: '_blank', rel: 'noopener noreferrer nofollow', text: t })); + else el.appendChild(document.createTextNode(t)); + }); + } + function blocsTexte(texte) { + var sortie = h('div', { class: 'lect-texte' }), para = [], cit = false; + function vider() { + if (!para.length) return; + var el = h(cit ? 'blockquote' : 'p'); + para.forEach(function (l, i) { if (i) el.appendChild(h('br')); avecLiens(el, l); }); + sortie.appendChild(el); para = []; + } + texte.split('\n').forEach(function (l) { + if (!l.trim()) { vider(); return; } + var estCit = /^\s*>/.test(l); + if (estCit !== cit) vider(); + cit = estCit; + para.push(estCit ? l.replace(/^(\s*>)+\s?/, '') : l); + }); + vider(); + return sortie; + } + function decouperAdresses(s) { return String(s || '').split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/).map(function (x) { return x.trim(); }).filter(Boolean); } + function pastilleAdresse(a) { + var m = a.match(/^\s*"?([^"<]*?)"?\s*<([^>]+)>\s*$/), nom = m ? m[1].trim() : '', adr = m ? m[2].trim() : a.trim(); + return h('span', { class: 'qui' }, nom ? h('span', { class: 'nom', text: nom }) : null, nom ? ' ' : null, h('span', { class: nom ? 'adr' : 'nom', text: nom ? '<' + adr + '>' : adr })); + } + function listeAdresses(s) { + var l = decouperAdresses(s); + if (!l.length) return null; + var dd = h('dd', { class: 'adrs' }); + l.forEach(function (a, i) { var e = pastilleAdresse(a); if (i >= 3) e.setAttribute('hidden', ''); dd.appendChild(e); }); + if (l.length > 3) { + var b = h('button', { type: 'button', class: 'btn-petit plus' }, '+ ' + (l.length - 3) + (l.length - 3 > 1 ? ' autres' : ' autre')); + b.addEventListener('click', function () { + dd.querySelectorAll('[hidden]').forEach(function (x) { x.removeAttribute('hidden'); }); b.remove(); + var z = document.querySelector('.lect-scroll'); if (z) z.focus(); + }); + dd.appendChild(b); + } + return dd; + } + function tailleLisible(n) { + n = Number(n) || 0; + if (n < 1024) return n + ' o'; + if (n < 1048576) return (n / 1024).toFixed(n < 10240 ? 1 : 0).replace('.', ',') + ' Ko'; + return (n / 1048576).toFixed(1).replace('.', ',') + ' Mo'; + } + function dateLongue(v) { + var iso = String(v || '').slice(0, 10); + if (!/^\d{4}-\d{2}-\d{2}$/.test(iso)) return String(v || ''); + return nomJour(iso).toLowerCase() + ' ' + fr(iso) + ' à ' + heureTunis(v); + } + function afficherMail(cont, m, r, c) { + var badges = h('div', { class: 'lect-badges' }, h('span', { class: 'lect-cat' }, pastille(couleur(m.cat, c && c.id)), m.cat)); + (m.etiquettes || []).forEach(function (t) { badges.appendChild(h('span', { class: 'etq', text: t })); }); + var meta = h('dl', { class: 'lect-meta' }); + function ligne(lib, dd) { if (!dd) return; meta.appendChild(h('dt', { text: lib })); meta.appendChild(dd); } + ligne('De', listeAdresses(r.de || m.expBrut)); ligne('À', listeAdresses(r.a)); ligne('Cc', listeAdresses(r.cc)); + ligne('Date', h('dd', { text: dateLongue(m.date || r.date) })); + var lignes = String(r.corps || '').replace(/\r\n?/g, '\n').replace(/ /g, ' ').split('\n'); + var cut = debutHistorique(lignes), principal = lignes, histo = null; + if (cut > 0) { + principal = lignes.slice(0, cut); histo = lignes.slice(cut); + while (principal.length && /^[\s_\-=]*$/.test(principal[principal.length - 1])) principal.pop(); + } + var corps = principal.join('\n').trim() ? blocsTexte(principal.join('\n')) : h('p', { class: 'lect-vide', text: 'Ce mail ne contient pas de texte à afficher.' }); + cont.appendChild(badges); cont.appendChild(meta); cont.appendChild(corps); + if (histo && histo.join('\n').trim()) { + cont.appendChild(h('details', { class: 'lect-histo' }, h('summary', null, 'Afficher l\'historique de la conversation'), blocsTexte(histo.join('\n')))); + } + if (r.tronque) cont.appendChild(h('p', { class: 'lect-note', text: 'Le texte est très long : seul le début est affiché.' })); + var pj = r.pieces_jointes || []; + if (pj.length) { + var ul = h('ul'); + pj.forEach(function (p) { + var ext = String(p.nom || '').indexOf('.') > -1 ? String(p.nom).split('.').pop().toUpperCase() : ''; + ul.appendChild(h('li', null, icone('pj', 18), h('span', { class: 'nom coupe', title: p.nom, text: p.nom }), + h('span', { class: 'info', text: (ext && ext.length <= 5 ? ext + ' · ' : '') + tailleLisible(p.taille) }))); + }); + cont.appendChild(h('section', { class: 'lect-pj', 'aria-labelledby': 'lect-pj-t' }, h('h3', { id: 'lect-pj-t', text: 'Pièces jointes (' + pj.length + ')' }), ul)); + } + cont.appendChild(h('p', { class: 'lect-note', text: 'Lecture seule : le mail n\'est pas modifié dans votre messagerie.' })); + } + function lireMail(m) { + var c = S.cats.find(function (x) { return x.nom === m.cat; }); + ouvrir(function (clore) { + var defil = h('div', { class: 'lect-scroll', tabindex: '0', 'data-focus': '', role: 'region', 'aria-label': 'Contenu du mail' }); + var boite = h('section', { class: 'boite lecture', role: 'dialog', 'aria-modal': 'true', 'aria-labelledby': 'lect-t' }, + h('div', { class: 'lect-tete' }, h('h2', { id: 'lect-t', text: m.objet || '(sans objet)' }), + h('button', { type: 'button', class: 'btn-icone', 'aria-label': 'Fermer la lecture', onclick: clore }, icone('fermer'))), + defil); + function charger() { + defil.textContent = ''; defil.setAttribute('aria-busy', 'true'); defil.focus(); + defil.appendChild(h('div', { class: 'lect-etat', role: 'status' }, h('p', { text: 'Ouverture du mail…' }), h('span', { class: 'lect-sq' }), h('span', { class: 'lect-sq court' }), h('span', { class: 'lect-sq' }))); + api('/mail?id=' + encodeURIComponent(m.id)).then(function (r) { + if (!document.body.contains(boite)) return; + defil.removeAttribute('aria-busy'); defil.textContent = ''; + afficherMail(defil, m, r, c); + }).catch(function (e) { + if (e.message === 'session' || !document.body.contains(boite)) return; + defil.removeAttribute('aria-busy'); defil.textContent = ''; + defil.appendChild(h('div', { class: 'erreur', role: 'alert' }, h('h2', null, 'Lecture du mail impossible'), + h('p', null, 'Le service de lecture n\'a pas répondu. Le mail reste intact dans votre messagerie.'), + h('button', { type: 'button', class: 'btn-plein', onclick: charger }, 'Réessayer'))); + }); + } + charger(); + return boite; + }); + } function reperes(m) { if (!m.etiquettes || !m.etiquettes.length) return null; var d = h('div', { class: 'etqs', 'aria-label': 'Étiquettes du miroir' }); @@ -321,17 +447,19 @@ var origCls = orig === 'manuel' ? ' manuel' : orig === 'sans règle' ? ' sansregle' : ''; var objetAff = m.objet || '(sans objet)'; var etq = 'Traité : ' + m.exp + ', ' + (m.objet || 'sans objet'); + var libLire = 'Lire le mail de ' + m.exp + ' : ' + (m.objet || 'sans objet'); var cb = h('input', { type: 'checkbox', 'aria-label': etq, onchange: function (e) { basculerTraite(m, e.target.checked); } }); cb.checked = m.traite; return h('div', { class: 'ligne' + (m.traite ? ' fait' : ''), role: 'listitem' }, h('label', { class: 'coche' }, cb), h('div', { class: 'corps d' }, h('div', { class: 'exp coupe', title: m.expBrut, text: m.exp })), - h('div', { class: 'corps d' }, h('div', { class: 'objet coupe' + (m.objet ? '' : ' sans'), text: objetAff }), reperes(m)), + h('div', { class: 'corps d' }, h('button', { type: 'button', class: 'lire', 'aria-label': libLire, onclick: function () { lireMail(m); } }, h('span', { class: 'objet coupe' + (m.objet ? '' : ' sans'), text: objetAff })), reperes(m)), h('div', { class: 'heure d', text: heureTunis(m.date) }), h('div', { class: 'd' }, h('span', { class: 'origine' + origCls, text: orig })), h('div', { class: 'm-corps' }, - h('div', { class: 'exp coupe', title: m.expBrut, text: m.exp }), h('div', { class: 'objet coupe' + (m.objet ? '' : ' sans'), text: objetAff }), - h('div', { class: 'm-meta' }, h('span', { class: 'heure', text: heureTunis(m.date) }), ' · ', h('span', { class: 'o' + (orig === 'manuel' ? ' manuel' : ''), text: orig })), reperes(m)), + h('button', { type: 'button', class: 'lire', 'aria-label': libLire, onclick: function () { lireMail(m); } }, + h('span', { class: 'exp coupe', title: m.expBrut, text: m.exp }), h('span', { class: 'objet coupe' + (m.objet ? '' : ' sans'), text: objetAff }), + h('span', { class: 'm-meta' }, h('span', { class: 'heure', text: heureTunis(m.date) }), ' · ', h('span', { class: 'o' + (orig === 'manuel' ? ' manuel' : ''), text: orig }))), reperes(m)), selecteur(m)); } function vueTri() { diff --git a/app/static/index.html b/app/static/index.html index fbd40e2..c94540b 100644 --- a/app/static/index.html +++ b/app/static/index.html @@ -10,7 +10,7 @@ - +