chore: more refactor

This commit is contained in:
kaichaosun 2026-03-30 12:59:48 +08:00
parent 927be286e8
commit a342a508b3
No known key found for this signature in database
GPG Key ID: 223E0F992F4F03BF
3 changed files with 16 additions and 20 deletions

View File

@ -192,7 +192,7 @@ impl<T: ChatStore> Context<T> {
let dr_state: RatchetState = self.ratchet_storage.load(&record.local_convo_id)?; let dr_state: RatchetState = self.ratchet_storage.load(&record.local_convo_id)?;
Ok(PrivateV1Convo::from_stored( Ok(PrivateV1Convo::new(
record.local_convo_id, record.local_convo_id,
record.remote_convo_id, record.remote_convo_id,
dr_state, dr_state,

View File

@ -29,15 +29,14 @@ pub trait Convo: Id + Debug {
fn remote_id(&self) -> String; fn remote_id(&self) -> String;
/// Returns the conversation type identifier for storage. /// Returns the conversation type identifier for storage.
fn convo_type(&self) -> &str; fn convo_type(&self) -> ConversationKind;
/// Persists ratchet state to storage. Default is no-op. /// Persists ratchet state to storage. Default is no-op.
fn save_ratchet_state(&self, _storage: &mut RatchetStorage) -> Result<(), ChatError> { fn save_ratchet_state(&self, _storage: &mut RatchetStorage) -> Result<(), ChatError>;
Ok(())
}
} }
mod privatev1; mod privatev1;
use chat_proto::logoschat::encryption::EncryptedPayload; use chat_proto::logoschat::encryption::EncryptedPayload;
pub use privatev1::PrivateV1Convo; pub use privatev1::PrivateV1Convo;
use storage::ConversationKind;

View File

@ -10,6 +10,7 @@ use crypto::{PrivateKey, PublicKey, SymmetricKey32};
use double_ratchets::{Header, InstallationKeyPair, RatchetState}; use double_ratchets::{Header, InstallationKeyPair, RatchetState};
use prost::{Message, bytes::Bytes}; use prost::{Message, bytes::Bytes};
use std::fmt::Debug; use std::fmt::Debug;
use storage::ConversationKind;
use crate::{ use crate::{
conversation::{ChatError, ConversationId, Convo, Id}, conversation::{ChatError, ConversationId, Convo, Id},
@ -60,6 +61,15 @@ pub struct PrivateV1Convo {
} }
impl PrivateV1Convo { impl PrivateV1Convo {
/// Reconstructs a PrivateV1Convo from persisted metadata and ratchet state.
pub fn new(local_convo_id: String, remote_convo_id: String, dr_state: RatchetState) -> Self {
Self {
local_convo_id,
remote_convo_id,
dr_state,
}
}
pub fn new_initiator(seed_key: SymmetricKey32, remote: PublicKey) -> Self { pub fn new_initiator(seed_key: SymmetricKey32, remote: PublicKey) -> Self {
let base_convo_id = BaseConvoId::new(&seed_key); let base_convo_id = BaseConvoId::new(&seed_key);
let local_convo_id = base_convo_id.id_for_participant(Role::Initiator); let local_convo_id = base_convo_id.id_for_participant(Role::Initiator);
@ -78,19 +88,6 @@ impl PrivateV1Convo {
} }
} }
/// Reconstructs a PrivateV1Convo from persisted metadata and ratchet state.
pub fn from_stored(
local_convo_id: String,
remote_convo_id: String,
dr_state: RatchetState,
) -> Self {
Self {
local_convo_id,
remote_convo_id,
dr_state,
}
}
pub fn new_responder(seed_key: SymmetricKey32, dh_self: &PrivateKey) -> Self { pub fn new_responder(seed_key: SymmetricKey32, dh_self: &PrivateKey) -> Self {
let base_convo_id = BaseConvoId::new(&seed_key); let base_convo_id = BaseConvoId::new(&seed_key);
let local_convo_id = base_convo_id.id_for_participant(Role::Responder); let local_convo_id = base_convo_id.id_for_participant(Role::Responder);
@ -224,8 +221,8 @@ impl Convo for PrivateV1Convo {
self.remote_convo_id.clone() self.remote_convo_id.clone()
} }
fn convo_type(&self) -> &str { fn convo_type(&self) -> ConversationKind {
"private_v1" ConversationKind::PrivateV1
} }
fn save_ratchet_state(&self, storage: &mut RatchetStorage) -> Result<(), ChatError> { fn save_ratchet_state(&self, storage: &mut RatchetStorage) -> Result<(), ChatError> {