From 4c32bf450dda8d2af1c47a9f50f36ab75b1a570b Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Tue, 24 Mar 2020 21:01:06 +0200 Subject: [PATCH] Add {un,}subscribe mailbox operations Concerns #17 --- src/conf/accounts.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/conf/accounts.rs b/src/conf/accounts.rs index 2d83efd5..4c0613e2 100644 --- a/src/conf/accounts.rs +++ b/src/conf/accounts.rs @@ -1048,8 +1048,48 @@ impl Account { Ok(format!("'`{}` has been deleted.", &path)) } - MailboxOperation::Subscribe(_) => Err(MeliError::new("Not implemented.")), - MailboxOperation::Unsubscribe(_) => Err(MeliError::new("Not implemented.")), + MailboxOperation::Subscribe(path) => { + let mailbox_hash = if let Some((mailbox_hash, _)) = self + .mailbox_entries + .iter() + .find(|(_, f)| f.ref_mailbox.path() == path) + { + *mailbox_hash + } else { + return Err(MeliError::new("Mailbox with that path not found.")); + }; + self.backend + .write() + .unwrap() + .set_mailbox_subscription(mailbox_hash, true)?; + self.mailbox_entries.entry(mailbox_hash).and_modify(|m| { + m.conf.mailbox_conf.subscribe = super::ToggleFlag::True; + let _ = m.ref_mailbox.set_is_subscribed(true); + }); + + Ok(format!("'`{}` has been subscribed.", &path)) + } + MailboxOperation::Unsubscribe(path) => { + let mailbox_hash = if let Some((mailbox_hash, _)) = self + .mailbox_entries + .iter() + .find(|(_, f)| f.ref_mailbox.path() == path) + { + *mailbox_hash + } else { + return Err(MeliError::new("Mailbox with that path not found.")); + }; + self.backend + .write() + .unwrap() + .set_mailbox_subscription(mailbox_hash, false)?; + self.mailbox_entries.entry(mailbox_hash).and_modify(|m| { + m.conf.mailbox_conf.subscribe = super::ToggleFlag::False; + let _ = m.ref_mailbox.set_is_subscribed(false); + }); + + Ok(format!("'`{}` has been unsubscribed.", &path)) + } MailboxOperation::Rename(_, _) => Err(MeliError::new("Not implemented.")), MailboxOperation::SetPermissions(_) => Err(MeliError::new("Not implemented.")), }