mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-05-12 04:59:27 +00:00
chore: params postion
This commit is contained in:
parent
31792d2db1
commit
54eb8edee4
@ -17,18 +17,18 @@ pub use crate::inbox::Introduction;
|
||||
|
||||
// This is the main entry point to the conversations api.
|
||||
// Ctx manages lifetimes of objects to process and generate payloads.
|
||||
pub struct Context<T: ChatStore> {
|
||||
pub struct Context<S: ChatStore> {
|
||||
_identity: Rc<Identity>,
|
||||
inbox: Inbox<T>,
|
||||
store: Rc<RefCell<T>>,
|
||||
inbox: Inbox<S>,
|
||||
store: Rc<RefCell<S>>,
|
||||
}
|
||||
|
||||
impl<T: ChatStore> Context<T> {
|
||||
impl<S: ChatStore> Context<S> {
|
||||
/// Opens or creates a Context with the given storage configuration.
|
||||
///
|
||||
/// If an identity exists in storage, it will be restored.
|
||||
/// Otherwise, a new identity will be created with the given name and saved.
|
||||
pub fn new_from_store(name: impl Into<String>, store: T) -> Result<Self, ChatError> {
|
||||
pub fn new_from_store(name: impl Into<String>, store: S) -> Result<Self, ChatError> {
|
||||
let name = name.into();
|
||||
let store = Rc::new(RefCell::new(store));
|
||||
|
||||
@ -42,7 +42,7 @@ impl<T: ChatStore> Context<T> {
|
||||
};
|
||||
|
||||
let identity = Rc::new(identity);
|
||||
let inbox = Inbox::new(Rc::clone(&identity), Rc::clone(&store));
|
||||
let inbox = Inbox::new(Rc::clone(&store), Rc::clone(&identity));
|
||||
|
||||
Ok(Self {
|
||||
_identity: identity,
|
||||
@ -54,7 +54,7 @@ impl<T: ChatStore> Context<T> {
|
||||
/// Creates a new in-memory Context (for testing).
|
||||
///
|
||||
/// Uses in-memory SQLite database. Each call creates a new isolated database.
|
||||
pub fn new_with_name(name: impl Into<String>, chat_store: T) -> Self {
|
||||
pub fn new_with_name(name: impl Into<String>, chat_store: S) -> Self {
|
||||
let name = name.into();
|
||||
let identity = Identity::new(&name);
|
||||
let chat_store = Rc::new(RefCell::new(chat_store));
|
||||
@ -64,7 +64,7 @@ impl<T: ChatStore> Context<T> {
|
||||
.expect("in-memory storage should not fail");
|
||||
|
||||
let identity = Rc::new(identity);
|
||||
let inbox = Inbox::new(Rc::clone(&identity), Rc::clone(&chat_store));
|
||||
let inbox = Inbox::new(Rc::clone(&chat_store), Rc::clone(&identity));
|
||||
|
||||
Self {
|
||||
_identity: identity,
|
||||
@ -87,7 +87,7 @@ impl<T: ChatStore> Context<T> {
|
||||
.invite_to_private_convo(remote_bundle, content, Rc::clone(&self.store))
|
||||
.unwrap_or_else(|_| todo!("Log/Surface Error"));
|
||||
|
||||
let remote_id = Inbox::<T>::inbox_identifier_for_key(*remote_bundle.installation_key());
|
||||
let remote_id = Inbox::<S>::inbox_identifier_for_key(*remote_bundle.installation_key());
|
||||
let payload_bytes = payloads
|
||||
.into_iter()
|
||||
.map(|p| p.into_envelope(remote_id.clone()))
|
||||
@ -144,7 +144,7 @@ impl<T: ChatStore> Context<T> {
|
||||
&mut self,
|
||||
enc_payload: EncryptedPayload,
|
||||
) -> Result<Option<ContentData>, ChatError> {
|
||||
let public_key_hex = Inbox::<T>::extract_ephemeral_key_hex(&enc_payload)?;
|
||||
let public_key_hex = Inbox::<S>::extract_ephemeral_key_hex(&enc_payload)?;
|
||||
let (convo, content) =
|
||||
self.inbox
|
||||
.handle_frame(enc_payload, &public_key_hex, Rc::clone(&self.store))?;
|
||||
@ -181,7 +181,7 @@ impl<T: ChatStore> Context<T> {
|
||||
}
|
||||
|
||||
/// Loads a conversation from DB by constructing it from metadata.
|
||||
fn load_convo(&self, convo_id: ConversationId) -> Result<Conversation<T>, ChatError> {
|
||||
fn load_convo(&self, convo_id: ConversationId) -> Result<Conversation<S>, ChatError> {
|
||||
let record = self
|
||||
.store
|
||||
.borrow()
|
||||
@ -191,9 +191,9 @@ impl<T: ChatStore> Context<T> {
|
||||
match record.kind {
|
||||
ConversationKind::PrivateV1 => {
|
||||
let private_convo = PrivateV1Convo::new(
|
||||
self.store.clone(),
|
||||
record.local_convo_id,
|
||||
record.remote_convo_id,
|
||||
self.store.clone(),
|
||||
)?;
|
||||
Ok(Conversation::Private(private_convo))
|
||||
}
|
||||
|
||||
@ -66,9 +66,9 @@ pub struct PrivateV1Convo<S: ConversationStore + RatchetStore> {
|
||||
impl<S: ConversationStore + RatchetStore> PrivateV1Convo<S> {
|
||||
/// Reconstructs a PrivateV1Convo from persisted metadata and ratchet state.
|
||||
pub fn new(
|
||||
store: Rc<RefCell<S>>,
|
||||
local_convo_id: String,
|
||||
remote_convo_id: String,
|
||||
store: Rc<RefCell<S>>,
|
||||
) -> Result<Self, ChatError> {
|
||||
let dr_record = store.borrow().load_ratchet_state(&local_convo_id)?;
|
||||
let skipped_keys = store.borrow().load_skipped_keys(&local_convo_id)?;
|
||||
@ -83,9 +83,9 @@ impl<S: ConversationStore + RatchetStore> PrivateV1Convo<S> {
|
||||
}
|
||||
|
||||
pub fn new_initiator(
|
||||
store: Rc<RefCell<S>>,
|
||||
seed_key: SymmetricKey32,
|
||||
remote: PublicKey,
|
||||
store: Rc<RefCell<S>>,
|
||||
) -> Self {
|
||||
let base_convo_id = BaseConvoId::new(&seed_key);
|
||||
let local_convo_id = base_convo_id.id_for_participant(Role::Initiator);
|
||||
@ -106,9 +106,9 @@ impl<S: ConversationStore + RatchetStore> PrivateV1Convo<S> {
|
||||
}
|
||||
|
||||
pub fn new_responder(
|
||||
store: Rc<RefCell<S>>,
|
||||
seed_key: SymmetricKey32,
|
||||
dh_self: &PrivateKey,
|
||||
store: Rc<RefCell<S>>,
|
||||
) -> Self {
|
||||
let base_convo_id = BaseConvoId::new(&seed_key);
|
||||
let local_convo_id = base_convo_id.id_for_participant(Role::Responder);
|
||||
@ -305,8 +305,8 @@ mod tests {
|
||||
let seed_key_saro = SymmetricKey32::from(seed_key);
|
||||
let seed_key_raya = SymmetricKey32::from(seed_key);
|
||||
let send_content_bytes = vec![0, 2, 4, 6, 8];
|
||||
let mut sr_convo = PrivateV1Convo::new_initiator(seed_key_saro, pub_raya, saro_storage);
|
||||
let mut rs_convo = PrivateV1Convo::new_responder(seed_key_raya, &raya, raya_storage);
|
||||
let mut sr_convo = PrivateV1Convo::new_initiator(saro_storage, seed_key_saro, pub_raya);
|
||||
let mut rs_convo = PrivateV1Convo::new_responder(raya_storage, seed_key_raya, &raya);
|
||||
|
||||
let send_frame = PrivateV1Frame {
|
||||
conversation_id: "_".into(),
|
||||
|
||||
@ -39,7 +39,7 @@ impl<S: EphemeralKeyStore> std::fmt::Debug for Inbox<S> {
|
||||
}
|
||||
|
||||
impl<S: EphemeralKeyStore> Inbox<S> {
|
||||
pub fn new(ident: Rc<Identity>, store: Rc<RefCell<S>>) -> Self {
|
||||
pub fn new(store: Rc<RefCell<S>>, ident: Rc<Identity>) -> Self {
|
||||
let local_convo_id = Self::inbox_identifier_for_key(ident.public_key());
|
||||
Self {
|
||||
ident,
|
||||
@ -83,7 +83,7 @@ impl<S: EphemeralKeyStore> Inbox<S> {
|
||||
InboxHandshake::perform_as_initiator(self.ident.secret(), &pkb, &mut rng);
|
||||
|
||||
let mut convo =
|
||||
PrivateV1Convo::new_initiator(seed_key, *remote_bundle.ephemeral_key(), private_store);
|
||||
PrivateV1Convo::new_initiator(private_store, seed_key, *remote_bundle.ephemeral_key());
|
||||
|
||||
let mut payloads = convo.send_message(initial_message)?;
|
||||
|
||||
@ -146,7 +146,7 @@ impl<S: EphemeralKeyStore> Inbox<S> {
|
||||
match frame.frame_type.unwrap() {
|
||||
proto::inbox_v1_frame::FrameType::InvitePrivateV1(_invite_private_v1) => {
|
||||
let mut convo =
|
||||
PrivateV1Convo::new_responder(seed_key, &ephemeral_key, private_store);
|
||||
PrivateV1Convo::new_responder(private_store, seed_key, &ephemeral_key);
|
||||
|
||||
let Some(enc_payload) = _invite_private_v1.initial_message else {
|
||||
return Err(ChatError::Protocol("missing initial encpayload".into()));
|
||||
@ -272,10 +272,10 @@ mod tests {
|
||||
));
|
||||
|
||||
let saro_ident = Identity::new("saro");
|
||||
let saro_inbox = Inbox::new(saro_ident.into(), Rc::clone(&saro_storage));
|
||||
let saro_inbox = Inbox::new(Rc::clone(&saro_storage), saro_ident.into());
|
||||
|
||||
let raya_ident = Identity::new("raya");
|
||||
let raya_inbox = Inbox::new(raya_ident.into(), Rc::clone(&raya_storage));
|
||||
let raya_inbox = Inbox::new(Rc::clone(&raya_storage), raya_ident.into());
|
||||
|
||||
let bundle = raya_inbox.create_intro_bundle().unwrap();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user