Skip to content

Commit

Permalink
Implement secondary connection for messages
Browse files Browse the repository at this point in the history
  • Loading branch information
matzipan committed Oct 2, 2023
1 parent d26101e commit 57ee69e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
31 changes: 25 additions & 6 deletions src/backends/imap/imap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::models;

use diesel::Connection;
use futures::Future;
use melib;
use melib::backends::imap::{
Expand All @@ -20,9 +21,12 @@ use std::time::{Duration, SystemTime};

use std::pin::Pin;

type ConnectionMutexType = Arc<FutureMutex<ImapConnection>>;

#[derive(Debug)]
pub struct ImapBackend {
pub connection: Arc<FutureMutex<ImapConnection>>,
pub connection: ConnectionMutexType,
pub messages_fetch_connection: ConnectionMutexType,
pub server_conf: ImapServerConf,
}

Expand Down Expand Up @@ -235,6 +239,11 @@ pub fn create_connection(server_conf: &ImapServerConf, event_consumer: BackendEv

pub type ResultFuture<T> = Result<Pin<Box<dyn Future<Output = Result<T, MeliError>> + Send + 'static>>, MeliError>;

pub enum ConnectionType {
Backend,
MessagesFetch,
}

impl ImapBackend {
pub fn new(
server_hostname: String,
Expand All @@ -245,7 +254,6 @@ impl ImapBackend {
use_tls: bool,
use_starttls: bool,
danger_accept_invalid_certs: bool,
event_consumer: BackendEventConsumer,
) -> Result<Box<ImapBackend>, MeliError> {
let server_conf = ImapServerConf {
server_hostname,
Expand All @@ -266,13 +274,24 @@ impl ImapBackend {
};

Ok(Box::new(ImapBackend {
connection: Arc::new(FutureMutex::new(create_connection(&server_conf, event_consumer))),
connection: Arc::new(FutureMutex::new(create_connection(
&server_conf,
BackendEventConsumer::new(Arc::new(|_, _| {})),
))),
messages_fetch_connection: Arc::new(FutureMutex::new(create_connection(
&server_conf,
BackendEventConsumer::new(Arc::new(|_, _| {})),
))),
server_conf,
}))
}

pub fn is_online(&self) -> ResultFuture<()> {
let connection = self.connection.clone();
pub fn is_online(&self, connection_type: ConnectionType) -> ResultFuture<()> {
let connection = match connection_type {
ConnectionType::Backend => self.connection.clone(),
ConnectionType::MessagesFetch => self.messages_fetch_connection.clone(),
};

let timeout_dur = self.server_conf.timeout;
Ok(Box::pin(async move {
match timeout(timeout_dur, connection.lock()).await {
Expand Down Expand Up @@ -373,7 +392,7 @@ impl ImapBackend {
}

pub fn fetch_message_content(&self, imap_path: &String, uid: i64) -> ResultFuture<String> {
let connection_clone = self.connection.clone();
let connection_clone = self.messages_fetch_connection.clone();
let timeout_dur = self.server_conf.timeout;
let imap_path_clone = imap_path.clone();

Expand Down
7 changes: 3 additions & 4 deletions src/models/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ impl Identity {
true,
false,
true,
BackendEventConsumer::new(Arc::new(|_, _| {})),
)
.unwrap();

Expand Down Expand Up @@ -186,7 +185,7 @@ impl Identity {

async fn fetch_folders(self: Rc<Self>) -> Result<Vec<Box<melib::backends::imap::ImapMailbox>>, String> {
self.backend
.is_online()
.is_online(imap::ConnectionType::Backend)
.map_err(|e| e.to_string())?
.await
.map_err(|e| e.to_string())?;
Expand Down Expand Up @@ -326,7 +325,7 @@ impl Identity {

debug!("Syncing messages for folder {}, checking if online", folder.folder_name);
self.backend
.is_online()
.is_online(imap::ConnectionType::Backend)
.map_err(|e| e.to_string())?
.await
.map_err(|e| e.to_string())?;
Expand Down Expand Up @@ -406,7 +405,7 @@ impl Identity {
);

self.backend
.is_online()
.is_online(imap::ConnectionType::MessagesFetch)
.map_err(|e| e.to_string())?
.await
.map_err(|e| e.to_string())?;
Expand Down

0 comments on commit 57ee69e

Please sign in to comment.