diff --git a/app/main.py b/app/main.py index 224c5d1..74ee67a 100644 --- a/app/main.py +++ b/app/main.py @@ -167,6 +167,27 @@ def get_mails_du_jour(date: Optional[str] = None, n: int = 50): "mails": classified_results } +@api.get("/mails/periode") +def get_mails_periode(jours: int = 7): + """Mails des N derniers jours (1 à 30), classés, pour la vue par groupe. Lecture seule.""" + from datetime import date as _date, timedelta + jours = max(1, min(int(jours), 30)) + fin = _date.fromisoformat(get_tunis_now_iso()[:10]) + debut = (fin - timedelta(days=jours - 1)).isoformat() + try: + mails = mirror_client.get_mails_since(debut, max_items=min(150 * jours, 800)) + except Exception as e: + return {"debut": debut, "fin": fin.isoformat(), "count": 0, "mails": [], "miroir_erreur": str(e)} + resultats = [] + for m in mails: + if not m.get("message_id"): + continue + resultats.append({ + "message_id": m["message_id"], "date": m.get("date"), "expediteur": m.get("expediteur"), + "objet": m.get("objet"), "etiquettes": m.get("etiquettes", []), "classement": classify_mail(m), + }) + return {"debut": debut, "fin": fin.isoformat(), "count": len(resultats), "mails": resultats} + # --- Classements --- @api.get("/classements") def get_classements( diff --git a/app/mirror_client.py b/app/mirror_client.py index e3ed9d3..85e5e06 100644 --- a/app/mirror_client.py +++ b/app/mirror_client.py @@ -216,3 +216,20 @@ class MirrorClient: } filtered.append(m_normalized) return filtered + + def get_mails_since(self, debut: str, max_items: int = 400) -> List[Dict[str, Any]]: + """Mails dont la date est >= debut (AAAA-MM-JJ), normalisés comme get_mails_for_day.""" + out = [] + for m in self.get_latest_mails(folder="*", n=max_items): + mid = m.get("message_id") or m.get("id") or m.get("hash") + d_mail = m.get("date") or m.get("date_reception") or m.get("date_heure") or "" + if d_mail[:10] >= debut: + out.append({ + "message_id": mid, + "date": d_mail, + "expediteur": m.get("from") or m.get("expediteur") or "", + "objet": m.get("sujet") or m.get("subject") or m.get("objet") or "", + "etiquettes": m.get("tags") or m.get("etiquettes") or [], + "apercu": m.get("snippet") or m.get("apercu") or "" + }) + return out diff --git a/app/static/app.css b/app/static/app.css index 5e23751..5667701 100644 --- a/app/static/app.css +++ b/app/static/app.css @@ -65,6 +65,13 @@ h1, h2, h3, p { margin: 0; } .barre > div { height: 100%; background: var(--ny-accent); transition: width var(--ny-duree) var(--ny-courbe); } .avancement b { font: 600 13px/18px var(--ny-police-chiffres); color: var(--ny-encre); } +/* Étiquettes du miroir et vue par groupe */ +.etqs { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; } +.etq { display: inline-flex; align-items: center; min-height: 20px; padding: 0 8px; border-radius: var(--ny-r-pastille); border: 1px solid var(--ny-trait); background: var(--ny-carte-2); color: var(--ny-encre-2); font: 500 12px/16px var(--ny-police); } +.objet.sans { font-style: italic; } +.titre-groupe { font: 600 17px/24px var(--ny-police); color: var(--ny-encre); } +.jour-titre { display: flex; align-items: baseline; justify-content: space-between; gap: var(--ny-e3); padding: var(--ny-e3) var(--ny-e5); background: var(--ny-carte-2); border-bottom: 1px solid var(--ny-trait); font: 600 14px/20px var(--ny-police); color: var(--ny-encre-2); } + /* Pastille de catégorie */ .pt { width: 10px; height: 10px; border-radius: var(--ny-r-pastille); flex: none; box-sizing: border-box; background: var(--ny-cat-vide); } .pt.c1 { background: var(--ny-cat-1); } .pt.c2 { background: var(--ny-cat-2); } .pt.c3 { background: var(--ny-cat-3); } .pt.c4 { background: var(--ny-cat-4); } @@ -181,7 +188,7 @@ h1, h2, h3, p { margin: 0; } .principal { padding: var(--ny-e4); gap: var(--ny-e3); overflow-y: auto; } .principal > * { flex-shrink: 0; } .entete h1 { font: var(--ny-t2); } - .nav-mobile { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--ny-carte); border-top: 1px solid var(--ny-trait); padding: 4px 8px 16px; } + .nav-mobile { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--ny-carte); border-top: 1px solid var(--ny-trait); padding: 4px 8px 16px; } .nav-mobile .nav-btn { min-height: 56px; flex-direction: column; justify-content: center; gap: 4px; padding: 0; text-align: center; font: 500 13px/18px var(--ny-police); } .nav-mobile .nav-btn[aria-current="page"] { font-weight: 600; } .nav-mobile .nav-btn svg { width: 22px; height: 22px; } diff --git a/app/static/app.js b/app/static/app.js index 2b7cbe5..7446993 100644 --- a/app/static/app.js +++ b/app/static/app.js @@ -13,10 +13,11 @@ courses: 'M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0Z|M12 13a3 3 0 1 0 0-6 3 3 0 0 0 0 6', 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' }; var NAV = [ - ['tri', 'Tri du jour', true], ['categories', 'Catégories', true], ['regles', 'Règles', true], + ['tri', 'Tri du jour', true], ['categories', 'Catégories', true], ['groupes', 'Groupes', true], ['regles', 'Règles', true], ['recherche', 'Recherche', false], ['courses', 'Courses pour Tunis', false], ['suivi', 'Suivi des demandes', false], ['pj', 'Pièces jointes', false] ]; var BIENTOT = { @@ -28,7 +29,7 @@ var COULEUR_NOM = { 'Tunis': 1, 'Demande FB': 2, 'CI': 3, 'Câbles': 4, 'Sans objet': 5, 'À classer': 0 }; var SYSTEME = 'À classer'; - var S = { vue: 'tri', jour: auj(), filtre: null, cats: [], mails: [], etat: 'chargement', banniere: null, notif: null, regles: [], reglesEtat: 'ok', miroirErreur: false, majHeure: '' }; + var S = { vue: 'tri', jour: auj(), filtre: null, cats: [], mails: [], etat: 'chargement', banniere: null, notif: null, regles: [], reglesEtat: 'ok', miroirErreur: false, majHeure: '', groupe: { jours: 7, cat: null, etq: null, mails: [], etat: 'chargement' } }; /* ---------- Utilitaires ---------- */ function auj() { return new Intl.DateTimeFormat('en-CA', { timeZone: TZ }).format(new Date()); } @@ -123,6 +124,15 @@ rendre(); }).catch(function (e) { if (e.message === 'session') return; if (jour !== S.jour) return; S.etat = 'erreur'; rendre(); }); } + function chargerGroupe() { + var G = S.groupe, jours = G.jours; + G.etat = 'chargement'; rendre(); + return Promise.all([chargerCats(), api('/mails/periode?jours=' + jours)]).then(function (rs) { + if (jours !== G.jours) return; + G.mails = (rs[1].mails || []).map(normaliser).sort(function (a, b) { return String(b.date).localeCompare(String(a.date)); }); + G.miroirErreur = !!rs[1].miroir_erreur; G.etat = 'ok'; rendre(); + }).catch(function (e) { if (e.message === 'session') return; if (jours !== G.jours) return; G.etat = 'erreur'; rendre(); }); + } function chargerRegles() { S.reglesEtat = 'chargement'; rendre(); return Promise.all([chargerCats(), api('/regles')]).then(function (rs) { @@ -140,6 +150,7 @@ function allerA(vue) { S.vue = vue; S.banniere = null; if (vue === 'tri' || vue === 'categories') chargerMails(); + else if (vue === 'groupes') chargerGroupe(); else if (vue === 'regles') chargerRegles(); else rendre(); document.getElementById('principal').focus(); @@ -157,7 +168,7 @@ .then(function (r) { var rp = r.regle_proposee; if (rp && rp.id) { - var autres = S.mails.filter(function (x) { return x.exp === m.exp && x.id !== m.id && x.source !== 'manuel' && x.cat !== cat; }).length; + var autres = (S.vue === 'groupes' ? S.groupe.mails : S.mails).filter(function (x) { return x.exp === m.exp && x.id !== m.id && x.source !== 'manuel' && x.cat !== cat; }).length; S.banniere = { id: rp.id, exp: m.exp, cat: cat, autres: autres }; rendre(); } }).catch(function (e) { if (e.message === 'session') return; m.cat = avant.cat; m.source = avant.source; rendre(); flash('La correction n\'a pas été enregistrée. Réessayez.'); }); @@ -271,7 +282,7 @@ } var liste = h('div', { class: 'nav-liste' }); NAV.forEach(function (n, i) { - if (i === 3) liste.appendChild(h('div', { class: 'nav-sep' }, 'Prochainement')); + if (i === 4) liste.appendChild(h('div', { class: 'nav-sep' }, 'Prochainement')); liste.appendChild(bouton(n)); if (n[2]) mobile.appendChild(bouton(n)); }); @@ -297,6 +308,12 @@ 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'))); } + function reperes(m) { + if (!m.etiquettes || !m.etiquettes.length) return null; + var d = h('div', { class: 'etqs', 'aria-label': 'Étiquettes du miroir' }); + m.etiquettes.forEach(function (t) { d.appendChild(h('span', { class: 'etq', text: t })); }); + return d; + } function ligneMail(m) { var orig = m.source === 'manuel' ? 'manuel' : (m.cat === SYSTEME ? 'sans règle' : 'règle'); var origCls = orig === 'manuel' ? ' manuel' : orig === 'sans règle' ? ' sansregle' : ''; @@ -307,12 +324,12 @@ 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 })), + h('div', { class: 'corps d' }, h('div', { 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 }))), + h('div', { 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() { @@ -409,6 +426,56 @@ frag.appendChild(h('p', { class: 'note' }, 'Une règle activée s\'applique aux mails du jour qui n\'ont pas été corrigés à la main.')); return frag; } + function vueGroupes() { + var G = S.groupe, frag = document.createDocumentFragment(); + frag.appendChild(h('header', { class: 'entete' }, h('div', null, h('h1', null, 'Groupes'), h('div', { class: 'maj' }, 'Un seul groupe, sur plusieurs jours · heure de Tunis')))); + if (S.notif) frag.appendChild(h('div', { class: 'notif', role: 'status', text: S.notif })); + var per = h('div', { class: 'puces', role: 'group', 'aria-label': 'Période' }); + [[7, '7 jours'], [30, '30 jours']].forEach(function (p) { + per.appendChild(h('button', { type: 'button', class: 'puce', 'aria-pressed': G.jours === p[0] ? 'true' : 'false', onclick: function () { if (G.jours !== p[0]) { G.jours = p[0]; chargerGroupe(); } } }, h('span', { text: p[1] }))); + }); + frag.appendChild(h('section', { class: 'carte repartition', 'aria-label': 'Choisir un groupe' }, h('h2', null, 'Période'), per)); + if (G.etat === 'erreur') { + frag.appendChild(h('div', { class: 'erreur', role: 'alert' }, h('h2', null, 'Lecture du courrier impossible'), h('p', null, 'Le service de lecture n\'a pas répondu.'), h('button', { type: 'button', class: 'btn-plein', onclick: chargerGroupe }, 'Réessayer'))); + return frag; + } + if (G.etat === 'chargement') { frag.appendChild(h('section', { class: 'carte squelette', 'aria-busy': 'true' }, h('div', { class: 'sq-statut', role: 'status', text: 'Lecture des ' + G.jours + ' derniers jours…' }))); return frag; } + if (G.miroirErreur) frag.appendChild(h('div', { class: 'bandeau', role: 'status' }, h('div', { class: 'txt' }, 'Lecture du courrier momentanément indisponible'))); + var cc = {}, ce = {}; + G.mails.forEach(function (m) { cc[m.cat] = (cc[m.cat] || 0) + 1; (m.etiquettes || []).forEach(function (t) { ce[t] = (ce[t] || 0) + 1; }); }); + var pc = h('div', { class: 'puces', role: 'group', 'aria-label': 'Catégorie' }); + S.cats.forEach(function (c) { + var k = couleur(c.nom, c.id), actif = G.cat === c.nom; + pc.appendChild(h('button', { type: 'button', 'aria-pressed': actif ? 'true' : 'false', class: 'puce' + (k === 0 ? ' vide' : '') + (k > 0 ? ' t' + k : ''), onclick: function () { G.cat = actif ? null : c.nom; rendre(); } }, + h('span', { class: 'pt c' + k, 'aria-hidden': 'true' }), h('span', { text: c.nom }), h('span', { class: 'n', text: d2(cc[c.nom] || 0) }))); + }); + var pe = h('div', { class: 'puces', role: 'group', 'aria-label': 'Étiquette du miroir' }); + Object.keys(ce).sort(function (a, b) { return ce[b] - ce[a] || a.localeCompare(b); }).forEach(function (t) { + var actif = G.etq === t; + pe.appendChild(h('button', { type: 'button', 'aria-pressed': actif ? 'true' : 'false', class: 'puce', onclick: function () { G.etq = actif ? null : t; rendre(); } }, h('span', { text: t }), h('span', { class: 'n', text: d2(ce[t]) }))); + }); + frag.appendChild(h('section', { class: 'carte repartition', 'aria-label': 'Groupes' }, h('h2', null, 'Catégorie'), pc, h('h2', null, 'Étiquette du miroir'), pe)); + if (!G.cat && !G.etq) { + frag.appendChild(h('section', { class: 'carte' }, h('div', { class: 'etat' }, icone('groupes', 28), h('h2', null, 'Choisissez un groupe'), h('p', null, G.mails.length + ' mails sur ' + G.jours + ' jours. Touchez une catégorie ou une étiquette pour ne voir que celle-ci.')))); + return frag; + } + var vis = G.mails.filter(function (m) { return (!G.cat || m.cat === G.cat) && (!G.etq || (m.etiquettes || []).indexOf(G.etq) !== -1); }); + var titreG = [G.cat, G.etq].filter(Boolean).join(' + '); + if (!vis.length) { + frag.appendChild(h('section', { class: 'carte' }, h('div', { class: 'etat-filtre' }, 'Aucun mail dans « ' + titreG + ' » sur ' + G.jours + ' jours.', h('button', { type: 'button', class: 'btn-lien', onclick: function () { G.cat = null; G.etq = null; rendre(); } }, 'Changer de groupe')))); + return frag; + } + frag.appendChild(h('h2', { class: 'titre-groupe', text: titreG + ' · ' + vis.length + (vis.length > 1 ? ' mails' : ' mail') })); + var jours = [], par = {}; + vis.forEach(function (m) { var j = String(m.date).slice(0, 10); if (!par[j]) { par[j] = []; jours.push(j); } par[j].push(m); }); + jours.forEach(function (j) { + var carte = h('section', { class: 'carte', 'aria-label': nomJour(j) + ' ' + fr(j) }); + carte.appendChild(h('div', { class: 'jour-titre' }, h('span', { text: nomJour(j) + ' ' + fr(j) }), h('span', { class: 'nb', text: par[j].length + (par[j].length > 1 ? ' mails' : ' mail') }))); + var liste = h('div', { role: 'list' }); par[j].forEach(function (m) { liste.appendChild(ligneMail(m)); }); + carte.appendChild(liste); frag.appendChild(carte); + }); + return frag; + } function vueBientot(k) { var t = BIENTOT[k]; return h('div', null, h('header', { class: 'entete' }, h('h1', null, t[0])), @@ -419,9 +486,9 @@ var ancre = document.activeElement && document.activeElement.getAttribute && document.activeElement.getAttribute('aria-label'); rendreNav(); var p = document.getElementById('principal'); p.textContent = ''; - var v = S.vue === 'tri' ? vueTri() : S.vue === 'categories' ? vueCategories() : S.vue === 'regles' ? vueRegles() : vueBientot(S.vue); + var v = S.vue === 'tri' ? vueTri() : S.vue === 'categories' ? vueCategories() : S.vue === 'regles' ? vueRegles() : S.vue === 'groupes' ? vueGroupes() : vueBientot(S.vue); p.appendChild(v); p.scrollTop = pos; - var titre = S.vue === 'tri' ? 'Tri du jour' : S.vue === 'categories' ? 'Catégories' : S.vue === 'regles' ? 'Règles' : BIENTOT[S.vue][0]; + var titre = S.vue === 'tri' ? 'Tri du jour' : S.vue === 'categories' ? 'Catégories' : S.vue === 'regles' ? 'Règles' : S.vue === 'groupes' ? 'Groupes' : BIENTOT[S.vue][0]; document.title = titre + ' · Courrier'; if (ancre && !dialogue) { var r = p.querySelector('[aria-label="' + ancre.replace(/"/g, '\\"') + '"]'); if (r && document.activeElement === document.body) r.focus(); } } diff --git a/app/static/index.html b/app/static/index.html index 119a48f..cebd93d 100644 --- a/app/static/index.html +++ b/app/static/index.html @@ -10,7 +10,7 @@ - +