diff --git a/core/conversations/src/context.rs b/core/conversations/src/context.rs index 2c14aff..44f94de 100644 --- a/core/conversations/src/context.rs +++ b/core/conversations/src/context.rs @@ -3,7 +3,7 @@ use std::{cell::RefCell, rc::Rc}; use crypto::Identity; use double_ratchets::{RatchetState, restore_ratchet_state}; -use storage::{ChatStore, ConversationKind, ConversationMeta}; +use storage::{ChatStore, ConversationKind}; use crate::{ conversation::{Conversation, ConversationId, Convo, Id, PrivateV1Convo}, @@ -83,7 +83,7 @@ impl Context { remote_bundle: &Introduction, content: &[u8], ) -> Result<(ConversationIdOwned, Vec), ChatError> { - let (convo, payloads) = self + let (mut convo, payloads) = self .inbox .invite_to_private_convo(remote_bundle, content, Rc::clone(&self.store)) .unwrap_or_else(|_| todo!("Log/Surface Error")); @@ -94,7 +94,7 @@ impl Context { .map(|p| p.into_envelope(remote_id.clone())) .collect(); - let convo_id = self.persist_convo(&convo)?; + let convo_id = convo.persist()?; Ok((convo_id, payload_bytes)) } @@ -117,7 +117,6 @@ impl Context { Conversation::Private(mut convo) => { let payloads = convo.send_message(content)?; let remote_id = convo.remote_id(); - convo.save_ratchet_state::(&mut *self.store.borrow_mut())?; Ok(payloads .into_iter() @@ -152,7 +151,7 @@ impl Context { .handle_frame(enc_payload, &public_key_hex, Rc::clone(&self.store))?; match convo { - Conversation::Private(convo) => self.persist_convo(&convo)?, + Conversation::Private(mut convo) => convo.persist()?, }; self.store @@ -172,7 +171,6 @@ impl Context { match convo { Conversation::Private(mut convo) => { let result = convo.handle_frame(enc_payload)?; - convo.save_ratchet_state(&mut *self.store.borrow_mut())?; Ok(result) } } @@ -216,21 +214,6 @@ impl Context { ))), } } - - /// Persists a conversation's metadata and ratchet state to DB. - fn persist_convo( - &mut self, - convo: &PrivateV1Convo, - ) -> Result { - let convo_info = ConversationMeta { - local_convo_id: convo.id().to_string(), - remote_convo_id: convo.remote_id(), - kind: convo.convo_type(), - }; - self.store.borrow_mut().save_conversation(&convo_info)?; - convo.save_ratchet_state(&mut *self.store.borrow_mut())?; - Ok(Arc::from(convo.id())) - } } #[cfg(test)] diff --git a/core/conversations/src/conversation/privatev1.rs b/core/conversations/src/conversation/privatev1.rs index 85427f0..805cafd 100644 --- a/core/conversations/src/conversation/privatev1.rs +++ b/core/conversations/src/conversation/privatev1.rs @@ -9,10 +9,11 @@ use chat_proto::logoschat::{ use crypto::{PrivateKey, PublicKey, SymmetricKey32}; use double_ratchets::{Header, InstallationKeyPair, RatchetState}; use prost::{Message, bytes::Bytes}; -use std::{cell::RefCell, fmt::Debug, rc::Rc}; -use storage::{ConversationKind, ConversationStore}; +use std::{cell::RefCell, fmt::Debug, rc::Rc, sync::Arc}; +use storage::{ConversationKind, ConversationMeta, ConversationStore}; use crate::{ + context::ConversationIdOwned, conversation::{ChatError, ConversationId, Convo, Id}, errors::EncryptionError, proto, @@ -186,6 +187,18 @@ impl PrivateV1Convo { }) } + /// Persists a conversation's metadata and ratchet state to DB. + pub fn persist(&mut self) -> Result { + let convo_info = ConversationMeta { + local_convo_id: self.id().to_string(), + remote_convo_id: self.remote_id(), + kind: self.convo_type(), + }; + self.store.borrow_mut().save_conversation(&convo_info)?; + self.save_ratchet_state(&mut *self.store.borrow_mut())?; + Ok(Arc::from(self.id())) + } + pub fn save_ratchet_state(&self, storage: &mut T) -> Result<(), ChatError> { let record = to_ratchet_record(&self.dr_state); let skipped_keys = to_skipped_key_records(&self.dr_state.skipped_keys()); @@ -214,6 +227,8 @@ impl Convo for PrivateV1Convo { let data = self.encrypt(frame); + self.save_ratchet_state::(&mut *self.store.borrow_mut())?; + Ok(vec![AddressedEncryptedPayload { delivery_address: "delivery_address".into(), data,