chore: reanme session to chat

This commit is contained in:
kaichaosun 2026-02-04 12:17:36 +08:00
parent cc6988d8ac
commit 0ddfd3f3f3
No known key found for this signature in database
GPG Key ID: 223E0F992F4F03BF
4 changed files with 45 additions and 54 deletions

View File

@ -5,75 +5,72 @@ use std::sync::Arc;
pub use crate::errors::ChatError;
use crate::types::{AddressedEncryptedPayload, ContentData};
pub type SessionId<'a> = &'a str;
pub type SessionIdOwned = Arc<str>;
pub type ChatId<'a> = &'a str;
pub type ChatIdOwned = Arc<str>;
pub trait HasConversationId: Debug {
fn id(&self) -> SessionId<'_>;
pub trait HasChatId: Debug {
fn id(&self) -> ChatId<'_>;
}
pub trait InboundSessionHandler: HasConversationId + Debug {
pub trait InboundMessageHandler {
fn handle_frame(
&mut self,
encoded_payload: &[u8],
) -> Result<(Box<dyn OutboundSession>, Vec<ContentData>), ChatError>;
) -> Result<(Box<dyn Chat>, Vec<ContentData>), ChatError>;
}
pub trait OutboundSession: HasConversationId + Debug {
pub trait Chat: HasChatId + Debug {
fn send_message(&mut self, content: &[u8])
-> Result<Vec<AddressedEncryptedPayload>, ChatError>;
fn remote_id(&self) -> String;
}
pub struct SessionRegistry {
sessions: HashMap<Arc<str>, Box<dyn OutboundSession>>,
handlers: HashMap<Arc<str>, Box<dyn InboundSessionHandler>>,
pub struct ChatStore {
chats: HashMap<Arc<str>, Box<dyn Chat>>,
handlers: HashMap<Arc<str>, Box<dyn InboundMessageHandler>>,
}
impl SessionRegistry {
impl ChatStore {
pub fn new() -> Self {
Self {
sessions: HashMap::new(),
chats: HashMap::new(),
handlers: HashMap::new(),
}
}
pub fn insert_session(
&mut self,
conversation: impl OutboundSession + HasConversationId + 'static,
) -> SessionIdOwned {
let key: SessionIdOwned = Arc::from(conversation.id());
self.sessions.insert(key.clone(), Box::new(conversation));
pub fn insert_chat(&mut self, conversation: impl Chat + HasChatId + 'static) -> ChatIdOwned {
let key: ChatIdOwned = Arc::from(conversation.id());
self.chats.insert(key.clone(), Box::new(conversation));
key
}
pub fn register_handler(
&mut self,
handler: impl InboundSessionHandler + HasConversationId + 'static,
) -> SessionIdOwned {
let key: SessionIdOwned = Arc::from(handler.id());
handler: impl InboundMessageHandler + HasChatId + 'static,
) -> ChatIdOwned {
let key: ChatIdOwned = Arc::from(handler.id());
self.handlers.insert(key.clone(), Box::new(handler));
key
}
pub fn get_session(&self, id: SessionId) -> Option<&(dyn OutboundSession + '_)> {
self.sessions.get(id).map(|c| c.as_ref())
pub fn get_chat(&self, id: ChatId) -> Option<&(dyn Chat + '_)> {
self.chats.get(id).map(|c| c.as_ref())
}
pub fn get_mut_session(&mut self, id: &str) -> Option<&mut (dyn OutboundSession + '_)> {
Some(self.sessions.get_mut(id)?.as_mut())
pub fn get_mut_chat(&mut self, id: &str) -> Option<&mut (dyn Chat + '_)> {
Some(self.chats.get_mut(id)?.as_mut())
}
pub fn get_handler(&mut self, id: SessionId) -> Option<&mut (dyn InboundSessionHandler + '_)> {
pub fn get_handler(&mut self, id: ChatId) -> Option<&mut (dyn InboundMessageHandler + '_)> {
Some(self.handlers.get_mut(id)?.as_mut())
}
pub fn session_ids(&self) -> impl Iterator<Item = SessionIdOwned> + '_ {
self.sessions.keys().cloned()
pub fn chat_ids(&self) -> impl Iterator<Item = ChatIdOwned> + '_ {
self.chats.keys().cloned()
}
pub fn handler_ids(&self) -> impl Iterator<Item = SessionIdOwned> + '_ {
pub fn handler_ids(&self) -> impl Iterator<Item = ChatIdOwned> + '_ {
self.handlers.keys().cloned()
}
}

View File

@ -1,7 +1,7 @@
use std::{collections::HashMap, rc::Rc, sync::Arc};
use crate::{
common::{HasConversationId, OutboundSession, SessionRegistry},
common::{Chat, ChatStore, HasChatId},
errors::ChatError,
identity::Identity,
inbox::Inbox,
@ -20,7 +20,7 @@ pub type ConvoHandle = u32;
// Ctx manages lifetimes of objects to process and generate payloads.
pub struct Context {
_identity: Rc<Identity>,
store: SessionRegistry,
store: ChatStore,
inbox: Inbox,
convo_handle_map: HashMap<u32, Arc<str>>,
next_convo_handle: ConvoHandle,
@ -32,7 +32,7 @@ impl Context {
let inbox = Inbox::new(Rc::clone(&identity)); //
Self {
_identity: identity,
store: SessionRegistry::new(),
store: ChatStore::new(),
inbox,
convo_handle_map: HashMap::new(),
next_convo_handle: INITIAL_CONVO_HANDLE,
@ -89,23 +89,17 @@ impl Context {
Ok(Introduction::from(pkb).into())
}
fn add_convo(
&mut self,
convo: impl OutboundSession + HasConversationId + 'static,
) -> ConvoHandle {
fn add_convo(&mut self, convo: impl Chat + HasChatId + 'static) -> ConvoHandle {
let handle = self.next_convo_handle;
self.next_convo_handle += 1;
let convo_id = self.store.insert_session(convo);
let convo_id = self.store.insert_chat(convo);
self.convo_handle_map.insert(handle, convo_id);
handle
}
// Returns a mutable reference to a Convo for a given ConvoHandle
fn get_convo_mut(
&mut self,
handle: ConvoHandle,
) -> Result<&mut dyn OutboundSession, ChatError> {
fn get_convo_mut(&mut self, handle: ConvoHandle) -> Result<&mut dyn Chat, ChatError> {
let convo_id = self
.convo_handle_map
.get(&handle)
@ -113,7 +107,7 @@ impl Context {
.clone();
self.store
.get_mut_session(&convo_id)
.get_mut_chat(&convo_id)
.ok_or_else(|| ChatError::NoConvo(handle))
}
}
@ -127,12 +121,12 @@ mod tests {
#[test]
fn convo_store_get() {
let mut store: SessionRegistry = SessionRegistry::new();
let mut store: ChatStore = ChatStore::new();
let new_convo = PrivateV1Convo::new([0; 32].into());
let convo_id = store.insert_session(new_convo);
let convo_id = store.insert_chat(new_convo);
let convo = store.get_mut_session(&convo_id).ok_or_else(|| 0);
let convo = store.get_mut_chat(&convo_id).ok_or_else(|| 0);
convo.unwrap();
}
}

View File

@ -6,7 +6,7 @@ use crypto::SecretKey;
use prost::{Message, bytes::Bytes};
use crate::{
common::{HasConversationId, OutboundSession, SessionId},
common::{ChatId, HasChatId, Chat},
errors::ChatError,
types::AddressedEncryptedPayload,
utils::timestamp_millis,
@ -35,14 +35,14 @@ impl PrivateV1Convo {
}
}
impl HasConversationId for PrivateV1Convo {
fn id(&self) -> SessionId<'_> {
impl HasChatId for PrivateV1Convo {
fn id(&self) -> ChatId<'_> {
// TODO: implementation
"private_v1_convo_id"
}
}
impl OutboundSession for PrivateV1Convo {
impl Chat for PrivateV1Convo {
fn send_message(
&mut self,
content: &[u8],

View File

@ -7,7 +7,7 @@ use std::rc::Rc;
use crypto::{PrekeyBundle, SecretKey};
use crate::common::{HasConversationId, InboundSessionHandler, OutboundSession, SessionId};
use crate::common::{Chat, ChatId, HasChatId, InboundMessageHandler};
use crate::context::Introduction;
use crate::dm::privatev1::PrivateV1Convo;
use crate::errors::ChatError;
@ -198,17 +198,17 @@ impl Inbox {
}
}
impl HasConversationId for Inbox {
fn id(&self) -> SessionId<'_> {
impl HasChatId for Inbox {
fn id(&self) -> ChatId<'_> {
&self.local_convo_id
}
}
impl InboundSessionHandler for Inbox {
impl InboundMessageHandler for Inbox {
fn handle_frame(
&mut self,
message: &[u8],
) -> Result<(Box<dyn OutboundSession>, Vec<ContentData>), ChatError> {
) -> Result<(Box<dyn Chat>, Vec<ContentData>), ChatError> {
if message.len() == 0 {
return Err(ChatError::Protocol("Example error".into()));
}