From 9480699362d92939dbf8e2db480fc3cbb8b27fe9 Mon Sep 17 00:00:00 2001 From: link2xt Date: Thu, 6 Jul 2023 01:22:01 +0000 Subject: [PATCH 1/4] test: test that "member removed" message is rewritten if self is not in chat --- src/chat.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/chat.rs b/src/chat.rs index 4db51c6c5..bc797a76e 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -4083,6 +4083,52 @@ mod tests { Ok(()) } + /// Test simultaneous removal of user from the chat and leaving the group. + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn test_simultaneous_member_remove() -> Result<()> { + let mut tcm = TestContextManager::new(); + + let alice = tcm.alice().await; + let bob = tcm.bob().await; + + alice.set_config(Config::E2eeEnabled, Some("0")).await?; + bob.set_config(Config::E2eeEnabled, Some("0")).await?; + + let alice_bob_contact_id = Contact::create(&alice, "Bob", "bob@example.net").await?; + let alice_fiona_contact_id = Contact::create(&alice, "Fiona", "fiona@example.net").await?; + + // Create and promote a group. + let alice_chat_id = + create_group_chat(&alice, ProtectionStatus::Unprotected, "Group chat").await?; + add_contact_to_chat(&alice, alice_chat_id, alice_bob_contact_id).await?; + add_contact_to_chat(&alice, alice_chat_id, alice_fiona_contact_id).await?; + let alice_sent_msg = alice + .send_text(alice_chat_id, "Hi! I created a group.") + .await; + let bob_received_msg = bob.recv_msg(&alice_sent_msg).await; + + let bob_chat_id = bob_received_msg.get_chat_id(); + bob_chat_id.accept(&bob).await?; + + // Alice removes Bob from the chat. + remove_contact_from_chat(&alice, alice_chat_id, alice_bob_contact_id).await?; + let alice_sent_remove_msg = alice.pop_sent_msg().await; + + // Bob leaves the chat. + remove_contact_from_chat(&bob, bob_chat_id, ContactId::SELF).await?; + + // Bob receives a msg about Alice removing him from the group. + let bob_received_remove_msg = bob.recv_msg(&alice_sent_remove_msg).await; + + // Test that the message is rewritten. + assert_eq!( + bob_received_remove_msg.get_text(), + "Member Me (bob@example.net) removed by alice@example.org." + ); + + Ok(()) + } + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_modify_chat_multi_device() -> Result<()> { let a1 = TestContext::new_alice().await; From b33ae3cd0fbc2eaad4c0e5185cb381a33db0fb85 Mon Sep 17 00:00:00 2001 From: link2xt Date: Thu, 6 Jul 2023 01:28:22 +0000 Subject: [PATCH 2/4] fix: rewrite "member removed" message even if change is not allowed --- src/receive_imf.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/receive_imf.rs b/src/receive_imf.rs index a146cb416..12b5e2d13 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -1682,6 +1682,13 @@ async fn apply_group_changes( if let Some(removed_addr) = mime_parser.get_header(HeaderDef::ChatGroupMemberRemoved) { removed_id = Contact::lookup_id_by_addr(context, removed_addr, Origin::Unknown).await?; + + better_msg = if removed_id == Some(from_id) { + Some(stock_str::msg_group_left_local(context, from_id).await) + } else { + Some(stock_str::msg_del_member_local(context, removed_addr, from_id).await) + }; + if let Some(contact_id) = removed_id { if allow_member_list_changes { // Remove a single member from the chat. @@ -1689,12 +1696,6 @@ async fn apply_group_changes( chat::remove_from_chat_contacts_table(context, chat_id, contact_id).await?; send_event_chat_modified = true; } - - better_msg = if contact_id == from_id { - Some(stock_str::msg_group_left_local(context, from_id).await) - } else { - Some(stock_str::msg_del_member_local(context, removed_addr, from_id).await) - }; } else { info!( context, From deed790950a104f14275975c3f0bd8e11d715830 Mon Sep 17 00:00:00 2001 From: link2xt Date: Thu, 6 Jul 2023 01:35:01 +0000 Subject: [PATCH 3/4] test: test that "member added" message is rewritten even if self is not in chat --- src/chat.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/chat.rs b/src/chat.rs index bc797a76e..db8a955d7 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -4096,6 +4096,8 @@ mod tests { let alice_bob_contact_id = Contact::create(&alice, "Bob", "bob@example.net").await?; let alice_fiona_contact_id = Contact::create(&alice, "Fiona", "fiona@example.net").await?; + let alice_claire_contact_id = + Contact::create(&alice, "Claire", "claire@example.net").await?; // Create and promote a group. let alice_chat_id = @@ -4110,6 +4112,10 @@ mod tests { let bob_chat_id = bob_received_msg.get_chat_id(); bob_chat_id.accept(&bob).await?; + // Alice adds Claire to the chat. + add_contact_to_chat(&alice, alice_chat_id, alice_claire_contact_id).await?; + let alice_sent_add_msg = alice.pop_sent_msg().await; + // Alice removes Bob from the chat. remove_contact_from_chat(&alice, alice_chat_id, alice_bob_contact_id).await?; let alice_sent_remove_msg = alice.pop_sent_msg().await; @@ -4117,10 +4123,19 @@ mod tests { // Bob leaves the chat. remove_contact_from_chat(&bob, bob_chat_id, ContactId::SELF).await?; + // Bob receives a msg about Alice adding Claire to the group. + let bob_received_add_msg = bob.recv_msg(&alice_sent_add_msg).await; + + // Test that add message is rewritten. + assert_eq!( + bob_received_add_msg.get_text(), + "Member claire@example.net added by alice@example.org." + ); + // Bob receives a msg about Alice removing him from the group. let bob_received_remove_msg = bob.recv_msg(&alice_sent_remove_msg).await; - // Test that the message is rewritten. + // Test that remove message is rewritten. assert_eq!( bob_received_remove_msg.get_text(), "Member Me (bob@example.net) removed by alice@example.org." From 64c218f1ead0f782291322b334da9db749581a70 Mon Sep 17 00:00:00 2001 From: link2xt Date: Thu, 6 Jul 2023 01:35:35 +0000 Subject: [PATCH 4/4] fix: rewrite "member added" message even if change is not allowed --- src/receive_imf.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/receive_imf.rs b/src/receive_imf.rs index 12b5e2d13..6db785650 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -1706,6 +1706,8 @@ async fn apply_group_changes( warn!(context, "Removed {removed_addr:?} has no contact id.") } } else if let Some(added_addr) = mime_parser.get_header(HeaderDef::ChatGroupMemberAdded) { + better_msg = Some(stock_str::msg_add_member_local(context, added_addr, from_id).await); + if allow_member_list_changes { // Add a single member to the chat. if !recreate_member_list { @@ -1718,8 +1720,6 @@ async fn apply_group_changes( warn!(context, "Added {added_addr:?} has no contact id.") } } - - better_msg = Some(stock_str::msg_add_member_local(context, added_addr, from_id).await); } else { info!(context, "Ignoring addition of {added_addr:?} to {chat_id}."); }