From 7803efec7f6f768f0f41d16530dec6d932ac2334 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 18 Sep 2023 18:29:21 +1000 Subject: [PATCH] pass room_alias aka dialogue_id aka dialogue.pddb_key to mtxchat.listen() this ensures that new Matrix Events received from the server are added to the correct chat::Dialogue --- apps/mtxchat/src/lib.rs | 18 +++++++++++------- apps/mtxchat/src/listen.rs | 2 ++ libs/chat/src/api.rs | 1 + libs/chat/src/lib.rs | 4 +++- libs/chat/src/ui.rs | 18 ++++++++++++------ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/apps/mtxchat/src/lib.rs b/apps/mtxchat/src/lib.rs index 08cb0877e..53c9993b3 100644 --- a/apps/mtxchat/src/lib.rs +++ b/apps/mtxchat/src/lib.rs @@ -557,18 +557,20 @@ impl<'a> MtxChat<'a> { &self.token, &self.room_id, &self.filter, + &self.room_alias(), ) { - (false, true, Some(token), Some(room_id), Some(filter)) => { + (false, true, Some(token), Some(room_id), Some(filter), Some(room_alias)) => { self.listening = true; std::thread::spawn({ let mut url = Url::parse("https://matrix.org").unwrap(); - if let Ok(host) = self.get(ROOM_DOMAIN_KEY){ + if let Ok(host) = self.get(ROOM_DOMAIN_KEY) { url.set_host(host.as_deref()).expect("failed to set host"); } let token = token.clone(); let room_id = room_id.clone(); let since = self.since.clone(); let filter = filter.clone(); + let room_alias = room_alias.clone(); let chat_cid = self.chat.cid().clone(); move || { listen( @@ -577,17 +579,19 @@ impl<'a> MtxChat<'a> { &room_id, since.as_deref(), &filter, + &room_alias, chat_cid, ); } }); "Started listening" } - (true, _, _, _, _) => "Already listening", - (_, false, _, _, _) => "Not logged in", - (_, _, None, _, _) => "No token set", - (_, _, _, None, _) => "No room id set", - (_, _, _, _, None) => "No filter set", + (true, _, _, _, _, _) => "Already listening", + (_, false, _, _, _, _) => "Not logged in", + (_, _, None, _, _, _) => "No token set", + (_, _, _, None, _, _) => "No room id set", + (_, _, _, _, None, _) => "No filter set", + (_, _, _, _, _, None) => "No room alias set", }; log::info!("{log_entry}"); } diff --git a/apps/mtxchat/src/listen.rs b/apps/mtxchat/src/listen.rs index 5c720ebff..9263ef882 100644 --- a/apps/mtxchat/src/listen.rs +++ b/apps/mtxchat/src/listen.rs @@ -13,6 +13,7 @@ pub fn listen( room_id: &str, since: Option<&str>, filter: &str, + dialogue_id: &str, chat_cid: CID, ) { let xns = xous_names::XousNames::new().unwrap(); @@ -46,6 +47,7 @@ pub fn listen( let sender = event.sender.unwrap_or("anon".to_string()); let body = event.body.unwrap_or("...".to_string()); let post = chat::Post { + dialogue_id: xous_ipc::String::from_str(&dialogue_id), author: xous_ipc::String::from_str(&get_username(&sender)), timestamp: event.ts.unwrap_or(0), text: xous_ipc::String::from_str(&body), diff --git a/libs/chat/src/api.rs b/libs/chat/src/api.rs index 582c43c40..2547a1892 100644 --- a/libs/chat/src/api.rs +++ b/libs/chat/src/api.rs @@ -65,6 +65,7 @@ pub struct Dialogue { #[derive(Archive, Serialize, Deserialize, Debug)] pub struct Post { + pub dialogue_id: xous_ipc::String<128>, pub author: xous_ipc::String<128>, pub timestamp: u64, pub text: xous_ipc::String, diff --git a/libs/chat/src/lib.rs b/libs/chat/src/lib.rs index bcf5f9137..e2a4b58dd 100644 --- a/libs/chat/src/lib.rs +++ b/libs/chat/src/lib.rs @@ -142,6 +142,7 @@ impl Chat { attach_url: Option<&str>, ) -> Result<(), Error> { let mut post = api::Post { + dialogue_id: xous_ipc::String::new(), author: xous_ipc::String::new(), timestamp: timestamp, text: xous_ipc::String::new(), @@ -389,9 +390,10 @@ pub fn server( log::info!("ChatOp::PostAdd"); let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; - match buffer.to_original::() { + match buffer.to_original::() { Ok(post) => ui .post_add( + post.dialogue_id.as_str().unwrap(), post.author.as_str().unwrap(), post.timestamp, post.text.as_str().unwrap(), diff --git a/libs/chat/src/ui.rs b/libs/chat/src/ui.rs index 21fa5508a..27c6009fe 100644 --- a/libs/chat/src/ui.rs +++ b/libs/chat/src/ui.rs @@ -343,18 +343,24 @@ impl Ui { /// pub fn post_add( &mut self, + dialogue_id: &str, author: &str, timestamp: u64, text: &str, attach_url: Option<&str>, ) -> Result<(), Error> { - match &mut self.dialogue { - Some(ref mut dialogue) => { - dialogue - .post_add(author, timestamp, text, attach_url) - .unwrap(); + match (&self.pddb_key, &mut self.dialogue) { + (Some(pddb_key), Some(ref mut dialogue)) => { + if pddb_key.eq(&dialogue_id) { + dialogue + .post_add(author, timestamp, text, attach_url) + .unwrap(); + } else { + log::warn!("dropping Post as dialogue_id does not match pddb_key"); + } } - None => log::warn!("no Dialogue available to add Post"), + (None, _) => log::warn!("no pddb_key set to match dialogue_id"), + (_, None) => log::warn!("no Dialogue available to add Post"), } Ok(()) }