From f4d1298669639c19a30d8a698b300c8809de35cc Mon Sep 17 00:00:00 2001 From: Septias Date: Mon, 11 Nov 2024 00:26:26 +0100 Subject: [PATCH 1/2] feat: add device message for imported contacts after configuration Add a short info message about how many contacts were added during configuration. --- src/imap.rs | 29 +++++++++++++++++++---------- src/scheduler.rs | 18 ++++++++++++++---- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/imap.rs b/src/imap.rs index 4eb432599d..fcf4f3a063 100644 --- a/src/imap.rs +++ b/src/imap.rs @@ -768,14 +768,16 @@ impl Imap { &mut self, context: &Context, session: &mut Session, - ) -> Result<()> { - add_all_recipients_as_contacts(context, session, Config::ConfiguredSentboxFolder) - .await - .context("failed to get recipients from the sentbox")?; - add_all_recipients_as_contacts(context, session, Config::ConfiguredMvboxFolder) + ) -> Result { + let mut created = 0; + created += + add_all_recipients_as_contacts(context, session, Config::ConfiguredSentboxFolder) + .await + .context("failed to get recipients from the sentbox")?; + created += add_all_recipients_as_contacts(context, session, Config::ConfiguredMvboxFolder) .await .context("failed to get recipients from the movebox")?; - add_all_recipients_as_contacts(context, session, Config::ConfiguredInboxFolder) + created += add_all_recipients_as_contacts(context, session, Config::ConfiguredInboxFolder) .await .context("failed to get recipients from the inbox")?; @@ -802,7 +804,7 @@ impl Imap { } info!(context, "Done fetching existing messages."); - Ok(()) + Ok(created) } } @@ -2479,11 +2481,14 @@ impl std::fmt::Display for UidRange { } } } + +/// Add all recipients as contacts. +/// Returns how many contacts were created. async fn add_all_recipients_as_contacts( context: &Context, session: &mut Session, folder: Config, -) -> Result<()> { +) -> Result { let mailbox = if let Some(m) = context.get_config(folder).await? { m } else { @@ -2491,7 +2496,7 @@ async fn add_all_recipients_as_contacts( context, "Folder {} is not configured, skipping fetching contacts from it.", folder ); - return Ok(()); + return Ok(0); }; session .select_with_uidvalidity(context, &mailbox) @@ -2504,6 +2509,7 @@ async fn add_all_recipients_as_contacts( .context("could not get recipients")?; let mut any_modified = false; + let mut created = 0; for recipient in recipients { let recipient_addr = match ContactAddress::new(&recipient.addr) { Err(err) => { @@ -2528,12 +2534,15 @@ async fn add_all_recipients_as_contacts( if modified != Modifier::None { any_modified = true; } + if modified == Modifier::Created { + created += 1; + } } if any_modified { context.emit_event(EventType::ContactsChanged(None)); } - Ok(()) + Ok(created) } #[cfg(test)] diff --git a/src/scheduler.rs b/src/scheduler.rs index 58e6dbaf3e..080afa4d15 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -19,12 +19,12 @@ use crate::download::{download_msg, DownloadState}; use crate::ephemeral::{self, delete_expired_imap_messages}; use crate::events::EventType; use crate::imap::{session::Session, FolderMeaning, Imap}; -use crate::location; use crate::log::LogExt; -use crate::message::MsgId; +use crate::message::{Message, MsgId}; use crate::smtp::{send_smtp_messages, Smtp}; use crate::sql; use crate::tools::{self, duration_to_str, maybe_add_time_based_warnings, time, time_elapsed}; +use crate::{chat, location}; pub(crate) mod connectivity; @@ -514,8 +514,18 @@ async fn inbox_fetch_idle(ctx: &Context, imap: &mut Imap, mut session: Session) warn!(ctx, "Can't set Config::FetchedExistingMsgs: {:#}", err); } - if let Err(err) = imap.fetch_existing_msgs(ctx, &mut session).await { - warn!(ctx, "Failed to fetch existing messages: {:#}", err); + match imap.fetch_existing_msgs(ctx, &mut session).await { + Err(err) => { + warn!(ctx, "Failed to fetch existing messages: {:#}", err); + } + Ok(count) => { + let mut msg = Message::new_text(format!( + "Added {} contacts from outgoing chats", + count + )); + + chat::add_device_msg(ctx, None, Some(&mut msg)); + } } } } From 1d8e969b34f2f800c5868861d61d7abf21a0ea6b Mon Sep 17 00:00:00 2001 From: Septias Date: Mon, 11 Nov 2024 00:35:26 +0100 Subject: [PATCH 2/2] use future and result --- src/scheduler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scheduler.rs b/src/scheduler.rs index 080afa4d15..4ba03d9541 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -524,7 +524,7 @@ async fn inbox_fetch_idle(ctx: &Context, imap: &mut Imap, mut session: Session) count )); - chat::add_device_msg(ctx, None, Some(&mut msg)); + chat::add_device_msg(ctx, None, Some(&mut msg)).await?; } } }