Skip to content

Commit 9d8b9be

Browse files
committed
chore: deep code cleanup, remove redundant blank lines and satisfy linters
1 parent b2352a3 commit 9d8b9be

File tree

2 files changed

+406
-534
lines changed

2 files changed

+406
-534
lines changed

ratatoskr-desktop/src-tauri/src/lib.rs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,19 @@ async fn get_messages(
3737
}
3838

3939
#[tauri::command]
40-
4140
async fn send_message(
4241
state: State<'_, AppState>,
43-
4442
recipient_did: String,
45-
4643
content: String,
47-
4844
msg_type_str: String,
49-
5045
reply_to_id: Option<String>,
5146
) -> Result<(), String> {
5247
use ratatoskr_core::models::{ChatMessage, MessageStatus, MessageType};
5348

5449
let msg_type = match msg_type_str.as_str() {
5550
"Ephemeral" => MessageType::Ephemeral,
56-
5751
"Transactional" => MessageType::Transactional,
58-
5952
"Feed" => MessageType::Feed,
60-
6153
_ => MessageType::Direct,
6254
};
6355

@@ -67,7 +59,6 @@ async fn send_message(
6759
.as_secs();
6860

6961
// Calculate TTL (60 seconds for Ephemeral messages for testing)
70-
7162
let ttl = if matches!(msg_type, MessageType::Ephemeral) {
7263
Some(now + 60)
7364
} else {
@@ -86,7 +77,7 @@ async fn send_message(
8677
// 1. Create message object
8778
let msg = ChatMessage {
8879
id: uuid::Uuid::new_v4().to_string(),
89-
sender_did: sender_did.clone(), // Use real DID
80+
sender_did: sender_did.clone(),
9081
recipient_did,
9182
msg_type,
9283
status: MessageStatus::Unread,
@@ -98,8 +89,6 @@ async fn send_message(
9889
};
9990

10091
// 2. Save to local DB
101-
// Note: When saving to OUR db, we might want to keep 'sender_did' as our DID so we know we sent it.
102-
// Or we rely on the fact that if sender_did == my_did, it's an outgoing message.
10392
state
10493
.storage
10594
.save_message(&msg)
@@ -112,7 +101,7 @@ async fn send_message(
112101
state.network_sender.lock().await;
113102
sender
114103
.send(NetworkCommand::BroadcastSos(packet_bytes))
115-
.await // Reuse broadcast for now
104+
.await
116105
.map_err(|e| e.to_string())?;
117106

118107
Ok(())
@@ -246,8 +235,6 @@ async fn delete_identity(state: State<'_, AppState>) -> Result<(), String> {
246235

247236
#[tauri::command]
248237
async fn export_backup(app_handle: tauri::AppHandle, content: String) -> Result<String, String> {
249-
// use tauri::path::BaseDirectory; // Removed unused import
250-
251238
let download_dir = app_handle
252239
.path()
253240
.download_dir()
@@ -277,7 +264,6 @@ async fn send_sos(
277264
long: f64,
278265
description: String,
279266
) -> Result<String, String> {
280-
// 1. Form the Payload (as before)
281267
let sos_type = match help_type.as_str() {
282268
"Medical" => SosType::Medical,
283269
"Evacuation" => SosType::Evacuation,
@@ -301,16 +287,13 @@ async fn send_sos(
301287
timestamp: now,
302288
};
303289

304-
// 2. Encrypt (Organization Key)
305290
let trusted_key = [7u8; 32];
306291
let packet = encrypt_sos_signal(&payload, &trusted_key)
307292
.map_err(|e| format!("Encryption Error: {}", e))?;
308293

309-
// 3. Serialize the packet for network transmission (to bytes)
310294
let packet_bytes =
311295
serde_json::to_vec(&packet).map_err(|e| format!("Serialization Error: {}", e))?;
312296

313-
// 4. Send the command to the network thread
314297
let sender: tokio::sync::MutexGuard<mpsc::Sender<NetworkCommand>> =
315298
state.network_sender.lock().await;
316299
match sender
@@ -327,15 +310,12 @@ async fn send_sos(
327310

328311
#[cfg_attr(mobile, tauri::mobile_entry_point)]
329312
pub fn run() {
330-
// Create channel: UI -> Network
331313
let (tx, rx) = mpsc::channel(32);
332314

333315
tauri::Builder::default()
334316
.plugin(tauri_plugin_opener::init())
335317
.setup(move |app| {
336318
let app_handle = app.handle();
337-
338-
// Allow overriding config dir via ENV for multi-instance testing
339319
let config_dir = if let Ok(custom_path) = std::env::var("RATATOSKR_CONFIG_DIR") {
340320
println!("Using custom config dir: {}", custom_path);
341321
PathBuf::from(custom_path)
@@ -350,24 +330,21 @@ pub fn run() {
350330
let identity_path = config_dir.join("identity.key");
351331
let db_path = config_dir.join("ratatoskr.db");
352332

353-
// Init DB (blocking for safety during startup)
354333
let storage = tauri::async_runtime::block_on(async {
355334
Storage::init(&db_path).await.expect("Failed to init DB")
356335
});
357-
let storage_arc = Arc::new(storage); // Clone for event loop
336+
let storage_arc = Arc::new(storage);
358337

359338
app.manage(AppState {
360339
network_sender: Mutex::new(tx),
361340
identity_path: identity_path.clone(),
362341
storage: storage_arc.clone(),
363342
});
364343

365-
// Channel for Network -> UI events
366344
let (event_tx, mut event_rx) = mpsc::channel(32);
367345
let app_handle_clone = app_handle.clone();
368346
let storage_for_events = storage_arc.clone();
369347

370-
// Event Loop: Network -> UI
371348
tauri::async_runtime::spawn(async move {
372349
while let Some(event) = event_rx.recv().await {
373350
match event {
@@ -380,27 +357,21 @@ pub fn run() {
380357
sender: _,
381358
} => {
382359
println!("UI: Received message on topic {}", topic);
383-
384-
// 1. Try to deserialize (assume it is a ChatMessage)
385360
if let Ok(mut msg) = serde_json::from_slice::<
386361
ratatoskr_core::models::ChatMessage,
387362
>(&payload)
388363
{
389-
// 2. Save to DB
390-
msg.status = ratatoskr_core::models::MessageStatus::Unread; // Mark as unread
364+
msg.status = ratatoskr_core::models::MessageStatus::Unread;
391365
if let Err(e) = storage_for_events.save_message(&msg).await {
392366
eprintln!("Failed to save incoming message: {}", e);
393367
}
394-
395-
// 3. Emit to UI
396368
let _ = app_handle_clone.emit("msg-received", msg);
397369
}
398370
}
399371
}
400372
}
401373
});
402374

403-
// Garbage Collector: Periodically delete expired messages
404375
let storage_gc = storage_arc.clone();
405376
tauri::async_runtime::spawn(async move {
406377
let mut interval = tokio::time::interval(std::time::Duration::from_secs(30));
@@ -416,9 +387,7 @@ pub fn run() {
416387
}
417388
});
418389

419-
// Start P2P node in a separate background thread
420390
tauri::async_runtime::spawn(async move {
421-
// Load or generate temporary identity for the network
422391
let local_key = if identity_path.exists() {
423392
match KeyVault::load_from_file(&identity_path) {
424393
Ok(vault) => {
@@ -434,14 +403,11 @@ pub fn run() {
434403
match build_swarm(local_key).await {
435404
Ok(swarm) => {
436405
println!("Desktop P2P Node Started");
437-
438-
// Better approach: Dial directly on swarm before running loop
439406
let mut active_swarm = swarm;
440407
if let Ok(addr) = "/ip4/127.0.0.1/tcp/4001".parse::<Multiaddr>() {
441408
println!("Bootstrapping: Connecting to local relay...");
442409
let _ = active_swarm.dial(addr);
443410
}
444-
445411
if let Err(e) = run_network_node(active_swarm, rx, event_tx).await {
446412
eprintln!("Network node crashed: {}", e);
447413
}

0 commit comments

Comments
 (0)