mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-05-12 13:09:29 +00:00
feat: move convo store to private v1
This commit is contained in:
parent
9c6d79d575
commit
033b6a7c50
@ -3,7 +3,7 @@ use std::{cell::RefCell, rc::Rc};
|
|||||||
|
|
||||||
use crypto::Identity;
|
use crypto::Identity;
|
||||||
use double_ratchets::{RatchetState, restore_ratchet_state};
|
use double_ratchets::{RatchetState, restore_ratchet_state};
|
||||||
use storage::{ChatStore, ConversationKind, ConversationMeta};
|
use storage::{ChatStore, ConversationKind};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
conversation::{Conversation, ConversationId, Convo, Id, PrivateV1Convo},
|
conversation::{Conversation, ConversationId, Convo, Id, PrivateV1Convo},
|
||||||
@ -83,7 +83,7 @@ impl<T: ChatStore> Context<T> {
|
|||||||
remote_bundle: &Introduction,
|
remote_bundle: &Introduction,
|
||||||
content: &[u8],
|
content: &[u8],
|
||||||
) -> Result<(ConversationIdOwned, Vec<AddressedEnvelope>), ChatError> {
|
) -> Result<(ConversationIdOwned, Vec<AddressedEnvelope>), ChatError> {
|
||||||
let (convo, payloads) = self
|
let (mut convo, payloads) = self
|
||||||
.inbox
|
.inbox
|
||||||
.invite_to_private_convo(remote_bundle, content, Rc::clone(&self.store))
|
.invite_to_private_convo(remote_bundle, content, Rc::clone(&self.store))
|
||||||
.unwrap_or_else(|_| todo!("Log/Surface Error"));
|
.unwrap_or_else(|_| todo!("Log/Surface Error"));
|
||||||
@ -94,7 +94,7 @@ impl<T: ChatStore> Context<T> {
|
|||||||
.map(|p| p.into_envelope(remote_id.clone()))
|
.map(|p| p.into_envelope(remote_id.clone()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let convo_id = self.persist_convo(&convo)?;
|
let convo_id = convo.persist()?;
|
||||||
Ok((convo_id, payload_bytes))
|
Ok((convo_id, payload_bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,7 +117,6 @@ impl<T: ChatStore> Context<T> {
|
|||||||
Conversation::Private(mut convo) => {
|
Conversation::Private(mut convo) => {
|
||||||
let payloads = convo.send_message(content)?;
|
let payloads = convo.send_message(content)?;
|
||||||
let remote_id = convo.remote_id();
|
let remote_id = convo.remote_id();
|
||||||
convo.save_ratchet_state::<T>(&mut *self.store.borrow_mut())?;
|
|
||||||
|
|
||||||
Ok(payloads
|
Ok(payloads
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@ -152,7 +151,7 @@ impl<T: ChatStore> Context<T> {
|
|||||||
.handle_frame(enc_payload, &public_key_hex, Rc::clone(&self.store))?;
|
.handle_frame(enc_payload, &public_key_hex, Rc::clone(&self.store))?;
|
||||||
|
|
||||||
match convo {
|
match convo {
|
||||||
Conversation::Private(convo) => self.persist_convo(&convo)?,
|
Conversation::Private(mut convo) => convo.persist()?,
|
||||||
};
|
};
|
||||||
|
|
||||||
self.store
|
self.store
|
||||||
@ -172,7 +171,6 @@ impl<T: ChatStore> Context<T> {
|
|||||||
match convo {
|
match convo {
|
||||||
Conversation::Private(mut convo) => {
|
Conversation::Private(mut convo) => {
|
||||||
let result = convo.handle_frame(enc_payload)?;
|
let result = convo.handle_frame(enc_payload)?;
|
||||||
convo.save_ratchet_state(&mut *self.store.borrow_mut())?;
|
|
||||||
Ok(result)
|
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)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -9,10 +9,11 @@ use chat_proto::logoschat::{
|
|||||||
use crypto::{PrivateKey, PublicKey, SymmetricKey32};
|
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::{cell::RefCell, fmt::Debug, rc::Rc};
|
use std::{cell::RefCell, fmt::Debug, rc::Rc, sync::Arc};
|
||||||
use storage::{ConversationKind, ConversationStore};
|
use storage::{ConversationKind, ConversationMeta, ConversationStore};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
context::ConversationIdOwned,
|
||||||
conversation::{ChatError, ConversationId, Convo, Id},
|
conversation::{ChatError, ConversationId, Convo, Id},
|
||||||
errors::EncryptionError,
|
errors::EncryptionError,
|
||||||
proto,
|
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> {
|
pub fn save_ratchet_state<T: RatchetStore>(&self, storage: &mut T) -> Result<(), ChatError> {
|
||||||
let record = to_ratchet_record(&self.dr_state);
|
let record = to_ratchet_record(&self.dr_state);
|
||||||
let skipped_keys = to_skipped_key_records(&self.dr_state.skipped_keys());
|
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);
|
let data = self.encrypt(frame);
|
||||||
|
|
||||||
|
self.save_ratchet_state::<S>(&mut *self.store.borrow_mut())?;
|
||||||
|
|
||||||
Ok(vec![AddressedEncryptedPayload {
|
Ok(vec![AddressedEncryptedPayload {
|
||||||
delivery_address: "delivery_address".into(),
|
delivery_address: "delivery_address".into(),
|
||||||
data,
|
data,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user