Skip to content

Commit 57ee69e

Browse files
committed
Implement secondary connection for messages
1 parent d26101e commit 57ee69e

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/backends/imap/imap.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use crate::models;
55

6+
use diesel::Connection;
67
use futures::Future;
78
use melib;
89
use melib::backends::imap::{
@@ -20,9 +21,12 @@ use std::time::{Duration, SystemTime};
2021

2122
use std::pin::Pin;
2223

24+
type ConnectionMutexType = Arc<FutureMutex<ImapConnection>>;
25+
2326
#[derive(Debug)]
2427
pub struct ImapBackend {
25-
pub connection: Arc<FutureMutex<ImapConnection>>,
28+
pub connection: ConnectionMutexType,
29+
pub messages_fetch_connection: ConnectionMutexType,
2630
pub server_conf: ImapServerConf,
2731
}
2832

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

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

242+
pub enum ConnectionType {
243+
Backend,
244+
MessagesFetch,
245+
}
246+
238247
impl ImapBackend {
239248
pub fn new(
240249
server_hostname: String,
@@ -245,7 +254,6 @@ impl ImapBackend {
245254
use_tls: bool,
246255
use_starttls: bool,
247256
danger_accept_invalid_certs: bool,
248-
event_consumer: BackendEventConsumer,
249257
) -> Result<Box<ImapBackend>, MeliError> {
250258
let server_conf = ImapServerConf {
251259
server_hostname,
@@ -266,13 +274,24 @@ impl ImapBackend {
266274
};
267275

268276
Ok(Box::new(ImapBackend {
269-
connection: Arc::new(FutureMutex::new(create_connection(&server_conf, event_consumer))),
277+
connection: Arc::new(FutureMutex::new(create_connection(
278+
&server_conf,
279+
BackendEventConsumer::new(Arc::new(|_, _| {})),
280+
))),
281+
messages_fetch_connection: Arc::new(FutureMutex::new(create_connection(
282+
&server_conf,
283+
BackendEventConsumer::new(Arc::new(|_, _| {})),
284+
))),
270285
server_conf,
271286
}))
272287
}
273288

274-
pub fn is_online(&self) -> ResultFuture<()> {
275-
let connection = self.connection.clone();
289+
pub fn is_online(&self, connection_type: ConnectionType) -> ResultFuture<()> {
290+
let connection = match connection_type {
291+
ConnectionType::Backend => self.connection.clone(),
292+
ConnectionType::MessagesFetch => self.messages_fetch_connection.clone(),
293+
};
294+
276295
let timeout_dur = self.server_conf.timeout;
277296
Ok(Box::pin(async move {
278297
match timeout(timeout_dur, connection.lock()).await {
@@ -373,7 +392,7 @@ impl ImapBackend {
373392
}
374393

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

src/models/identity.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ impl Identity {
5555
true,
5656
false,
5757
true,
58-
BackendEventConsumer::new(Arc::new(|_, _| {})),
5958
)
6059
.unwrap();
6160

@@ -186,7 +185,7 @@ impl Identity {
186185

187186
async fn fetch_folders(self: Rc<Self>) -> Result<Vec<Box<melib::backends::imap::ImapMailbox>>, String> {
188187
self.backend
189-
.is_online()
188+
.is_online(imap::ConnectionType::Backend)
190189
.map_err(|e| e.to_string())?
191190
.await
192191
.map_err(|e| e.to_string())?;
@@ -326,7 +325,7 @@ impl Identity {
326325

327326
debug!("Syncing messages for folder {}, checking if online", folder.folder_name);
328327
self.backend
329-
.is_online()
328+
.is_online(imap::ConnectionType::Backend)
330329
.map_err(|e| e.to_string())?
331330
.await
332331
.map_err(|e| e.to_string())?;
@@ -406,7 +405,7 @@ impl Identity {
406405
);
407406

408407
self.backend
409-
.is_online()
408+
.is_online(imap::ConnectionType::MessagesFetch)
410409
.map_err(|e| e.to_string())?
411410
.await
412411
.map_err(|e| e.to_string())?;

0 commit comments

Comments
 (0)