imap: resultify add_all_recipients_as_contacts()

This commit is contained in:
link2xt
2022-06-26 13:49:04 +00:00
parent 5920c5c136
commit 88a047b003

View File

@@ -919,9 +919,9 @@ impl Imap {
} }
self.prepare(context).await.context("could not connect")?; self.prepare(context).await.context("could not connect")?;
add_all_recipients_as_contacts(context, self, Config::ConfiguredSentboxFolder).await; add_all_recipients_as_contacts(context, self, Config::ConfiguredSentboxFolder).await?;
add_all_recipients_as_contacts(context, self, Config::ConfiguredMvboxFolder).await; add_all_recipients_as_contacts(context, self, Config::ConfiguredMvboxFolder).await?;
add_all_recipients_as_contacts(context, self, Config::ConfiguredInboxFolder).await; add_all_recipients_as_contacts(context, self, Config::ConfiguredInboxFolder).await?;
if context.get_config_bool(Config::FetchExistingMsgs).await? { if context.get_config_bool(Config::FetchExistingMsgs).await? {
for config in &[ for config in &[
@@ -2334,19 +2334,23 @@ impl std::fmt::Display for UidRange {
} }
} }
} }
async fn add_all_recipients_as_contacts(context: &Context, imap: &mut Imap, folder: Config) {
let mailbox = if let Ok(Some(m)) = context.get_config(folder).await { async fn add_all_recipients_as_contacts(
context: &Context,
imap: &mut Imap,
folder: Config,
) -> Result<()> {
let mailbox = if let Some(m) = context.get_config(folder).await? {
m m
} else { } else {
return; return Ok(());
}; };
if let Err(e) = imap.select_with_uidvalidity(context, &mailbox).await { imap.select_with_uidvalidity(context, &mailbox).await?;
// We are using Anyhow's .context() and to show the inner error, too, we need the {:#}: let contacts = imap
warn!(context, "Could not select {}: {:#}", mailbox, e); .get_all_recipients(context)
return; .await
} .context("could not get recipients")?;
match imap.get_all_recipients(context).await {
Ok(contacts) => {
let mut any_modified = false; let mut any_modified = false;
for contact in contacts { for contact in contacts {
let display_name_normalized = contact let display_name_normalized = contact
@@ -2355,28 +2359,22 @@ async fn add_all_recipients_as_contacts(context: &Context, imap: &mut Imap, fold
.map(|s| normalize_name(s)) .map(|s| normalize_name(s))
.unwrap_or_default(); .unwrap_or_default();
match Contact::add_or_lookup( let (_, modified) = Contact::add_or_lookup(
context, context,
&display_name_normalized, &display_name_normalized,
&contact.addr, &contact.addr,
Origin::OutgoingTo, Origin::OutgoingTo,
) )
.await .await
{ .context("could not add recipient")?;
Ok((_, modified)) => {
if modified != Modifier::None { if modified != Modifier::None {
any_modified = true; any_modified = true;
} }
} }
Err(e) => warn!(context, "Could not add recipient: {}", e),
}
}
if any_modified { if any_modified {
context.emit_event(EventType::ContactsChanged(None)); context.emit_event(EventType::ContactsChanged(None));
} }
} Ok(())
Err(e) => warn!(context, "Could not add recipients: {}", e),
};
} }
#[cfg(test)] #[cfg(test)]