feat: move convo store to private v1

This commit is contained in:
kaichaosun 2026-04-08 16:04:11 +08:00
parent 9c6d79d575
commit 033b6a7c50
No known key found for this signature in database
GPG Key ID: 223E0F992F4F03BF
2 changed files with 21 additions and 23 deletions

View File

@ -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<T: ChatStore> Context<T> {
remote_bundle: &Introduction,
content: &[u8],
) -> Result<(ConversationIdOwned, Vec<AddressedEnvelope>), 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<T: ChatStore> Context<T> {
.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<T: ChatStore> Context<T> {
Conversation::Private(mut convo) => {
let payloads = convo.send_message(content)?;
let remote_id = convo.remote_id();
convo.save_ratchet_state::<T>(&mut *self.store.borrow_mut())?;
Ok(payloads
.into_iter()
@ -152,7 +151,7 @@ impl<T: ChatStore> Context<T> {
.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<T: ChatStore> Context<T> {
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<T: ChatStore> Context<T> {
))),
}
}
/// Persists a conversation's metadata and ratchet state to DB.
fn persist_convo(
&mut self,
convo: &PrivateV1Convo<T>,
) -> Result<ConversationIdOwned, ChatError> {
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)]

View File

@ -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<S: ConversationStore + RatchetStore> PrivateV1Convo<S> {
})
}
/// Persists a conversation's metadata and ratchet state to DB.
pub fn persist(&mut self) -> Result<ConversationIdOwned, ChatError> {
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<T: RatchetStore>(&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<S: ConversationStore + RatchetStore> Convo for PrivateV1Convo<S> {
let data = self.encrypt(frame);
self.save_ratchet_state::<S>(&mut *self.store.borrow_mut())?;
Ok(vec![AddressedEncryptedPayload {
delivery_address: "delivery_address".into(),
data,