Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/imap/idle.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use super::Imap;

use anyhow::{bail, Context as _, Result};
use async_imap::extensions::idle::IdleResponse;
use async_imap::{
extensions::idle::IdleResponse,
imap_proto::{MailboxDatum, Response},
};
use async_std::prelude::*;
use std::time::{Duration, SystemTime};

use crate::{context::Context, scheduler::InterruptInfo};
use crate::{config::Config, context::Context, scheduler::InterruptInfo};

use super::session::Session;

Expand All @@ -14,6 +17,7 @@ impl Imap {
self.config.can_idle
}

//TODO rename to `idle_fetch()` and `fetch_idle()` to `fetch_idle_fetch()`?
pub async fn idle(
&mut self,
context: &Context,
Expand Down Expand Up @@ -68,9 +72,13 @@ impl Imap {
Ok(Event::Interrupt(info.unwrap_or_default()))
});

let mut needs_fetch = false;
match fut.await {
Ok(Event::IdleResponse(IdleResponse::NewData(x))) => {
info!(context, "Idle has NewData {:?}", x);
if let Response::MailboxData(MailboxDatum::Exists(_)) = x.parsed() {
needs_fetch = true;
}
}
Ok(Event::IdleResponse(IdleResponse::Timeout)) => {
info!(context, "Idle-wait timeout or interruption");
Expand All @@ -93,6 +101,24 @@ impl Imap {
.await?
.context("IMAP IDLE protocol timed out")?;
self.session = Some(Session { inner: session });

if needs_fetch {
if let Some(folder) = &watch_folder {
// There are new messages. Immediately fetch them for quick performance.
if context.get_config_bool(Config::MvboxMove).await?
&& watch_folder == context.get_config(Config::ConfiguredInboxFolder).await?
{
// We may want to move the messages out of the Inbox folder, so call
// `fetch_move_delete()` so that they are moved as quickly as possible
// and the mvbox loop can fetch them.
// TODO testing
self.fetch_move_delete(context, folder).await?;
} else {
self.fetch_new_messages(context, folder, false).await?;
// TODO .context() and interrupt_ephemeral_task() are missing
}
}
}
} else {
warn!(context, "Attempted to idle without a session");
}
Expand Down