From 61d3d2b769a1ec20d9eb6141c70ca40de028fe4b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 3 Jan 2020 12:48:30 +0100 Subject: Add MailStore::thread_root_of() This patch adds a function to find the root node of a mailthread (as in In-Reply-To header). Signed-off-by: Matthias Beyer --- lib/domain/libimagmail/src/mail.rs | 1 + lib/domain/libimagmail/src/store.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/domain/libimagmail/src/mail.rs b/lib/domain/libimagmail/src/mail.rs index bdcee6a2..d8e009f0 100644 --- a/lib/domain/libimagmail/src/mail.rs +++ b/lib/domain/libimagmail/src/mail.rs @@ -249,6 +249,7 @@ impl Mail for Entry { Ok(iter) } + } #[derive(Debug)] diff --git a/lib/domain/libimagmail/src/store.rs b/lib/domain/libimagmail/src/store.rs index 637dac54..88f49df1 100644 --- a/lib/domain/libimagmail/src/store.rs +++ b/lib/domain/libimagmail/src/store.rs @@ -59,6 +59,8 @@ pub trait MailStore<'a> { fn get_mail(&'a self, mid: MessageId) -> Result>>; fn all_mails(&'a self) -> Result>; + + fn thread_root_of(&'a self, mid: MessageId, refconfig: &Config) -> Result>>; } impl<'a> MailStore<'a> for Store { @@ -149,5 +151,23 @@ impl<'a> MailStore<'a> for Store { fn all_mails(&'a self) -> Result> { self.entries()?.in_collection("mail") } + + /// Compute the thread root of the thread the Mail belongs to + /// + /// This function recursively traverses the thread using `Store::get_mail()` and + /// `Mail::get_in_reply_to()` to find the root of the thread (The mail that is not a reply to + /// any other mail). + /// + fn thread_root_of(&'a self, mid: MessageId, refconfig: &Config) -> Result>> { + if let Some(entry) = self.get_mail(mid)? { + if let Some(parent_mid) = entry.get_in_reply_to(refconfig)? { + self.thread_root_of(parent_mid, refconfig) + } else { + Ok(Some(entry)) + } + } else { + Ok(None) + } + } } -- cgit v1.2.3