imap: flatten fetch_idle()

This commit is contained in:
link2xt
2022-11-13 16:10:06 +00:00
parent 13f30c3167
commit 43f2d64a6f

View File

@@ -166,16 +166,33 @@ async fn inbox_loop(ctx: Context, started: Sender<()>, inbox_handlers: ImapConne
.await; .await;
} }
async fn fetch_idle(ctx: &Context, connection: &mut Imap, folder: Config) -> InterruptInfo { async fn fetch_idle(ctx: &Context, connection: &mut Imap, folder_config: Config) -> InterruptInfo {
match ctx.get_config(folder).await { let folder = match ctx.get_config(folder_config).await {
Ok(Some(watch_folder)) => { Ok(folder) => folder,
Err(err) => {
warn!(
ctx,
"Can not watch {} folder, failed to retrieve config: {:#}", folder_config, err
);
return connection.fake_idle(ctx, None).await;
}
};
let watch_folder = if let Some(watch_folder) = folder {
watch_folder
} else {
connection.connectivity.set_not_configured(ctx).await;
info!(ctx, "Can not watch {} folder, not set", folder_config);
return connection.fake_idle(ctx, None).await;
};
// connect and fake idle if unable to connect // connect and fake idle if unable to connect
if let Err(err) = connection.prepare(ctx).await { if let Err(err) = connection.prepare(ctx).await {
warn!(ctx, "imap connection failed: {}", err); warn!(ctx, "imap connection failed: {}", err);
return connection.fake_idle(ctx, Some(watch_folder)).await; return connection.fake_idle(ctx, Some(watch_folder)).await;
} }
if folder == Config::ConfiguredInboxFolder { if folder_config == Config::ConfiguredInboxFolder {
if let Err(err) = connection if let Err(err) = connection
.store_seen_flags_on_imap(ctx) .store_seen_flags_on_imap(ctx)
.await .await
@@ -210,7 +227,7 @@ async fn fetch_idle(ctx: &Context, connection: &mut Imap, folder: Config) -> Int
// //
// On iOS the application has strictly limited time to work in background, so we may not // On iOS the application has strictly limited time to work in background, so we may not
// be able to scan all folders before time is up if there are many of them. // be able to scan all folders before time is up if there are many of them.
if folder == Config::ConfiguredInboxFolder { if folder_config == Config::ConfiguredInboxFolder {
// Only scan on the Inbox thread in order to prevent parallel scans, which might lead to duplicate messages // Only scan on the Inbox thread in order to prevent parallel scans, which might lead to duplicate messages
match connection.scan_folders(ctx).await { match connection.scan_folders(ctx).await {
Err(err) => { Err(err) => {
@@ -248,7 +265,10 @@ async fn fetch_idle(ctx: &Context, connection: &mut Imap, folder: Config) -> Int
connection.connectivity.set_connected(ctx).await; connection.connectivity.set_connected(ctx).await;
// idle // idle
if connection.can_idle() { if !connection.can_idle() {
return connection.fake_idle(ctx, Some(watch_folder)).await;
}
match connection.idle(ctx, Some(watch_folder)).await { match connection.idle(ctx, Some(watch_folder)).await {
Ok(v) => v, Ok(v) => v,
Err(err) => { Err(err) => {
@@ -257,23 +277,6 @@ async fn fetch_idle(ctx: &Context, connection: &mut Imap, folder: Config) -> Int
InterruptInfo::new(false) InterruptInfo::new(false)
} }
} }
} else {
connection.fake_idle(ctx, Some(watch_folder)).await
}
}
Ok(None) => {
connection.connectivity.set_not_configured(ctx).await;
info!(ctx, "Can not watch {} folder, not set", folder);
connection.fake_idle(ctx, None).await
}
Err(err) => {
warn!(
ctx,
"Can not watch {} folder, failed to retrieve config: {:?}", folder, err
);
connection.fake_idle(ctx, None).await
}
}
} }
async fn simple_imap_loop( async fn simple_imap_loop(