Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/spark-mail-rs/src/store.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / spark-mail-rs / src / store.rs` 178 lines ยท 6071 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use crate::models::{EmailMessage, MailboxStatus}; 17 use std::fs; 18 use std::path::PathBuf; 19 20 #[derive(Clone, Debug)] 21 pub struct MailStore { 22 pub base_path: PathBuf, 23 } 24 25 impl MailStore { 26 pub fn new(custom_path: Option) -> Self { 27 let base_path = 28 custom_path.unwrap_or_else(|| dirs_or_home().join(".local/share/sovereign/mail")); 29 30 let store = Self { base_path }; 31 store.ensure_dirs(); 32 store 33 } 34 35 fn ensure_dirs(&self) { 36 let folders = ["inbox", "sent", "archive", "drafts"]; 37 for folder in &folders { 38 let p = self.base_path.join(folder); 39 let _ = fs::create_dir_all(&p); 40 } 41 } 42 43 pub fn save_message(&self, message: &EmailMessage) -> Result<(), String> { 44 self.ensure_dirs(); 45 let folder_path = self.base_path.join(&message.folder); 46 if !folder_path.exists() { 47 let _ = fs::create_dir_all(&folder_path); 48 } 49 let file_path = folder_path.join(format!("{}.json", message.id)); 50 let data = serde_json::to_string_pretty(message) 51 .map_err(|e| format!("Failed to serialize email: {}", e))?; 52 fs::write(&file_path, data).map_err(|e| format!("Failed to write email to disk: {}", e))?; 53 Ok(()) 54 } 55 56 pub fn get_message(&self, id: &str) -> Option { 57 let folders = ["inbox", "sent", "archive", "drafts"]; 58 for folder in &folders { 59 let file_path = self.base_path.join(folder).join(format!("{}.json", id)); 60 if file_path.exists() 61 && let Ok(data) = fs::read_to_string(&file_path) 62 && let Ok(msg) = serde_json::from_str::(&data) 63 { 64 return Some(msg); 65 } 66 } 67 None 68 } 69 70 pub fn list_folder(&self, folder: &str) -> Vec { 71 let folder_path = self.base_path.join(folder); 72 if !folder_path.exists() { 73 return Vec::new(); 74 } 75 76 let mut messages = Vec::new(); 77 if let Ok(entries) = fs::read_dir(&folder_path) { 78 for entry in entries.flatten() { 79 let path = entry.path(); 80 if path.extension().and_then(|s| s.to_str()) == Some("json") 81 && let Ok(data) = fs::read_to_string(&path) 82 && let Ok(msg) = serde_json::from_str::(&data) 83 { 84 messages.push(msg); 85 } 86 } 87 } 88 89 // Sort descending by received_at 90 messages.sort_by(|a, b| b.received_at.cmp(&a.received_at)); 91 messages 92 } 93 94 pub fn count_folder(&self, folder: &str) -> usize { 95 let folder_path = self.base_path.join(folder); 96 if let Ok(entries) = fs::read_dir(&folder_path) { 97 entries 98 .flatten() 99 .filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("json")) 100 .count() 101 } else { 102 0 103 } 104 } 105 106 pub fn get_status( 107 &self, 108 operator_email: &str, 109 smtp_port: u16, 110 api_port: u16, 111 cortex_ok: bool, 112 bridge_ok: bool, 113 ) -> MailboxStatus { 114 MailboxStatus { 115 inbox_count: self.count_folder("inbox"), 116 sent_count: self.count_folder("sent"), 117 archive_count: self.count_folder("archive"), 118 drafts_count: self.count_folder("drafts"), 119 storage_path: self.base_path.to_string_lossy().to_string(), 120 smtp_port, 121 api_port, 122 cortex_connected: cortex_ok, 123 proton_bridge_detected: bridge_ok, 124 operator_email: operator_email.to_string(), 125 } 126 } 127 } 128 129 fn dirs_or_home() -> PathBuf { 130 std::env::var("HOME") 131 .or_else(|_| std::env::var("USERPROFILE")) 132 .map(PathBuf::from) 133 .unwrap_or_else(|_| PathBuf::from(".")) 134 } 135 136 #[cfg(test)] 137 mod tests { 138 use super::*; 139 use crate::models::EmailMessage; 140 use std::collections::HashMap; 141 142 #[test] 143 fn test_mail_store_roundtrip() { 144 let temp_dir = std::env::temp_dir().join(format!("test_mail_{}", uuid::Uuid::new_v4())); 145 let store = MailStore::new(Some(temp_dir.clone())); 146 147 let msg = EmailMessage { 148 id: "test-msg-1".to_string(), 149 from: "alice@test.local".to_string(), 150 to: vec!["bob@test.local".to_string()], 151 subject: "Unit Test Subject".to_string(), 152 body: "Unit test body content.".to_string(), 153 headers: HashMap::new(), 154 received_at: "2026-09-18T17:00:00Z".to_string(), 155 folder: "inbox".to_string(), 156 cortex_indexed: false, 157 untrusted: true, 158 }; 159 160 store.save_message(&msg).expect("save should succeed"); 161 let retrieved = store 162 .get_message("test-msg-1") 163 .expect("message should be found"); 164 assert_eq!(retrieved.subject, "Unit Test Subject"); 165 assert_eq!(retrieved.from, "alice@test.local"); 166 assert!(retrieved.untrusted); 167 168 let list = store.list_folder("inbox"); 169 assert_eq!(list.len(), 1); 170 assert_eq!(list[0].id, "test-msg-1"); 171 172 let status = store.get_status("bob@test.local", 2525, 18092, true, false); 173 assert_eq!(status.inbox_count, 1); 174 assert_eq!(status.sent_count, 0); 175 176 let _ = fs::remove_dir_all(&temp_dir); 177 } 178 }