aien-sovereign-core
Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark
Brought by drakestapleton
aien-sovereign-core / crates / spark-mail-rs / src / store.rs
| 1 | use crate::models::{EmailMessage, MailboxStatus}; |
| 2 | use std::fs; |
| 3 | use std::path::PathBuf; |
| 4 | |
| 5 | #[derive(Clone, Debug)] |
| 6 | pub struct MailStore { |
| 7 | pub base_path: PathBuf, |
| 8 | } |
| 9 | |
| 10 | impl MailStore { |
| 11 | pub fn new(custom_path: Option<PathBuf>) -> Self { |
| 12 | let base_path = custom_path.unwrap_or_else(|| { |
| 13 | dirs_or_home().join(".local/share/sovereign/mail") |
| 14 | }); |
| 15 | |
| 16 | let store = Self { base_path }; |
| 17 | store.ensure_dirs(); |
| 18 | store |
| 19 | } |
| 20 | |
| 21 | fn ensure_dirs(&self) { |
| 22 | let folders = ["inbox", "sent", "archive", "drafts"]; |
| 23 | for folder in &folders { |
| 24 | let p = self.base_path.join(folder); |
| 25 | let _ = fs::create_dir_all(&p); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | pub fn save_message(&self, message: &EmailMessage) -> Result<(), String> { |
| 30 | self.ensure_dirs(); |
| 31 | let folder_path = self.base_path.join(&message.folder); |
| 32 | if !folder_path.exists() { |
| 33 | let _ = fs::create_dir_all(&folder_path); |
| 34 | } |
| 35 | let file_path = folder_path.join(format!("{}.json", message.id)); |
| 36 | let data = serde_json::to_string_pretty(message) |
| 37 | .map_err(|e| format!("Failed to serialize email: {}", e))?; |
| 38 | fs::write(&file_path, data) |
| 39 | .map_err(|e| format!("Failed to write email to disk: {}", e))?; |
| 40 | Ok(()) |
| 41 | } |
| 42 | |
| 43 | pub fn get_message(&self, id: &str) -> Option<EmailMessage> { |
| 44 | let folders = ["inbox", "sent", "archive", "drafts"]; |
| 45 | for folder in &folders { |
| 46 | let file_path = self.base_path.join(folder).join(format!("{}.json", id)); |
| 47 | if file_path.exists() { |
| 48 | if let Ok(data) = fs::read_to_string(&file_path) { |
| 49 | if let Ok(msg) = serde_json::from_str::<EmailMessage>(&data) { |
| 50 | return Some(msg); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | None |
| 56 | } |
| 57 | |
| 58 | pub fn list_folder(&self, folder: &str) -> Vec<EmailMessage> { |
| 59 | let folder_path = self.base_path.join(folder); |
| 60 | if !folder_path.exists() { |
| 61 | return Vec::new(); |
| 62 | } |
| 63 | |
| 64 | let mut messages = Vec::new(); |
| 65 | if let Ok(entries) = fs::read_dir(&folder_path) { |
| 66 | for entry in entries.flatten() { |
| 67 | let path = entry.path(); |
| 68 | if path.extension().and_then(|s| s.to_str()) == Some("json") { |
| 69 | if let Ok(data) = fs::read_to_string(&path) { |
| 70 | if let Ok(msg) = serde_json::from_str::<EmailMessage>(&data) { |
| 71 | messages.push(msg); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Sort descending by received_at |
| 79 | messages.sort_by(|a, b| b.received_at.cmp(&a.received_at)); |
| 80 | messages |
| 81 | } |
| 82 | |
| 83 | pub fn count_folder(&self, folder: &str) -> usize { |
| 84 | let folder_path = self.base_path.join(folder); |
| 85 | if let Ok(entries) = fs::read_dir(&folder_path) { |
| 86 | entries.flatten().filter(|e| { |
| 87 | e.path().extension().and_then(|s| s.to_str()) == Some("json") |
| 88 | }).count() |
| 89 | } else { |
| 90 | 0 |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | pub fn get_status( |
| 95 | &self, |
| 96 | operator_email: &str, |
| 97 | smtp_port: u16, |
| 98 | api_port: u16, |
| 99 | cortex_ok: bool, |
| 100 | bridge_ok: bool, |
| 101 | ) -> MailboxStatus { |
| 102 | MailboxStatus { |
| 103 | inbox_count: self.count_folder("inbox"), |
| 104 | sent_count: self.count_folder("sent"), |
| 105 | archive_count: self.count_folder("archive"), |
| 106 | drafts_count: self.count_folder("drafts"), |
| 107 | storage_path: self.base_path.to_string_lossy().to_string(), |
| 108 | smtp_port, |
| 109 | api_port, |
| 110 | cortex_connected: cortex_ok, |
| 111 | proton_bridge_detected: bridge_ok, |
| 112 | operator_email: operator_email.to_string(), |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | fn dirs_or_home() -> PathBuf { |
| 118 | if let Ok(home) = std::env::var("HOME") { |
| 119 | PathBuf::from(home) |
| 120 | } else { |
| 121 | PathBuf::from("/home/drakestapleton") |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | #[cfg(test)] |
| 126 | mod tests { |
| 127 | use super::*; |
| 128 | use crate::models::EmailMessage; |
| 129 | use std::collections::HashMap; |
| 130 | |
| 131 | #[test] |
| 132 | fn test_mail_store_roundtrip() { |
| 133 | let temp_dir = std::env::temp_dir().join(format!("test_mail_{}", uuid::Uuid::new_v4())); |
| 134 | let store = MailStore::new(Some(temp_dir.clone())); |
| 135 | |
| 136 | let msg = EmailMessage { |
| 137 | id: "test-msg-1".to_string(), |
| 138 | from: "alice@test.local".to_string(), |
| 139 | to: vec!["bob@test.local".to_string()], |
| 140 | subject: "Unit Test Subject".to_string(), |
| 141 | body: "Unit test body content.".to_string(), |
| 142 | headers: HashMap::new(), |
| 143 | received_at: "2026-09-18T17:00:00Z".to_string(), |
| 144 | folder: "inbox".to_string(), |
| 145 | cortex_indexed: false, |
| 146 | }; |
| 147 | |
| 148 | store.save_message(&msg).expect("save should succeed"); |
| 149 | let retrieved = store.get_message("test-msg-1").expect("message should be found"); |
| 150 | assert_eq!(retrieved.subject, "Unit Test Subject"); |
| 151 | assert_eq!(retrieved.from, "alice@test.local"); |
| 152 | |
| 153 | let list = store.list_folder("inbox"); |
| 154 | assert_eq!(list.len(), 1); |
| 155 | assert_eq!(list[0].id, "test-msg-1"); |
| 156 | |
| 157 | let status = store.get_status("bob@test.local", 2525, 18092, true, false); |
| 158 | assert_eq!(status.inbox_count, 1); |
| 159 | assert_eq!(status.sent_count, 0); |
| 160 | |
| 161 | let _ = fs::remove_dir_all(&temp_dir); |
| 162 | } |
| 163 | } |