mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-05-12 04:59:27 +00:00
feat: clean context
This commit is contained in:
parent
033b6a7c50
commit
31792d2db1
@ -2,7 +2,6 @@ use std::sync::Arc;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use crypto::Identity;
|
||||
use double_ratchets::{RatchetState, restore_ratchet_state};
|
||||
use storage::{ChatStore, ConversationKind};
|
||||
|
||||
use crate::{
|
||||
@ -181,7 +180,7 @@ impl<T: ChatStore> Context<T> {
|
||||
Ok(intro.into())
|
||||
}
|
||||
|
||||
/// Loads a conversation from DB by constructing it from metadata + ratchet state.
|
||||
/// Loads a conversation from DB by constructing it from metadata.
|
||||
fn load_convo(&self, convo_id: ConversationId) -> Result<Conversation<T>, ChatError> {
|
||||
let record = self
|
||||
.store
|
||||
@ -191,22 +190,12 @@ impl<T: ChatStore> Context<T> {
|
||||
|
||||
match record.kind {
|
||||
ConversationKind::PrivateV1 => {
|
||||
let dr_record = self
|
||||
.store
|
||||
.borrow()
|
||||
.load_ratchet_state(&record.local_convo_id)?;
|
||||
let skipped_keys = self
|
||||
.store
|
||||
.borrow()
|
||||
.load_skipped_keys(&record.local_convo_id)?;
|
||||
let dr_state: RatchetState = restore_ratchet_state(dr_record, skipped_keys);
|
||||
|
||||
Ok(Conversation::Private(PrivateV1Convo::new(
|
||||
let private_convo = PrivateV1Convo::new(
|
||||
record.local_convo_id,
|
||||
record.remote_convo_id,
|
||||
dr_state,
|
||||
Rc::clone(&self.store),
|
||||
)))
|
||||
self.store.clone(),
|
||||
)?;
|
||||
Ok(Conversation::Private(private_convo))
|
||||
}
|
||||
ConversationKind::Unknown(_) => Err(ChatError::BadBundleValue(format!(
|
||||
"unsupported conversation type: {}",
|
||||
@ -336,7 +325,6 @@ mod tests {
|
||||
let content = alice.handle_payload(&payload.data).unwrap().unwrap();
|
||||
let alice_convo_id = content.conversation_id;
|
||||
|
||||
// Exchange a few messages to advance ratchet state
|
||||
let payloads = alice.send_content(&alice_convo_id, b"reply 1").unwrap();
|
||||
let payload = payloads.first().unwrap();
|
||||
bob.handle_payload(&payload.data).unwrap().unwrap();
|
||||
|
||||
@ -7,7 +7,7 @@ use chat_proto::logoschat::{
|
||||
encryption::{Doubleratchet, EncryptedPayload, encrypted_payload::Encryption},
|
||||
};
|
||||
use crypto::{PrivateKey, PublicKey, SymmetricKey32};
|
||||
use double_ratchets::{Header, InstallationKeyPair, RatchetState};
|
||||
use double_ratchets::{Header, InstallationKeyPair, RatchetState, restore_ratchet_state};
|
||||
use prost::{Message, bytes::Bytes};
|
||||
use std::{cell::RefCell, fmt::Debug, rc::Rc, sync::Arc};
|
||||
use storage::{ConversationKind, ConversationMeta, ConversationStore};
|
||||
@ -68,15 +68,18 @@ impl<S: ConversationStore + RatchetStore> PrivateV1Convo<S> {
|
||||
pub fn new(
|
||||
local_convo_id: String,
|
||||
remote_convo_id: String,
|
||||
dr_state: RatchetState,
|
||||
store: Rc<RefCell<S>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
) -> 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)?;
|
||||
let dr_state: RatchetState = restore_ratchet_state(dr_record, skipped_keys);
|
||||
|
||||
Ok(Self {
|
||||
local_convo_id,
|
||||
remote_convo_id,
|
||||
dr_state,
|
||||
store,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn new_initiator(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user