Преглед на файлове

EmailStartDownload and EmailStopDownload added

Yurii Sokolovskyi преди 3 седмици
родител
ревизия
f5c08eef2e
променени са 3 файла, в които са добавени 34 реда и са изтрити 2 реда
  1. 1 0
      Cargo.toml
  2. 14 0
      src/api_broadcast_structure.rs
  3. 19 2
      src/imap.rs

+ 1 - 0
Cargo.toml

@@ -36,6 +36,7 @@ lettre = {version = "0.11.7", default-features = false, features = ["builder"]}
 tar = "0.4"
 flate2 = "1.0"
 futures = "0.3.30"
+uuid = { version = "1.11.0", features = ["v4"] }
 
 [target.'cfg(target_os = "wasi")'.dependencies]
 tokio_wasi = {version = "1.25.2", features = ["full", "rt", "rt-multi-thread", "macros", "time"] }

+ 14 - 0
src/api_broadcast_structure.rs

@@ -6,6 +6,8 @@ use serde::{Serialize, Deserialize};
 pub enum Message {
     NewEmail(NewEmail),
     EmailRemoved(EmailRemoved),
+    EmailStartDownload(EmailStartDownload),
+    EmailStopDownload(EmailStopDownload)
 }
 
 #[derive(Serialize, Deserialize)]
@@ -13,6 +15,18 @@ pub struct NewEmail {
     pub list: String
 }
 
+#[derive(Serialize, Deserialize)]
+pub struct EmailStartDownload {
+    pub list: String,
+    pub broadcast_id: String
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct EmailStopDownload {
+    pub list: String,
+    pub broadcast_id: String
+}
+
 #[derive(Serialize, Deserialize)]
 pub struct EmailRemoved {
     pub list: String

+ 19 - 2
src/imap.rs

@@ -35,7 +35,8 @@ use async_imap_wasi::{Client, Session};
 use async_imap_wasi::extensions::idle::IdleResponse::NewData;
 #[cfg(target_os = "wasi")]
 use async_imap_wasi::types::Uid;
-use crate::api_broadcast_structure::{EmailRemoved, Message, NewEmail};
+use uuid::Uuid;
+use crate::api_broadcast_structure::{EmailRemoved, EmailStartDownload, EmailStopDownload, Message, NewEmail};
 use crate::server::broadcast_message;
 
 /// create TLS connect with the IMAP server
@@ -131,7 +132,15 @@ pub async fn list_imap_folders(session: &mut Session<TlsStream<TcpStream>>) -> a
 
 /// download all emails from one mailbox 
 pub async fn fetch_and_store_emails(session: &mut Session<TlsStream<TcpStream>>, list: String) -> anyhow::Result<Vec<(u32, PathBuf)>> {
-    // TODO send a message to UI about start and end of the download
+    // send a message to UI about start and end of the download
+    let broadcast_id = Uuid::new_v4().to_string();
+    let message = Message::EmailStartDownload(EmailStartDownload {
+        list: list.clone(),
+        broadcast_id: broadcast_id.clone().to_string()
+    });
+    let json_message = serde_json::to_string(&message).unwrap();
+    broadcast_message(json_message).await;
+    
     let out_dir_name = Config::global().out_dir.clone().join(list.clone());
     if !out_dir_name.exists() { create_dir_all(out_dir_name).ok(); }
     
@@ -211,6 +220,14 @@ pub async fn fetch_and_store_emails(session: &mut Session<TlsStream<TcpStream>>,
         }
     }
 
+    // send a message to UI about start and end of the download
+    let message = Message::EmailStopDownload(EmailStopDownload {
+        list: list.clone(),
+        broadcast_id: broadcast_id.clone().to_string()
+    });
+    let json_message = serde_json::to_string(&message).unwrap();
+    broadcast_message(json_message).await;
+    
     Ok(stored_paths)
 }