2025-12-22 09:40:46 -08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::fmt::Debug;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
pub use crate::errors::ChatError;
|
2026-01-29 23:36:18 +07:00
|
|
|
use crate::types::{AddressedEncryptedPayload, ContentData};
|
2025-12-22 09:40:46 -08:00
|
|
|
|
|
|
|
|
pub type ConversationId<'a> = &'a str;
|
|
|
|
|
pub type ConversationIdOwned = Arc<str>;
|
|
|
|
|
|
2026-01-22 06:39:09 +07:00
|
|
|
pub trait Id: Debug {
|
2025-12-22 09:40:46 -08:00
|
|
|
fn id(&self) -> ConversationId;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 06:39:09 +07:00
|
|
|
pub trait ConvoFactory: Id + Debug {
|
|
|
|
|
fn handle_frame(
|
|
|
|
|
&mut self,
|
|
|
|
|
encoded_payload: &[u8],
|
|
|
|
|
) -> Result<(Box<dyn Convo>, Vec<ContentData>), ChatError>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait Convo: Id + Debug {
|
2026-01-29 23:36:18 +07:00
|
|
|
fn send_message(&mut self, content: &[u8])
|
|
|
|
|
-> Result<Vec<AddressedEncryptedPayload>, ChatError>;
|
|
|
|
|
|
|
|
|
|
fn remote_id(&self) -> String;
|
2026-01-22 06:39:09 +07:00
|
|
|
}
|
2025-12-22 09:40:46 -08:00
|
|
|
|
|
|
|
|
pub struct ConversationStore {
|
|
|
|
|
conversations: HashMap<Arc<str>, Box<dyn Convo>>,
|
2026-01-22 06:39:09 +07:00
|
|
|
factories: HashMap<Arc<str>, Box<dyn ConvoFactory>>,
|
2025-12-22 09:40:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ConversationStore {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
conversations: HashMap::new(),
|
2026-01-22 06:39:09 +07:00
|
|
|
factories: HashMap::new(),
|
2025-12-22 09:40:46 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 06:39:09 +07:00
|
|
|
pub fn insert_convo(&mut self, conversation: impl Convo + Id + 'static) -> ConversationIdOwned {
|
2025-12-22 09:40:46 -08:00
|
|
|
let key: ConversationIdOwned = Arc::from(conversation.id());
|
|
|
|
|
self.conversations
|
|
|
|
|
.insert(key.clone(), Box::new(conversation));
|
|
|
|
|
key
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 06:39:09 +07:00
|
|
|
pub fn register_factory(
|
|
|
|
|
&mut self,
|
|
|
|
|
handler: impl ConvoFactory + Id + 'static,
|
|
|
|
|
) -> ConversationIdOwned {
|
|
|
|
|
let key: ConversationIdOwned = Arc::from(handler.id());
|
|
|
|
|
self.factories.insert(key.clone(), Box::new(handler));
|
|
|
|
|
key
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 09:40:46 -08:00
|
|
|
pub fn get(&self, id: ConversationId) -> Option<&(dyn Convo + '_)> {
|
|
|
|
|
self.conversations.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())
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 06:39:09 +07:00
|
|
|
pub fn get_factory(&mut self, id: ConversationId) -> Option<&mut (dyn ConvoFactory + '_)> {
|
|
|
|
|
Some(self.factories.get_mut(id)?.as_mut())
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 09:40:46 -08:00
|
|
|
pub fn conversation_ids(&self) -> impl Iterator<Item = ConversationIdOwned> + '_ {
|
|
|
|
|
self.conversations.keys().cloned()
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 06:39:09 +07:00
|
|
|
pub fn factory_ids(&self) -> impl Iterator<Item = ConversationIdOwned> + '_ {
|
|
|
|
|
self.factories.keys().cloned()
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-22 09:40:46 -08:00
|
|
|
|
|
|
|
|
mod group_test;
|
|
|
|
|
mod privatev1;
|
|
|
|
|
|
|
|
|
|
pub use group_test::GroupTestConvo;
|
|
|
|
|
pub use privatev1::PrivateV1Convo;
|