wip: fix thread id mapping

This commit is contained in:
Clément DOUIN 2024-05-26 09:30:05 +02:00
parent b773218c94
commit 16d273febc
No known key found for this signature in database
GPG key ID: 353E4A18EE0FAB72
2 changed files with 95 additions and 14 deletions

View file

@ -25,7 +25,7 @@ use email::{
list::{ListEnvelopes, ListEnvelopesOptions}, list::{ListEnvelopes, ListEnvelopesOptions},
thread::ThreadEnvelopes, thread::ThreadEnvelopes,
watch::WatchEnvelopes, watch::WatchEnvelopes,
Id, SingleId, ThreadedEnvelopes, Id, SingleId,
}, },
flag::{add::AddFlags, remove::RemoveFlags, set::SetFlags, Flag, Flags}, flag::{add::AddFlags, remove::RemoveFlags, set::SetFlags, Flag, Flags},
folder::{ folder::{
@ -46,7 +46,11 @@ use email::{
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{account::config::TomlAccountConfig, cache::IdMapper, envelope::Envelopes}; use crate::{
account::config::TomlAccountConfig,
cache::IdMapper,
envelope::{Envelopes, ThreadedEnvelopes},
};
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
@ -713,8 +717,7 @@ impl Backend {
let backend_kind = self.toml_account_config.thread_envelopes_kind(); let backend_kind = self.toml_account_config.thread_envelopes_kind();
let id_mapper = self.build_id_mapper(folder, backend_kind)?; let id_mapper = self.build_id_mapper(folder, backend_kind)?;
let envelopes = self.backend.thread_envelopes(folder, opts).await?; let envelopes = self.backend.thread_envelopes(folder, opts).await?;
// let envelopes = let envelopes = ThreadedEnvelopes::try_from_backend(&id_mapper, envelopes)?;
// Envelopes::from_backend(&self.backend.account_config, &id_mapper, envelopes)?;
Ok(envelopes) Ok(envelopes)
} }
@ -726,12 +729,12 @@ impl Backend {
) -> Result<ThreadedEnvelopes> { ) -> Result<ThreadedEnvelopes> {
let backend_kind = self.toml_account_config.thread_envelopes_kind(); let backend_kind = self.toml_account_config.thread_envelopes_kind();
let id_mapper = self.build_id_mapper(folder, backend_kind)?; let id_mapper = self.build_id_mapper(folder, backend_kind)?;
let id = id_mapper.get_id(id)?;
let envelopes = self let envelopes = self
.backend .backend
.thread_envelope(folder, SingleId::from(id), opts) .thread_envelope(folder, SingleId::from(id), opts)
.await?; .await?;
// let envelopes = let envelopes = ThreadedEnvelopes::try_from_backend(&id_mapper, envelopes)?;
// Envelopes::from_backend(&self.backend.account_config, &id_mapper, envelopes)?;
Ok(envelopes) Ok(envelopes)
} }

View file

@ -6,13 +6,10 @@ pub mod flag;
use color_eyre::Result; use color_eyre::Result;
use comfy_table::{presets, Attribute, Cell, ContentArrangement, Row, Table}; use comfy_table::{presets, Attribute, Cell, ContentArrangement, Row, Table};
use crossterm::{cursor, style::Stylize, terminal}; use crossterm::{cursor, style::Stylize, terminal};
use email::{ use email::{account::config::AccountConfig, envelope::ThreadedEnvelope};
account::config::AccountConfig,
envelope::{ThreadedEnvelope, ThreadedEnvelopes},
};
use petgraph::graphmap::DiGraphMap; use petgraph::graphmap::DiGraphMap;
use serde::{Serialize, Serializer}; use serde::{Serialize, Serializer};
use std::{fmt, ops::Deref, sync::Arc}; use std::{collections::HashMap, fmt, ops::Deref, sync::Arc};
use crate::{ use crate::{
cache::IdMapper, cache::IdMapper,
@ -156,7 +153,6 @@ impl From<&Envelope> for Row {
} }
} }
/// Represents the list of envelopes.
#[derive(Clone, Debug, Default, Serialize)] #[derive(Clone, Debug, Default, Serialize)]
pub struct Envelopes(Vec<Envelope>); pub struct Envelopes(Vec<Envelope>);
@ -261,6 +257,80 @@ impl Serialize for EnvelopesTable {
} }
} }
pub struct ThreadedEnvelopes(email::envelope::ThreadedEnvelopes);
impl ThreadedEnvelopes {
pub fn try_from_backend(
id_mapper: &IdMapper,
envelopes: email::envelope::ThreadedEnvelopes,
) -> Result<ThreadedEnvelopes> {
let prev_edges = envelopes
.graph()
.all_edges()
.map(|(a, b, w)| {
let a = id_mapper.get_or_create_alias(&a.id)?;
let b = id_mapper.get_or_create_alias(&b.id)?;
Ok((a, b, *w))
})
.collect::<Result<Vec<_>>>()?;
let envelopes = envelopes
.map()
.iter()
.map(|(_, envelope)| {
let id = id_mapper.get_or_create_alias(&envelope.id)?;
let envelope = email::envelope::Envelope {
id: id.clone(),
message_id: envelope.message_id.clone(),
in_reply_to: envelope.in_reply_to.clone(),
flags: envelope.flags.clone(),
subject: envelope.subject.clone(),
from: envelope.from.clone(),
to: envelope.to.clone(),
date: envelope.date.clone(),
};
Ok((id, envelope))
})
.collect::<Result<HashMap<_, _>>>()?;
let envelopes = email::envelope::ThreadedEnvelopes::build(envelopes, move |envelopes| {
let mut graph = DiGraphMap::<ThreadedEnvelope, u8>::new();
for (a, b, w) in prev_edges.clone() {
let eb = envelopes.get(&b).unwrap();
match envelopes.get(&a) {
Some(ea) => {
graph.add_edge(ea.as_threaded(), eb.as_threaded(), w);
}
None => {
let ea = ThreadedEnvelope {
id: "0",
message_id: "0",
subject: "",
from: "",
date: Default::default(),
};
graph.add_edge(ea, eb.as_threaded(), w);
}
}
}
graph
});
Ok(ThreadedEnvelopes(envelopes))
}
}
impl Deref for ThreadedEnvelopes {
type Target = email::envelope::ThreadedEnvelopes;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub struct EnvelopesTree { pub struct EnvelopesTree {
config: Arc<AccountConfig>, config: Arc<AccountConfig>,
envelopes: ThreadedEnvelopes, envelopes: ThreadedEnvelopes,
@ -339,7 +409,7 @@ impl fmt::Display for EnvelopesTree {
EnvelopesTree::fmt( EnvelopesTree::fmt(
f, f,
&self.config, &self.config,
self.envelopes.graph(), self.envelopes.0.graph(),
ThreadedEnvelope { ThreadedEnvelope {
id: "0", id: "0",
message_id: "0", message_id: "0",
@ -358,6 +428,14 @@ impl Serialize for EnvelopesTree {
where where
S: Serializer, S: Serializer,
{ {
self.envelopes.serialize(serializer) self.envelopes.0.serialize(serializer)
}
}
impl Deref for EnvelopesTree {
type Target = ThreadedEnvelopes;
fn deref(&self) -> &Self::Target {
&self.envelopes
} }
} }