diff --git a/conversations/src/common.rs b/conversations/src/common.rs index 0b1d9ca..55916f0 100644 --- a/conversations/src/common.rs +++ b/conversations/src/common.rs @@ -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; +pub type ChatId<'a> = &'a str; +pub type ChatIdOwned = Arc; -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, Vec), ChatError>; + ) -> Result<(Box, Vec), ChatError>; } -pub trait OutboundSession: HasConversationId + Debug { +pub trait Chat: HasChatId + Debug { fn send_message(&mut self, content: &[u8]) -> Result, ChatError>; fn remote_id(&self) -> String; } -pub struct SessionRegistry { - sessions: HashMap, Box>, - handlers: HashMap, Box>, +pub struct ChatStore { + chats: HashMap, Box>, + handlers: HashMap, Box>, } -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 + '_ { - self.sessions.keys().cloned() + pub fn chat_ids(&self) -> impl Iterator + '_ { + self.chats.keys().cloned() } - pub fn handler_ids(&self) -> impl Iterator + '_ { + pub fn handler_ids(&self) -> impl Iterator + '_ { self.handlers.keys().cloned() } } diff --git a/conversations/src/context.rs b/conversations/src/context.rs index e21ba1b..7323c01 100644 --- a/conversations/src/context.rs +++ b/conversations/src/context.rs @@ -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, - store: SessionRegistry, + store: ChatStore, inbox: Inbox, convo_handle_map: HashMap>, 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(); } } diff --git a/conversations/src/dm/privatev1.rs b/conversations/src/dm/privatev1.rs index 4175d1f..8810a9c 100644 --- a/conversations/src/dm/privatev1.rs +++ b/conversations/src/dm/privatev1.rs @@ -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], diff --git a/conversations/src/inbox/inbox.rs b/conversations/src/inbox/inbox.rs index 599d90b..b6af232 100644 --- a/conversations/src/inbox/inbox.rs +++ b/conversations/src/inbox/inbox.rs @@ -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, Vec), ChatError> { + ) -> Result<(Box, Vec), ChatError> { if message.len() == 0 { return Err(ChatError::Protocol("Example error".into())); }