Skip to content

Commit

Permalink
fix: trash no-op addition messages
Browse files Browse the repository at this point in the history
This partially restores the fix from c9cf2b7
that was removed during the addition of new group consistency at de63527
but only for "Member added" messages.

Multiple "Member added" messages happen
when the same QR code is processed multiple times
by multiple devices.
  • Loading branch information
link2xt committed Jan 15, 2025
1 parent 3e7b662 commit 498979c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5301,8 +5301,8 @@ mod tests {
// Bob receives the add and deletion messages out of order
let bob = TestContext::new_bob().await;
bob.recv_msg(&add1).await;
bob.recv_msg(&add3).await;
let bob_chat_id = bob.recv_msg(&add2).await.chat_id;
let bob_chat_id = bob.recv_msg(&add3).await.chat_id;
bob.recv_msg_trash(&add2).await; // No-op addition message is trashed.
assert_eq!(get_chat_contacts(&bob, bob_chat_id).await?.len(), 4);

bob.recv_msg(&remove2).await;
Expand Down
13 changes: 11 additions & 2 deletions src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,9 @@ async fn add_parts(

let mut txt_raw = "".to_string();
let (msg, typ): (&str, Viewtype) = if let Some(better_msg) = &better_msg {
if better_msg.is_empty() && is_partial_download.is_none() {
chat_id = DC_CHAT_ID_TRASH;
}
(better_msg, Viewtype::Text)
} else {
(&part.msg, part.typ)
Expand Down Expand Up @@ -2197,7 +2200,8 @@ async fn update_chats_contacts_timestamps(
/// Apply group member list, name, avatar and protection status changes from the MIME message.
///
/// Returns `Vec` of group changes messages and, optionally, a better message to replace the
/// original system message.
/// original system message. If the better message is empty, the original system message
/// should be trashed.
///
/// * `to_ids` - contents of the `To` and `Cc` headers.
/// * `past_ids` - contents of the `Chat-Group-Past-Members` header.
Expand Down Expand Up @@ -2394,7 +2398,12 @@ async fn apply_group_changes(
}

if let Some(added_id) = added_id {
added_ids.remove(&added_id);
if !added_ids.remove(&added_id) && !self_added {
// No-op "Member added" message.
//
// Trash it.
better_msg = Some(String::new());
}
}
if let Some(removed_id) = removed_id {
removed_ids.remove(&removed_id);
Expand Down
26 changes: 26 additions & 0 deletions src/receive_imf/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5049,6 +5049,32 @@ async fn test_unarchive_on_member_removal() -> Result<()> {
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_no_op_member_added_is_trash() -> Result<()> {
let mut tcm = TestContextManager::new();
let alice = &tcm.alice().await;
let bob = &tcm.bob().await;
let alice_chat_id = alice
.create_group_with_members(ProtectionStatus::Unprotected, "foos", &[bob])
.await;
send_text_msg(alice, alice_chat_id, "populate".to_string()).await?;
let msg = alice.pop_sent_msg().await;
bob.recv_msg(&msg).await;
let bob_chat_id = bob.get_last_msg().await.chat_id;
bob_chat_id.accept(bob).await?;

let fiona_id = Contact::create(alice, "", "[email protected]").await?;
add_contact_to_chat(alice, alice_chat_id, fiona_id).await?;
let msg = alice.pop_sent_msg().await;

let fiona_id = Contact::create(bob, "", "[email protected]").await?;
add_contact_to_chat(bob, bob_chat_id, fiona_id).await?;
bob.recv_msg_trash(&msg).await;
let contacts = get_chat_contacts(bob, bob_chat_id).await?;
assert_eq!(contacts.len(), 3);
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_forged_from() -> Result<()> {
let mut tcm = TestContextManager::new();
Expand Down

0 comments on commit 498979c

Please sign in to comment.