mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-05-12 13:09:29 +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 std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
use crypto::Identity;
|
use crypto::Identity;
|
||||||
use double_ratchets::{RatchetState, restore_ratchet_state};
|
|
||||||
use storage::{ChatStore, ConversationKind};
|
use storage::{ChatStore, ConversationKind};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -181,7 +180,7 @@ impl<T: ChatStore> Context<T> {
|
|||||||
Ok(intro.into())
|
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> {
|
fn load_convo(&self, convo_id: ConversationId) -> Result<Conversation<T>, ChatError> {
|
||||||
let record = self
|
let record = self
|
||||||
.store
|
.store
|
||||||
@ -191,22 +190,12 @@ impl<T: ChatStore> Context<T> {
|
|||||||
|
|
||||||
match record.kind {
|
match record.kind {
|
||||||
ConversationKind::PrivateV1 => {
|
ConversationKind::PrivateV1 => {
|
||||||
let dr_record = self
|
let private_convo = PrivateV1Convo::new(
|
||||||
.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(
|
|
||||||
record.local_convo_id,
|
record.local_convo_id,
|
||||||
record.remote_convo_id,
|
record.remote_convo_id,
|
||||||
dr_state,
|
self.store.clone(),
|
||||||
Rc::clone(&self.store),
|
)?;
|
||||||
)))
|
Ok(Conversation::Private(private_convo))
|
||||||
}
|
}
|
||||||
ConversationKind::Unknown(_) => Err(ChatError::BadBundleValue(format!(
|
ConversationKind::Unknown(_) => Err(ChatError::BadBundleValue(format!(
|
||||||
"unsupported conversation type: {}",
|
"unsupported conversation type: {}",
|
||||||
@ -336,7 +325,6 @@ mod tests {
|
|||||||
let content = alice.handle_payload(&payload.data).unwrap().unwrap();
|
let content = alice.handle_payload(&payload.data).unwrap().unwrap();
|
||||||
let alice_convo_id = content.conversation_id;
|
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 payloads = alice.send_content(&alice_convo_id, b"reply 1").unwrap();
|
||||||
let payload = payloads.first().unwrap();
|
let payload = payloads.first().unwrap();
|
||||||
bob.handle_payload(&payload.data).unwrap().unwrap();
|
bob.handle_payload(&payload.data).unwrap().unwrap();
|
||||||
|
|||||||
@ -7,7 +7,7 @@ use chat_proto::logoschat::{
|
|||||||
encryption::{Doubleratchet, EncryptedPayload, encrypted_payload::Encryption},
|
encryption::{Doubleratchet, EncryptedPayload, encrypted_payload::Encryption},
|
||||||
};
|
};
|
||||||
use crypto::{PrivateKey, PublicKey, SymmetricKey32};
|
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 prost::{Message, bytes::Bytes};
|
||||||
use std::{cell::RefCell, fmt::Debug, rc::Rc, sync::Arc};
|
use std::{cell::RefCell, fmt::Debug, rc::Rc, sync::Arc};
|
||||||
use storage::{ConversationKind, ConversationMeta, ConversationStore};
|
use storage::{ConversationKind, ConversationMeta, ConversationStore};
|
||||||
@ -68,15 +68,18 @@ impl<S: ConversationStore + RatchetStore> PrivateV1Convo<S> {
|
|||||||
pub fn new(
|
pub fn new(
|
||||||
local_convo_id: String,
|
local_convo_id: String,
|
||||||
remote_convo_id: String,
|
remote_convo_id: String,
|
||||||
dr_state: RatchetState,
|
|
||||||
store: Rc<RefCell<S>>,
|
store: Rc<RefCell<S>>,
|
||||||
) -> Self {
|
) -> Result<Self, ChatError> {
|
||||||
Self {
|
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,
|
local_convo_id,
|
||||||
remote_convo_id,
|
remote_convo_id,
|
||||||
dr_state,
|
dr_state,
|
||||||
store,
|
store,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_initiator(
|
pub fn new_initiator(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user