From f033d0af40ee86bfa4684db701e55619a204a346 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 6 Jan 2020 18:11:20 +0100 Subject: Make sure messages are dropped before re-fetching them from notmuch Signed-off-by: Matthias Beyer --- lib/domain/libimagmail/src/mailtree.rs | 47 +++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/domain/libimagmail/src/mailtree.rs b/lib/domain/libimagmail/src/mailtree.rs index 1ac63350..e1c052af 100644 --- a/lib/domain/libimagmail/src/mailtree.rs +++ b/lib/domain/libimagmail/src/mailtree.rs @@ -21,6 +21,7 @@ use failure::Fallible as Result; use indextree::Arena; use indextree::NodeId; use indextree::Node; +use itertools::Itertools; use crate::store::MailStoreWithConnection; use crate::mail::Mail; @@ -55,9 +56,11 @@ fn fill_arena_with<'a>(arena: &mut Arena, store: &'a MailStoreWithConnec trace!("Filling arena starting at: {}", root.get_id()); let root_id = root.get_id().clone(); let root_node = arena.new_node(root_id); + let root_thread_id = root.get_thread_id().to_owned(); + drop(root); store.connection().execute(|db| { - let q = format!("thread:{}", root.get_thread_id()); + let q = format!("thread:{}", root_thread_id); trace!("Executing query: {}", q); let query = db.create_query(&q)?; let r = query.search_threads()? @@ -68,28 +71,36 @@ fn fill_arena_with<'a>(arena: &mut Arena, store: &'a MailStoreWithConnec let id = msg.id(); trace!("Found Message: {}", id); - let mail = store.get_mail_by_id(&id)? - .ok_or_else(|| format_err!("Cannot find mail for id {}", id))?; - trace!("Fetched from store: {}", mail.get_location()); - - let reply = mail - .load(store.connection())? - .ok_or_else(|| format_err!("Tried to load unavailable mail: {}", mail.get_location()))?; - trace!("Loaded from store: {}", reply.get_id()); - - let new_node_id = arena.new_node(id.into_owned()); + let id = id.into_owned(); + let new_node_id = arena.new_node(id.clone()); root_node.append(new_node_id, arena); - - trace!("Recursing with: {}", reply.get_id()); - fill_arena_with(arena, store, reply).map(|_| ()) + id }) - .collect::>>() + .collect::>() }) - .collect::>>() - .map(|_| root_node); - + .flat_map(|v| v.into_iter()) + .unique() + .map(Ok) + .collect::>>(); r + })? + .into_iter() + .map(|id: String| { + let mail = store.get_mail_by_id(&id)? + .ok_or_else(|| format_err!("Cannot find mail for id {}", id))?; + trace!("Fetched from store: {}", mail.get_location()); + + + let reply = mail + .load(store.connection())? + .ok_or_else(|| format_err!("Tried to load unavailable mail: {}", mail.get_location()))?; + trace!("Loaded from store: {}", reply.get_id()); + + trace!("Recursing with: {}", reply.get_id()); + fill_arena_with(arena, store, reply).map(|_| ()) }) + .collect::>>() + .map(|_| root_node) } /// A type for traversing the Mailtree -- cgit v1.2.3