chore: rename sessions to chats

This commit is contained in:
kaichaosun 2026-04-17 14:30:30 +08:00
parent 9e0800ef1d
commit 3b4e93b88e
No known key found for this signature in database
GPG Key ID: 223E0F992F4F03BF

View File

@ -31,7 +31,7 @@ pub struct ChatSession {
#[derive(Debug, Default, Serialize, Deserialize)] #[derive(Debug, Default, Serialize, Deserialize)]
pub struct AppState { pub struct AppState {
/// Map from remote username to chat session. /// Map from remote username to chat session.
pub sessions: HashMap<String, ChatSession>, pub chats: HashMap<String, ChatSession>,
/// Currently active chat (remote username). /// Currently active chat (remote username).
pub active_chat: Option<String>, pub active_chat: Option<String>,
} }
@ -84,7 +84,7 @@ impl ChatApp {
let state = Self::load_state(&state_path); let state = Self::load_state(&state_path);
// Count existing chats // Count existing chats
let chat_count = state.sessions.len(); let chat_count = state.chats.len();
let status = if chat_count > 0 { let status = if chat_count > 0 {
format!( format!(
"Welcome back, {}! {} chat(s) loaded. Type /help for commands.", "Welcome back, {}! {} chat(s) loaded. Type /help for commands.",
@ -130,7 +130,7 @@ impl ChatApp {
self.state self.state
.active_chat .active_chat
.as_ref() .as_ref()
.and_then(|name| self.state.sessions.get(name)) .and_then(|name| self.state.chats.get(name))
} }
/// Get the current messages to display. /// Get the current messages to display.
@ -155,7 +155,7 @@ impl ChatApp {
/// Connect to another user using their introduction bundle. /// Connect to another user using their introduction bundle.
pub fn connect(&mut self, remote_user: &str, bundle_str: &str) -> Result<()> { pub fn connect(&mut self, remote_user: &str, bundle_str: &str) -> Result<()> {
// Check if we already have a chat with this user // Check if we already have a chat with this user
if self.state.sessions.contains_key(remote_user) { if self.state.chats.contains_key(remote_user) {
return Err(anyhow::anyhow!( return Err(anyhow::anyhow!(
"Already have a chat with {}. Use /switch {} to switch to it.", "Already have a chat with {}. Use /switch {} to switch to it.",
remote_user, remote_user,
@ -187,7 +187,7 @@ impl ChatApp {
timestamp: now(), timestamp: now(),
}); });
self.state.sessions.insert(remote_user.to_string(), session); self.state.chats.insert(remote_user.to_string(), session);
self.state.active_chat = Some(remote_user.to_string()); self.state.active_chat = Some(remote_user.to_string());
self.save_state()?; self.save_state()?;
@ -197,7 +197,7 @@ impl ChatApp {
/// Switch to a different chat. /// Switch to a different chat.
pub fn switch_chat(&mut self, remote_user: &str) -> Result<()> { pub fn switch_chat(&mut self, remote_user: &str) -> Result<()> {
if self.state.sessions.contains_key(remote_user) { if self.state.chats.contains_key(remote_user) {
self.state.active_chat = Some(remote_user.to_string()); self.state.active_chat = Some(remote_user.to_string());
self.save_state()?; self.save_state()?;
self.status = format!("Switched to chat with {}", remote_user); self.status = format!("Switched to chat with {}", remote_user);
@ -212,7 +212,7 @@ impl ChatApp {
/// Delete a chat session. /// Delete a chat session.
pub fn delete_chat(&mut self, remote_user: &str) -> Result<()> { pub fn delete_chat(&mut self, remote_user: &str) -> Result<()> {
if let Some(_session) = self.state.sessions.remove(remote_user) { if let Some(_session) = self.state.chats.remove(remote_user) {
// TODO delete not implemented in libchat // TODO delete not implemented in libchat
// Also delete from the library's storage // Also delete from the library's storage
// if let Err(e) = self.manager.delete_chat(&session.chat_id) { // if let Err(e) = self.manager.delete_chat(&session.chat_id) {
@ -223,7 +223,7 @@ impl ChatApp {
// If we deleted the active chat, clear it // If we deleted the active chat, clear it
if self.state.active_chat.as_deref() == Some(remote_user) { if self.state.active_chat.as_deref() == Some(remote_user) {
// Switch to another chat if available, otherwise clear // Switch to another chat if available, otherwise clear
self.state.active_chat = self.state.sessions.keys().next().cloned(); self.state.active_chat = self.state.chats.keys().next().cloned();
} }
self.save_state()?; self.save_state()?;
@ -247,7 +247,7 @@ impl ChatApp {
let session = self let session = self
.state .state
.sessions .chats
.get(&active) .get(&active)
.ok_or_else(|| anyhow::anyhow!("Chat session not found"))?; .ok_or_else(|| anyhow::anyhow!("Chat session not found"))?;
@ -261,7 +261,7 @@ impl ChatApp {
} }
// Update messages // Update messages
if let Some(session) = self.state.sessions.get_mut(&active) { if let Some(session) = self.state.chats.get_mut(&active) {
session.messages.push(DisplayMessage { session.messages.push(DisplayMessage {
from_self: true, from_self: true,
content: content.to_string(), content: content.to_string(),
@ -293,21 +293,21 @@ impl ChatApp {
let chat_id = content.conversation_id.clone(); let chat_id = content.conversation_id.clone();
// Find or create session for this user // Find or create session for this user
if !self.state.sessions.contains_key(from_user) { if !self.state.chats.contains_key(from_user) {
// New chat from someone // New chat from someone
let session = ChatSession { let session = ChatSession {
chat_id: chat_id.clone(), chat_id: chat_id.clone(),
remote_user: from_user.clone(), remote_user: from_user.clone(),
messages: Vec::new(), messages: Vec::new(),
}; };
self.state.sessions.insert(from_user.clone(), session); self.state.chats.insert(from_user.clone(), session);
self.state.active_chat = Some(from_user.clone()); self.state.active_chat = Some(from_user.clone());
self.status = format!("New chat from {}!", from_user); self.status = format!("New chat from {}!", from_user);
} }
let message = String::from_utf8_lossy(&content.data).to_string(); let message = String::from_utf8_lossy(&content.data).to_string();
if !message.is_empty() if !message.is_empty()
&& let Some(session) = self.state.sessions.get_mut(from_user) && let Some(session) = self.state.chats.get_mut(from_user)
{ {
session.messages.push(DisplayMessage { session.messages.push(DisplayMessage {
from_self: false, from_self: false,
@ -334,7 +334,7 @@ impl ChatApp {
}; };
if let Some(active) = &self.state.active_chat.clone() if let Some(active) = &self.state.active_chat.clone()
&& let Some(session) = self.state.sessions.get_mut(active) && let Some(session) = self.state.chats.get_mut(active)
{ {
session.messages.push(msg); session.messages.push(msg);
return; return;
@ -385,7 +385,7 @@ impl ChatApp {
Ok(Some(format!("Connected to {}", remote_user))) Ok(Some(format!("Connected to {}", remote_user)))
} }
"/chats" => { "/chats" => {
let chat_names: Vec<_> = self.state.sessions.keys().cloned().collect(); let chat_names: Vec<_> = self.state.chats.keys().cloned().collect();
if chat_names.is_empty() { if chat_names.is_empty() {
Ok(Some("No chats yet. Use /connect to start one.".to_string())) Ok(Some("No chats yet. Use /connect to start one.".to_string()))
} else { } else {
@ -430,7 +430,7 @@ impl ChatApp {
} }
} }
"/status" => { "/status" => {
let chats = self.state.sessions.len(); let chats = self.state.chats.len();
let active = self.state.active_chat.as_deref().unwrap_or("none"); let active = self.state.active_chat.as_deref().unwrap_or("none");
let status = format!( let status = format!(
"User: {}\nAddress: {}\nChats: {}\nActive: {}", "User: {}\nAddress: {}\nChats: {}\nActive: {}",
@ -443,7 +443,7 @@ impl ChatApp {
} }
"/clear" => { "/clear" => {
if let Some(active) = &self.state.active_chat.clone() if let Some(active) = &self.state.active_chat.clone()
&& let Some(session) = self.state.sessions.get_mut(active) && let Some(session) = self.state.chats.get_mut(active)
{ {
session.messages.clear(); session.messages.clear();
self.save_state()?; self.save_state()?;