mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-02-10 17:03:12 +00:00
chore: refactor conversations
This commit is contained in:
parent
74695877fa
commit
6c54bbd79d
@ -1,7 +1,8 @@
|
||||
use std::{collections::HashMap, rc::Rc, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
conversation::{ConversationStore, Convo, Id},
|
||||
// conversation::{ConversationStore, Convo, Id},
|
||||
conversation::common::{ConversationStore, Convo, HasConversationId},
|
||||
errors::ChatError,
|
||||
identity::Identity,
|
||||
inbox::Inbox,
|
||||
@ -99,7 +100,7 @@ impl Context {
|
||||
Ok(Introduction::from(pkb).into())
|
||||
}
|
||||
|
||||
fn add_convo(&mut self, convo: impl Convo + Id + 'static) -> ConvoHandle {
|
||||
fn add_convo(&mut self, convo: impl Convo + HasConversationId + 'static) -> ConvoHandle {
|
||||
let handle = self.next_convo_handle;
|
||||
self.next_convo_handle += 1;
|
||||
let convo_id = self.store.insert_convo(convo);
|
||||
@ -125,14 +126,15 @@ impl Context {
|
||||
#[cfg(test)]
|
||||
|
||||
mod tests {
|
||||
use crate::conversation::privatev1::PrivateV1Convo;
|
||||
|
||||
use super::*;
|
||||
use crate::conversation::GroupTestConvo;
|
||||
|
||||
#[test]
|
||||
fn convo_store_get() {
|
||||
let mut store: ConversationStore = ConversationStore::new();
|
||||
|
||||
let new_convo = GroupTestConvo::new();
|
||||
let new_convo = PrivateV1Convo::new([0; 32].into());
|
||||
let convo_id = store.insert_convo(new_convo);
|
||||
|
||||
let convo = store.get_mut(&convo_id).ok_or_else(|| 0);
|
||||
|
||||
@ -8,18 +8,18 @@ use crate::types::{AddressedEncryptedPayload, ContentData};
|
||||
pub type ConversationId<'a> = &'a str;
|
||||
pub type ConversationIdOwned = Arc<str>;
|
||||
|
||||
pub trait Id: Debug {
|
||||
pub trait HasConversationId: Debug {
|
||||
fn id(&self) -> ConversationId;
|
||||
}
|
||||
|
||||
pub trait ConvoFactory: Id + Debug {
|
||||
pub trait ConvoFactory: HasConversationId + Debug {
|
||||
fn handle_frame(
|
||||
&mut self,
|
||||
encoded_payload: &[u8],
|
||||
) -> Result<(Box<dyn Convo>, Vec<ContentData>), ChatError>;
|
||||
}
|
||||
|
||||
pub trait Convo: Id + Debug {
|
||||
pub trait Convo: HasConversationId + Debug {
|
||||
fn send_message(&mut self, content: &[u8])
|
||||
-> Result<Vec<AddressedEncryptedPayload>, ChatError>;
|
||||
|
||||
@ -39,7 +39,10 @@ impl ConversationStore {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_convo(&mut self, conversation: impl Convo + Id + 'static) -> ConversationIdOwned {
|
||||
pub fn insert_convo(
|
||||
&mut self,
|
||||
conversation: impl Convo + HasConversationId + 'static,
|
||||
) -> ConversationIdOwned {
|
||||
let key: ConversationIdOwned = Arc::from(conversation.id());
|
||||
self.conversations
|
||||
.insert(key.clone(), Box::new(conversation));
|
||||
@ -48,7 +51,7 @@ impl ConversationStore {
|
||||
|
||||
pub fn register_factory(
|
||||
&mut self,
|
||||
handler: impl ConvoFactory + Id + 'static,
|
||||
handler: impl ConvoFactory + HasConversationId + 'static,
|
||||
) -> ConversationIdOwned {
|
||||
let key: ConversationIdOwned = Arc::from(handler.id());
|
||||
self.factories.insert(key.clone(), Box::new(handler));
|
||||
@ -75,9 +78,3 @@ impl ConversationStore {
|
||||
self.factories.keys().cloned()
|
||||
}
|
||||
}
|
||||
|
||||
mod group_test;
|
||||
mod privatev1;
|
||||
|
||||
pub use group_test::GroupTestConvo;
|
||||
pub use privatev1::PrivateV1Convo;
|
||||
@ -1,33 +0,0 @@
|
||||
use crate::{
|
||||
conversation::{ChatError, ConversationId, Convo, Id},
|
||||
types::AddressedEncryptedPayload,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct GroupTestConvo {}
|
||||
|
||||
impl GroupTestConvo {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Id for GroupTestConvo {
|
||||
fn id(&self) -> ConversationId {
|
||||
// implementation
|
||||
"grouptest"
|
||||
}
|
||||
}
|
||||
|
||||
impl Convo for GroupTestConvo {
|
||||
fn send_message(
|
||||
&mut self,
|
||||
_content: &[u8],
|
||||
) -> Result<Vec<AddressedEncryptedPayload>, ChatError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn remote_id(&self) -> String {
|
||||
self.id().to_string()
|
||||
}
|
||||
}
|
||||
2
conversations/src/conversation/mod.rs
Normal file
2
conversations/src/conversation/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod common;
|
||||
pub mod privatev1;
|
||||
@ -6,7 +6,8 @@ use crypto::SecretKey;
|
||||
use prost::{Message, bytes::Bytes};
|
||||
|
||||
use crate::{
|
||||
conversation::{ChatError, ConversationId, Convo, Id},
|
||||
conversation::common::{ConversationId, Convo, HasConversationId},
|
||||
errors::ChatError,
|
||||
types::AddressedEncryptedPayload,
|
||||
utils::timestamp_millis,
|
||||
};
|
||||
@ -34,7 +35,7 @@ impl PrivateV1Convo {
|
||||
}
|
||||
}
|
||||
|
||||
impl Id for PrivateV1Convo {
|
||||
impl HasConversationId for PrivateV1Convo {
|
||||
fn id(&self) -> ConversationId {
|
||||
// TODO: implementation
|
||||
"private_v1_convo_id"
|
||||
|
||||
@ -8,8 +8,11 @@ use std::rc::Rc;
|
||||
use crypto::{PrekeyBundle, SecretKey};
|
||||
|
||||
use crate::context::Introduction;
|
||||
use crate::conversation::{ChatError, ConversationId, Convo, ConvoFactory, Id, PrivateV1Convo};
|
||||
use crate::conversation::common::{ConversationId, Convo, ConvoFactory, HasConversationId};
|
||||
use crate::conversation::privatev1::PrivateV1Convo;
|
||||
// use crate::conversation::{ChatError, ConversationId, Convo, ConvoFactory, Id, PrivateV1Convo};
|
||||
use crate::crypto::{Blake2b128, CopyBytes, Digest, PublicKey, StaticSecret};
|
||||
use crate::errors::ChatError;
|
||||
use crate::identity::Identity;
|
||||
use crate::inbox::handshake::InboxHandshake;
|
||||
use crate::proto;
|
||||
@ -200,7 +203,7 @@ impl Inbox {
|
||||
}
|
||||
}
|
||||
|
||||
impl Id for Inbox {
|
||||
impl HasConversationId for Inbox {
|
||||
fn id(&self) -> ConversationId {
|
||||
&self.local_convo_id
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user