Переглянути джерело

send updates when new email detected and when an email was deleted

Yurii Sokolovskyi 1 місяць тому
батько
коміт
8649f69763
4 змінених файлів з 35 додано та 4 видалено
  1. 19 0
      src/api_broadcast_structure.rs
  2. 14 3
      src/imap.rs
  3. 1 0
      src/main.rs
  4. 1 1
      src/server.rs

+ 19 - 0
src/api_broadcast_structure.rs

@@ -0,0 +1,19 @@
+use serde::{Serialize, Deserialize};
+
+// Define the Message enum
+#[derive(Serialize, Deserialize)]
+#[serde(tag = "type")]
+pub enum Message {
+    NewEmail(NewEmail),
+    EmailRemoved(EmailRemoved),
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct NewEmail {
+    pub list: String
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct EmailRemoved {
+    pub list: String
+}

+ 14 - 3
src/imap.rs

@@ -35,6 +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 crate::server::broadcast_message;
 
 /// create TLS connect with the IMAP server
 pub async fn connect_to_imap() -> anyhow::Result<Client<TlsStream<TcpStream>>>{
@@ -194,6 +196,12 @@ pub async fn fetch_and_store_emails(session: &mut Session<TlsStream<TcpStream>>,
             }
 
             println!("Downloaded message with UID {}", uid.to_string());
+            
+            let message = Message::NewEmail(NewEmail {
+                list: list.clone()
+            });
+            let json_message = serde_json::to_string(&message).unwrap();
+            broadcast_message(json_message).await;
         } else {
             return Err(anyhow!("No message found with the given UID"));
         }
@@ -326,6 +334,12 @@ pub async fn remove_deleted_emails(mailbox: String) -> anyhow::Result<u32> {
             Some(message) => delete_email(message.id).await.unwrap()
         }
     }
+
+    let message = Message::EmailRemoved(EmailRemoved {
+        list: mailbox.clone()
+    });
+    let json_message = serde_json::to_string(&message).unwrap();
+    broadcast_message(json_message).await;
     
     Ok(deleted_uids.len() as u32)
 }
@@ -378,9 +392,6 @@ pub fn check_for_updates(mailbox: String) -> JoinHandle<Result<(), anyhow::Error
                 }
             }
 
-            // Ensure the idle handle is dropped before the next loop iteration
-            // idle_handle.await.unwrap();
-
             // Reassign session to prevent ownership issues in the next loop iteration
             session = idle.done().await.unwrap();
         }

+ 1 - 0
src/main.rs

@@ -39,6 +39,7 @@ mod imap;
 mod server;
 mod js;
 mod sonic;
+mod api_broadcast_structure;
 
 struct ImapTaskStorage {
     downloading: Option<JoinHandle<()>>,

+ 1 - 1
src/server.rs

@@ -160,7 +160,7 @@ async fn ws_handler(ws: WebSocket) {
 }
 
 
-async fn broadcast_message(message: String) {
+pub async fn broadcast_message(message: String) {
     let msg = Message::text(message);
 
     let client_guard = CLIENT.lock().unwrap();