diff --git a/conversations/src/context.rs b/conversations/src/context.rs index d3e4d8e..97f70c9 100644 --- a/conversations/src/context.rs +++ b/conversations/src/context.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap, rc::Rc, sync::Arc}; use crate::{ // conversation::{ConversationStore, Convo, Id}, - conversation::common::{ConversationStore, Convo, HasConversationId}, + conversation::common::{HasConversationId, OutboundSession, SessionRegistry}, errors::ChatError, identity::Identity, inbox::Inbox, @@ -21,7 +21,7 @@ pub type ConvoHandle = u32; // Ctx manages lifetimes of objects to process and generate payloads. pub struct Context { _identity: Rc, - store: ConversationStore, + store: SessionRegistry, inbox: Inbox, buf_size: usize, convo_handle_map: HashMap>, @@ -34,7 +34,7 @@ impl Context { let inbox = Inbox::new(Rc::clone(&identity)); // Self { _identity: identity, - store: ConversationStore::new(), + store: SessionRegistry::new(), inbox, buf_size: 0, convo_handle_map: HashMap::new(), @@ -100,17 +100,23 @@ impl Context { Ok(Introduction::from(pkb).into()) } - fn add_convo(&mut self, convo: impl Convo + HasConversationId + 'static) -> ConvoHandle { + fn add_convo( + &mut self, + convo: impl OutboundSession + HasConversationId + 'static, + ) -> ConvoHandle { let handle = self.next_convo_handle; self.next_convo_handle += 1; - let convo_id = self.store.insert_convo(convo); + let convo_id = self.store.insert_session(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 Convo, ChatError> { + fn get_convo_mut( + &mut self, + handle: ConvoHandle, + ) -> Result<&mut dyn OutboundSession, ChatError> { let convo_id = self .convo_handle_map .get(&handle) @@ -118,7 +124,7 @@ impl Context { .clone(); self.store - .get_mut(&convo_id) + .get_mut_session(&convo_id) .ok_or_else(|| ChatError::NoConvo(handle)) } } @@ -132,12 +138,12 @@ mod tests { #[test] fn convo_store_get() { - let mut store: ConversationStore = ConversationStore::new(); + let mut store: SessionRegistry = SessionRegistry::new(); let new_convo = PrivateV1Convo::new([0; 32].into()); - let convo_id = store.insert_convo(new_convo); + let convo_id = store.insert_session(new_convo); - let convo = store.get_mut(&convo_id).ok_or_else(|| 0); + let convo = store.get_mut_session(&convo_id).ok_or_else(|| 0); convo.unwrap(); } } diff --git a/conversations/src/conversation/common.rs b/conversations/src/conversation/common.rs index 24c7c12..3bc698e 100644 --- a/conversations/src/conversation/common.rs +++ b/conversations/src/conversation/common.rs @@ -5,76 +5,78 @@ use std::sync::Arc; pub use crate::errors::ChatError; use crate::types::{AddressedEncryptedPayload, ContentData}; -pub type ConversationId<'a> = &'a str; -pub type ConversationIdOwned = Arc; +pub type SessionId<'a> = &'a str; +pub type SessionIdOwned = Arc; pub trait HasConversationId: Debug { - fn id(&self) -> ConversationId; + fn id(&self) -> SessionId<'_>; } -pub trait ConvoFactory: HasConversationId + Debug { +#[allow(dead_code)] +pub trait InboundSessionHandler: HasConversationId + Debug { fn handle_frame( &mut self, encoded_payload: &[u8], - ) -> Result<(Box, Vec), ChatError>; + ) -> Result<(Box, Vec), ChatError>; } -pub trait Convo: HasConversationId + Debug { +pub trait OutboundSession: HasConversationId + Debug { fn send_message(&mut self, content: &[u8]) -> Result, ChatError>; fn remote_id(&self) -> String; } -pub struct ConversationStore { - conversations: HashMap, Box>, - factories: HashMap, Box>, +#[allow(dead_code)] +pub struct SessionRegistry { + sessions: HashMap, Box>, + handlers: HashMap, Box>, } -impl ConversationStore { +#[allow(dead_code)] +impl SessionRegistry { pub fn new() -> Self { Self { - conversations: HashMap::new(), - factories: HashMap::new(), + sessions: HashMap::new(), + handlers: HashMap::new(), } } - pub fn insert_convo( + pub fn insert_session( &mut self, - conversation: impl Convo + HasConversationId + 'static, - ) -> ConversationIdOwned { - let key: ConversationIdOwned = Arc::from(conversation.id()); - self.conversations - .insert(key.clone(), Box::new(conversation)); + conversation: impl OutboundSession + HasConversationId + 'static, + ) -> SessionIdOwned { + let key: SessionIdOwned = Arc::from(conversation.id()); + self.sessions.insert(key.clone(), Box::new(conversation)); key } - pub fn register_factory( + pub fn register_handler( &mut self, - handler: impl ConvoFactory + HasConversationId + 'static, - ) -> ConversationIdOwned { - let key: ConversationIdOwned = Arc::from(handler.id()); - self.factories.insert(key.clone(), Box::new(handler)); + handler: impl InboundSessionHandler + HasConversationId + 'static, + ) -> SessionIdOwned { + let key: SessionIdOwned = Arc::from(handler.id()); + self.handlers.insert(key.clone(), Box::new(handler)); key } - pub fn get(&self, id: ConversationId) -> Option<&(dyn Convo + '_)> { - self.conversations.get(id).map(|c| c.as_ref()) + pub fn get_session(&self, id: SessionId) -> Option<&(dyn OutboundSession + '_)> { + self.sessions.get(id).map(|c| c.as_ref()) } - pub fn get_mut(&mut self, id: &str) -> Option<&mut (dyn Convo + '_)> { - Some(self.conversations.get_mut(id)?.as_mut()) + pub fn get_mut_session(&mut self, id: &str) -> Option<&mut (dyn OutboundSession + '_)> { + Some(self.sessions.get_mut(id)?.as_mut()) } - pub fn get_factory(&mut self, id: ConversationId) -> Option<&mut (dyn ConvoFactory + '_)> { - Some(self.factories.get_mut(id)?.as_mut()) + pub fn get_handler(&mut self, id: SessionId) -> Option<&mut (dyn InboundSessionHandler + '_)> { + Some(self.handlers.get_mut(id)?.as_mut()) } - pub fn conversation_ids(&self) -> impl Iterator + '_ { - self.conversations.keys().cloned() + pub fn session_ids(&self) -> impl Iterator + '_ { + self.sessions.keys().cloned() } - pub fn factory_ids(&self) -> impl Iterator + '_ { - self.factories.keys().cloned() + pub fn handler_ids(&self) -> impl Iterator + '_ { + self.handlers.keys().cloned() } } diff --git a/conversations/src/conversation/privatev1.rs b/conversations/src/conversation/privatev1.rs index 2bf45db..2950629 100644 --- a/conversations/src/conversation/privatev1.rs +++ b/conversations/src/conversation/privatev1.rs @@ -6,7 +6,7 @@ use crypto::SecretKey; use prost::{Message, bytes::Bytes}; use crate::{ - conversation::common::{ConversationId, Convo, HasConversationId}, + conversation::common::{HasConversationId, OutboundSession, SessionId}, errors::ChatError, types::AddressedEncryptedPayload, utils::timestamp_millis, @@ -36,13 +36,13 @@ impl PrivateV1Convo { } impl HasConversationId for PrivateV1Convo { - fn id(&self) -> ConversationId { + fn id(&self) -> SessionId<'_> { // TODO: implementation "private_v1_convo_id" } } -impl Convo for PrivateV1Convo { +impl OutboundSession for PrivateV1Convo { fn send_message( &mut self, content: &[u8], diff --git a/conversations/src/inbox/inbox.rs b/conversations/src/inbox/inbox.rs index a6f0ccb..44c07d8 100644 --- a/conversations/src/inbox/inbox.rs +++ b/conversations/src/inbox/inbox.rs @@ -8,10 +8,11 @@ use std::rc::Rc; use crypto::{PrekeyBundle, SecretKey}; use crate::context::Introduction; -use crate::conversation::common::{ConversationId, Convo, ConvoFactory, HasConversationId}; +use crate::conversation::common::{ + HasConversationId, InboundSessionHandler, OutboundSession, SessionId, +}; use crate::conversation::privatev1::PrivateV1Convo; -// use crate::conversation::{ChatError, ConversationId, Convo, ConvoFactory, Id, PrivateV1Convo}; -use crate::crypto::{Blake2b128, CopyBytes, Digest, PublicKey, StaticSecret}; +use crate::crypto::{CopyBytes, PublicKey, StaticSecret}; use crate::errors::ChatError; use crate::identity::Identity; use crate::inbox::handshake::InboxHandshake; @@ -53,11 +54,6 @@ impl Inbox { } } - fn compute_local_convo_id(addr: &str) -> String { - let hash = Blake2b128::digest(format!("{}:{}:{}", "logoschat", "inboxV1", addr)); - hex::encode(hash) - } - pub fn create_bundle(&mut self) -> PrekeyBundle { let ephemeral = StaticSecret::random(); @@ -204,16 +200,16 @@ impl Inbox { } impl HasConversationId for Inbox { - fn id(&self) -> ConversationId { + fn id(&self) -> SessionId<'_> { &self.local_convo_id } } -impl ConvoFactory for Inbox { +impl InboundSessionHandler 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())); }