From 45da43805af25bf4696f77072e16bd2d3f07e5fb Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 7 Aug 2026 23:53:12 +0200 Subject: [PATCH] [Fix] User States --- iota-connection/src/message_handlers.rs | 54 ++++++++++++++++++++++--- mtp-type-maps | 2 +- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/iota-connection/src/message_handlers.rs b/iota-connection/src/message_handlers.rs index 1330ef2..73244d7 100644 --- a/iota-connection/src/message_handlers.rs +++ b/iota-connection/src/message_handlers.rs @@ -296,6 +296,45 @@ fn contact_value( typed_container(fields) } +fn current_contact_ids(user_id: i64) -> DataValue { + contact_ids_value( + chats_util::get_users(user_id) + .into_iter() + .map(|contact| contact.user_id), + ) +} + +fn contact_ids_value(ids: impl IntoIterator) -> DataValue { + let mut contact_ids = ids.into_iter().collect::>(); + contact_ids.sort_unstable(); + contact_ids.dedup(); + + DataValue::Array( + contact_ids + .into_iter() + .map(|user_id| DataValue::SignedNumber(user_id as i128)) + .collect(), + ) +} + +#[cfg(test)] +mod presence_tests { + use super::contact_ids_value; + use mtp::codec::DataValue; + + #[test] + fn contact_snapshot_is_sorted_and_deduplicated() { + assert_eq!( + contact_ids_value([9, 3, 9, 4, 3]), + DataValue::Array(vec![ + DataValue::SignedNumber(3), + DataValue::SignedNumber(4), + DataValue::SignedNumber(9), + ]) + ); + } +} + fn sync_error(cv: &CommunicationValue) -> CommunicationValue { error_response(cv, CommunicationType::ErrorInvalidData).add_typed_default( DataType::SessionId, @@ -356,10 +395,6 @@ pub fn handle_client_connected(cv: &CommunicationValue) -> CommunicationValue { ), } }; - let all_contact_ids = chats_util::get_users(user_id) - .into_iter() - .map(|contact| DataValue::SignedNumber(contact.user_id as i128)) - .collect(); let message_values = messages .iter() .map(|message| stored_message_value(message, user_id, message.external_user)) @@ -408,7 +443,7 @@ pub fn handle_client_connected(cv: &CommunicationValue) -> CommunicationValue { .collect(), ), ) - .add_typed_default(DataType::UserIds, DataValue::Array(all_contact_ids)) + .add_typed_default(DataType::UserIds, current_contact_ids(user_id)) .add_typed_default(DataType::Calls, DataValue::Array(Vec::new())) } @@ -511,6 +546,10 @@ pub fn handle_get_chats(cv: &CommunicationValue) -> CommunicationValue { pub fn handle_add_conversation(cv: &CommunicationValue) -> CommunicationValue { let user_id = cv.get_sender(); + let session_id = match data_i64(cv, DataType::SessionId) { + Some(id) if id > 0 => id, + _ => return sync_error(cv), + }; let other_id = match cv.get_data(DataType::ChatPartnerId).as_number() { Some(n) => n as i64, None => cv @@ -532,6 +571,11 @@ pub fn handle_add_conversation(cv: &CommunicationValue) -> CommunicationValue { CommunicationValue::new(CommunicationType::AddConversation) .with_id(cv.get_id()) .with_receiver(user_id) + .add_typed_default( + DataType::SessionId, + DataValue::SignedNumber(session_id as i128), + ) + .add_typed_default(DataType::UserIds, current_contact_ids(user_id as i64)) } pub fn handle_add_community(cv: &CommunicationValue) -> CommunicationValue { diff --git a/mtp-type-maps b/mtp-type-maps index f430cd3..486541b 160000 --- a/mtp-type-maps +++ b/mtp-type-maps @@ -1 +1 @@ -Subproject commit f430cd358b3d07a4cfd6982eb8917fec80c24a7d +Subproject commit 486541b9483356ff49ff3ec7016f87d3ecbeaa0e