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::{
|
|
|
|
|
conversation::{ConversationId, ConversationIdOwned, ConversationStore},
|
|
|
|
|
identity::Identity,
|
|
|
|
|
inbox::Inbox,
|
2026-01-26 23:53:44 +07:00
|
|
|
proto,
|
2026-01-22 06:39:09 +07:00
|
|
|
types::{ContentData, PayloadData},
|
|
|
|
|
};
|
2025-12-22 09:40:46 -08:00
|
|
|
|
2026-01-26 23:53:44 +07:00
|
|
|
pub use crate::inbox::Introduction;
|
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,
|
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,
|
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-01-22 06:39:09 +07:00
|
|
|
content: String,
|
2026-01-28 07:07:35 +07:00
|
|
|
) -> (ConversationIdOwned, Vec<PayloadData>) {
|
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-26 23:53:44 +07:00
|
|
|
let convo_id = self.store.insert_convo(convo);
|
|
|
|
|
(convo_id, payloads)
|
2025-12-22 09:40:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn send_content(&mut self, _convo_id: ConversationId, _content: &[u8]) -> Vec<PayloadData> {
|
|
|
|
|
// !TODO Replace Mock
|
|
|
|
|
vec![PayloadData {
|
|
|
|
|
delivery_address: _convo_id.into(),
|
|
|
|
|
data: vec![40, 30, 20, 10],
|
|
|
|
|
}]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn handle_payload(&mut self, _payload: &[u8]) -> Option<ContentData> {
|
|
|
|
|
// !TODO Replace Mock
|
|
|
|
|
Some(ContentData {
|
|
|
|
|
conversation_id: "convo_id".into(),
|
|
|
|
|
data: vec![1, 2, 3, 4, 5, 6],
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
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();
|
|
|
|
|
let convo_id = store.insert_convo(new_convo);
|
|
|
|
|
|
|
|
|
|
let convo = store.get_mut(&convo_id).ok_or_else(|| 0);
|
|
|
|
|
convo.unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|