From b2049fb4327bb8c027b16842f037d9482cddbb53 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Fri, 13 Jun 2025 17:47:29 -0700 Subject: [PATCH] Workaround for empty server messages (typing indicator) --- kordophone-db/src/repository.rs | 15 +++++++++++++-- kordophoned/src/daemon/mod.rs | 10 +++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/kordophone-db/src/repository.rs b/kordophone-db/src/repository.rs index 9fd70b1..8c4102f 100644 --- a/kordophone-db/src/repository.rs +++ b/kordophone-db/src/repository.rs @@ -250,6 +250,10 @@ impl<'a> Repository<'a> { Ok(()) })?; + // TODO: May need to update conversation metadata here, but this has a perf impact. + // Ideally we would consolidate this in the code above, assuming we're only inserting *new* messages, but + // this may not necessarily be the case. + Ok(()) } @@ -325,8 +329,15 @@ impl<'a> Repository<'a> { conversation_guid, last_message ); - conversation.date = last_message.date; - conversation.last_message_preview = Some(last_message.text.clone()); + + if last_message.date > conversation.date { + conversation.date = last_message.date; + } + + if !last_message.text.is_empty() && !last_message.text.trim().is_empty() { + conversation.last_message_preview = Some(last_message.text.clone()); + } + self.insert_conversation(conversation)?; } } diff --git a/kordophoned/src/daemon/mod.rs b/kordophoned/src/daemon/mod.rs index 0a78113..9876c0b 100644 --- a/kordophoned/src/daemon/mod.rs +++ b/kordophoned/src/daemon/mod.rs @@ -559,7 +559,15 @@ impl Daemon { let messages = client .get_messages(&conversation_id, None, None, last_message_id) .await?; - let db_messages: Vec = messages + + // Filter messages that have an empty body, or a body that is just whitespace. + // This is a workaround for a bug in the server where it returns messages with an empty body, which is usually + // the typing indicator or stuff like that. In the future, we need to move to ChatItems instead of Messages. + let insertable_messages: Vec = messages + .into_iter() + .filter(|m| !m.text.is_empty() && !m.text.trim().is_empty()).collect(); + + let db_messages: Vec = insertable_messages .into_iter() .map(kordophone_db::models::Message::from) .collect();