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
This commit is contained in:
nworbnhoj 2023-09-18 18:29:21 +10:00
parent 65cc7feb05
commit 7803efec7f
5 changed files with 29 additions and 14 deletions

View file

@ -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}");
}

View file

@ -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),

View file

@ -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<POST_TEXT_MAX>,

View file

@ -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::<Post, _>() {
match buffer.to_original::<api::Post, _>() {
Ok(post) => ui
.post_add(
post.dialogue_id.as_str().unwrap(),
post.author.as_str().unwrap(),
post.timestamp,
post.text.as_str().unwrap(),

View file

@ -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(())
}