2026-01-28 07:07:35 +07:00
|
|
|
use std::{collections::HashMap, rc::Rc, sync::Arc};
|
2025-12-22 09:40:46 -08:00
|
|
|
|
2026-01-22 06:39:09 +07:00
|
|
|
use crate::{
|
2026-02-06 23:41:12 +07:00
|
|
|
conversation::{ConversationId, ConversationStore, Convo, Id},
|
2026-01-29 00:59:07 +07:00
|
|
|
errors::ChatError,
|
2026-01-22 06:39:09 +07:00
|
|
|
identity::Identity,
|
|
|
|
|
inbox::Inbox,
|
2026-02-06 23:41:12 +07:00
|
|
|
proto::{EncryptedPayload, EnvelopeV1, Message},
|
2026-01-29 23:36:18 +07:00
|
|
|
types::{AddressedEnvelope, ContentData},
|
2026-01-22 06:39:09 +07:00
|
|
|
};
|
2025-12-22 09:40:46 -08:00
|
|
|
|
2026-01-26 23:53:44 +07:00
|
|
|
pub use crate::inbox::Introduction;
|
2026-01-29 00:59:07 +07:00
|
|
|
|
|
|
|
|
//Offset handles to make debuging easier
|
|
|
|
|
const INITIAL_CONVO_HANDLE: u32 = 0xF5000001;
|
|
|
|
|
|
|
|
|
|
/// Used to identify a conversation on the othersize of the FFI.
|
2026-01-29 01:50:41 +07:00
|
|
|
pub type ConvoHandle = u32;
|
2026-01-29 00:59:07 +07:00
|
|
|
|
2026-01-22 06:39:09 +07:00
|
|
|
// This is the main entry point to the conversations api.
|
|
|
|
|
// Ctx manages lifetimes of objects to process and generate payloads.
|
|
|
|
|
pub struct Context {
|
|
|
|
|
_identity: Rc<Identity>,
|
2025-12-22 09:40:46 -08:00
|
|
|
store: ConversationStore,
|
2026-01-22 06:39:09 +07:00
|
|
|
inbox: Inbox,
|
2026-01-29 00:59:07 +07:00
|
|
|
convo_handle_map: HashMap<u32, Arc<str>>,
|
|
|
|
|
next_convo_handle: ConvoHandle,
|
2025-12-22 09:40:46 -08:00
|
|
|
}
|
|
|
|
|
|
2026-01-22 06:39:09 +07:00
|
|
|
impl Context {
|
2025-12-22 09:40:46 -08:00
|
|
|
pub fn new() -> Self {
|
2026-01-22 06:39:09 +07:00
|
|
|
let identity = Rc::new(Identity::new());
|
|
|
|
|
let inbox = Inbox::new(Rc::clone(&identity)); //
|
2025-12-22 09:40:46 -08:00
|
|
|
Self {
|
2026-01-22 06:39:09 +07:00
|
|
|
_identity: identity,
|
2025-12-22 09:40:46 -08:00
|
|
|
store: ConversationStore::new(),
|
2026-01-22 06:39:09 +07:00
|
|
|
inbox,
|
2026-01-29 00:59:07 +07:00
|
|
|
convo_handle_map: HashMap::new(),
|
|
|
|
|
next_convo_handle: INITIAL_CONVO_HANDLE,
|
2025-12-22 09:40:46 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 06:39:09 +07:00
|
|
|
pub fn create_private_convo(
|
|
|
|
|
&mut self,
|
2026-01-26 23:53:44 +07:00
|
|
|
remote_bundle: &Introduction,
|
2026-02-06 23:08:06 +07:00
|
|
|
content: &[u8],
|
2026-01-29 23:36:18 +07:00
|
|
|
) -> (ConvoHandle, Vec<AddressedEnvelope>) {
|
2026-01-26 23:53:44 +07:00
|
|
|
let (convo, payloads) = self
|
2026-01-22 06:39:09 +07:00
|
|
|
.inbox
|
|
|
|
|
.invite_to_private_convo(remote_bundle, content)
|
|
|
|
|
.unwrap_or_else(|_| todo!("Log/Surface Error"));
|
|
|
|
|
|
2026-01-29 23:36:18 +07:00
|
|
|
let payload_bytes = payloads
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|p| p.to_envelope(convo.id().to_string()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
2026-02-06 23:41:12 +07:00
|
|
|
let convo_handle = self.add_convo(Box::new(convo));
|
2026-01-29 23:36:18 +07:00
|
|
|
(convo_handle, payload_bytes)
|
2025-12-22 09:40:46 -08:00
|
|
|
}
|
|
|
|
|
|
2026-01-29 23:36:18 +07:00
|
|
|
pub fn send_content(
|
|
|
|
|
&mut self,
|
|
|
|
|
convo_handle: ConvoHandle,
|
|
|
|
|
content: &[u8],
|
|
|
|
|
) -> Result<Vec<AddressedEnvelope>, ChatError> {
|
|
|
|
|
// Lookup convo from handle
|
|
|
|
|
let convo = self.get_convo_mut(convo_handle)?;
|
|
|
|
|
|
|
|
|
|
// Generate encrypted payloads
|
|
|
|
|
let payloads = convo.send_message(content)?;
|
|
|
|
|
|
|
|
|
|
// Attach conversation_ids to Envelopes
|
|
|
|
|
Ok(payloads
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|p| p.to_envelope(convo.remote_id()))
|
|
|
|
|
.collect())
|
2025-12-22 09:40:46 -08:00
|
|
|
}
|
|
|
|
|
|
2026-02-06 23:41:12 +07:00
|
|
|
// Decode bytes and send to protocol for processing.
|
|
|
|
|
pub fn handle_payload(&mut self, payload: &[u8]) -> Result<Option<ContentData>, ChatError> {
|
|
|
|
|
let env = match EnvelopeV1::decode(payload) {
|
|
|
|
|
Ok(v) => v,
|
|
|
|
|
Err(e) => return Err(e.into()),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO: Impl Conversation hinting
|
|
|
|
|
let convo_id = env.conversation_hint;
|
|
|
|
|
let enc = EncryptedPayload::decode(payload)?;
|
|
|
|
|
|
|
|
|
|
match convo_id {
|
|
|
|
|
c if c == self.inbox.id() => self.dispatch_to_inbox(enc),
|
|
|
|
|
c if self.store.has(&c) => self.dispatch_to_convo(&c, enc),
|
|
|
|
|
_ => Err(ChatError::NoConvo(0)), // TODO: Remove ConvoHandle type
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Dispatch encrypted payload to Inbox, and register the created Conversation
|
|
|
|
|
fn dispatch_to_inbox(
|
|
|
|
|
&mut self,
|
|
|
|
|
enc_payload: EncryptedPayload,
|
|
|
|
|
) -> Result<Option<ContentData>, ChatError> {
|
|
|
|
|
let (convo, content) = self.inbox.handle_frame(enc_payload)?;
|
|
|
|
|
self.add_convo(convo);
|
|
|
|
|
Ok(content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Dispatch encrypted payload to its corresponding conversation
|
|
|
|
|
fn dispatch_to_convo(
|
|
|
|
|
&mut self,
|
|
|
|
|
convo_id: ConversationId,
|
|
|
|
|
enc_payload: EncryptedPayload,
|
|
|
|
|
) -> Result<Option<ContentData>, ChatError> {
|
|
|
|
|
let Some(convo) = self.store.get_mut(&convo_id) else {
|
|
|
|
|
return Err(ChatError::Protocol("convo id not found".into()));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
convo.handle_frame(enc_payload)
|
2025-12-22 09:40:46 -08:00
|
|
|
}
|
2026-01-29 00:59:07 +07:00
|
|
|
|
|
|
|
|
pub fn create_intro_bundle(&mut self) -> Result<Vec<u8>, ChatError> {
|
|
|
|
|
let pkb = self.inbox.create_bundle();
|
|
|
|
|
Ok(Introduction::from(pkb).into())
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 23:41:12 +07:00
|
|
|
fn add_convo(&mut self, convo: Box<dyn Convo>) -> ConvoHandle {
|
2026-01-29 00:59:07 +07:00
|
|
|
let handle = self.next_convo_handle;
|
|
|
|
|
self.next_convo_handle += 1;
|
|
|
|
|
let convo_id = self.store.insert_convo(convo);
|
|
|
|
|
self.convo_handle_map.insert(handle, convo_id);
|
|
|
|
|
|
|
|
|
|
handle
|
|
|
|
|
}
|
2026-01-29 23:36:18 +07:00
|
|
|
|
|
|
|
|
// Returns a mutable reference to a Convo for a given ConvoHandle
|
|
|
|
|
fn get_convo_mut(&mut self, handle: ConvoHandle) -> Result<&mut dyn Convo, ChatError> {
|
|
|
|
|
let convo_id = self
|
|
|
|
|
.convo_handle_map
|
|
|
|
|
.get(&handle)
|
|
|
|
|
.ok_or_else(|| ChatError::NoConvo(handle))?
|
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
|
|
self.store
|
|
|
|
|
.get_mut(&convo_id)
|
|
|
|
|
.ok_or_else(|| ChatError::NoConvo(handle))
|
|
|
|
|
}
|
2025-12-22 09:40:46 -08:00
|
|
|
}
|
2026-01-22 06:39:09 +07:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::conversation::GroupTestConvo;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn convo_store_get() {
|
|
|
|
|
let mut store: ConversationStore = ConversationStore::new();
|
|
|
|
|
|
|
|
|
|
let new_convo = GroupTestConvo::new();
|
2026-02-06 23:41:12 +07:00
|
|
|
let convo_id = store.insert_convo(Box::new(new_convo));
|
2026-01-22 06:39:09 +07:00
|
|
|
|
|
|
|
|
let convo = store.get_mut(&convo_id).ok_or_else(|| 0);
|
|
|
|
|
convo.unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|