|
| 1 | +use std::fs; |
| 2 | +use std::path::{Path}; |
| 3 | +use serde_json::{Value, json}; |
| 4 | +use std::io::{self, Write}; |
| 5 | +use crate::commands::conversation::ThreadArgs; |
| 6 | + |
| 7 | +pub fn resolve_target_thread_id( |
| 8 | + index: &Value, |
| 9 | + args: &ThreadArgs, |
| 10 | +) -> Option<String> { |
| 11 | + let empty_vec: Vec<Value> = Vec::new(); |
| 12 | + let threads: Vec<String> = index["threads"] |
| 13 | + .as_array() |
| 14 | + .unwrap_or(&empty_vec) |
| 15 | + .iter() |
| 16 | + .filter_map(|t| t.as_str().map(|s| s.to_string())) |
| 17 | + .collect(); |
| 18 | + |
| 19 | + // If ID prefix provided |
| 20 | + if let Some(prefix) = &args.id { |
| 21 | + let matches: Vec<&String> = threads |
| 22 | + .iter() |
| 23 | + .filter(|tid| tid.starts_with(prefix)) |
| 24 | + .collect(); |
| 25 | + |
| 26 | + return match matches.as_slice() { |
| 27 | + [] => { |
| 28 | + eprintln!("❌ No conversation matches '{}'", prefix); |
| 29 | + None |
| 30 | + } |
| 31 | + [single] => Some((*single).clone()), |
| 32 | + _ => { |
| 33 | + eprintln!("❌ Ambiguous prefix '{}': {:?}", prefix, matches); |
| 34 | + None |
| 35 | + } |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + // Otherwise use active thread |
| 40 | + let active = index["active_thread"].as_str().unwrap_or("").to_string(); |
| 41 | + if active.is_empty() { |
| 42 | + eprintln!("❌ No active conversation to delete."); |
| 43 | + return None; |
| 44 | + } |
| 45 | + |
| 46 | + Some(active) |
| 47 | +} |
| 48 | + |
| 49 | +pub fn confirm_delete_primary() -> bool { |
| 50 | + |
| 51 | + println!("Are you sure you want to delete this conversation? (y/N)"); |
| 52 | + print!("> "); |
| 53 | + io::stdout().flush().unwrap(); |
| 54 | + |
| 55 | + let mut input = String::new(); |
| 56 | + io::stdin().read_line(&mut input).unwrap(); |
| 57 | + |
| 58 | + input.trim().to_lowercase() == "y" |
| 59 | +} |
| 60 | + |
| 61 | +pub fn confirm_delete_destructive() -> bool { |
| 62 | + |
| 63 | + println!(); |
| 64 | + println!( |
| 65 | + "\x1b[31m⚠️ Reminder: deleting a conversation is a destructive action.\n\ |
| 66 | + It cannot be reversed unless the project is version-controlled (git).\x1b[0m" |
| 67 | + ); |
| 68 | + println!(); |
| 69 | + println!("Type DELETE to confirm:"); |
| 70 | + print!("> "); |
| 71 | + io::stdout().flush().unwrap(); |
| 72 | + |
| 73 | + let mut input = String::new(); |
| 74 | + io::stdin().read_line(&mut input).unwrap(); |
| 75 | + |
| 76 | + input.trim() == "DELETE" |
| 77 | +} |
| 78 | + |
| 79 | +pub fn perform_conversation_deletion( |
| 80 | + index: &mut Value, |
| 81 | + fur_dir: &Path, |
| 82 | + target_tid: &str, |
| 83 | + threads: &[String], |
| 84 | +) { |
| 85 | + let convo_path = fur_dir.join("threads").join(format!("{}.json", target_tid)); |
| 86 | + |
| 87 | + // Load convo to extract message IDs + title |
| 88 | + let convo_content = fs::read_to_string(&convo_path) |
| 89 | + .expect("Failed to load conversation JSON."); |
| 90 | + let convo: Value = serde_json::from_str(&convo_content).unwrap(); |
| 91 | + |
| 92 | + let title = convo["title"].as_str().unwrap_or("Untitled"); |
| 93 | + let msg_ids: Vec<String> = convo["messages"] |
| 94 | + .as_array() |
| 95 | + .unwrap_or(&vec![]) |
| 96 | + .iter() |
| 97 | + .filter_map(|v| v.as_str().map(|s| s.to_string())) |
| 98 | + .collect(); |
| 99 | + |
| 100 | + println!( |
| 101 | + "🗑️ Deleting conversation {} \"{}\"...", |
| 102 | + &target_tid[..8], |
| 103 | + title |
| 104 | + ); |
| 105 | + |
| 106 | + // 1. Delete conversation JSON |
| 107 | + let _ = fs::remove_file(&convo_path); |
| 108 | + |
| 109 | + // 2. Delete message files and markdown attachments |
| 110 | + for mid in msg_ids { |
| 111 | + let msg_path = fur_dir.join("messages").join(format!("{}.json", mid)); |
| 112 | + |
| 113 | + if let Ok(content) = fs::read_to_string(&msg_path) { |
| 114 | + if let Ok(msg_json) = serde_json::from_str::<Value>(&content) { |
| 115 | + if let Some(md_raw) = msg_json["markdown"].as_str() { |
| 116 | + let md_path = Path::new(md_raw); |
| 117 | + if md_path.is_absolute() { |
| 118 | + let _ = fs::remove_file(md_path); |
| 119 | + } else { |
| 120 | + let _ = fs::remove_file(Path::new(".").join(md_raw)); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + let _ = fs::remove_file(&msg_path); |
| 127 | + } |
| 128 | + |
| 129 | + // 3. Update index.json (remove thread) |
| 130 | + let new_threads: Vec<String> = threads |
| 131 | + .iter() |
| 132 | + .filter(|tid| tid.as_str() != target_tid) |
| 133 | + .cloned() |
| 134 | + .collect(); |
| 135 | + |
| 136 | + index["threads"] = json!(new_threads); |
| 137 | + |
| 138 | + // 4. Clear active thread if it matches deleted |
| 139 | + if index["active_thread"].as_str() == Some(target_tid) { |
| 140 | + index["active_thread"] = Value::Null; |
| 141 | + index["current_message"] = Value::Null; |
| 142 | + } |
| 143 | + |
| 144 | + // 5. Save index.json |
| 145 | + let index_path = fur_dir.join("index.json"); |
| 146 | + fs::write(&index_path, serde_json::to_string_pretty(&index).unwrap()).unwrap(); |
| 147 | + |
| 148 | + println!("✔️ Conversation deleted successfully."); |
| 149 | +} |
0 commit comments