Skip to content

Commit

Permalink
fix: do not create tombstones for members removed from unpromoted groups
Browse files Browse the repository at this point in the history
If we create an unpromoted group,
add a member there and then remove it
before we promote a group, there is no need to
add such member to the list of past members
and send the address of this member to the group
when it is promoted.
  • Loading branch information
link2xt committed Jan 20, 2025
1 parent d4b1f86 commit 19a8416
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3955,7 +3955,18 @@ pub async fn remove_contact_from_chat(
} else {
let mut sync = Nosync;

remove_from_chat_contacts_table(context, chat_id, contact_id).await?;
if chat.is_promoted() {
remove_from_chat_contacts_table(context, chat_id, contact_id).await?;
} else {
context
.sql
.execute(
"DELETE FROM chats_contacts
WHERE chat_id=? AND contact_id=?",
(chat_id, contact_id),
)
.await?;
}

// We do not return an error if the contact does not exist in the database.
// This allows to delete dangling references to deleted contacts
Expand Down Expand Up @@ -8100,4 +8111,43 @@ mod tests {

Ok(())
}

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

let alice = &tcm.alice().await;
let bob = &tcm.bob().await;

let bob_addr = bob.get_config(Config::Addr).await?.unwrap();
let alice_bob_contact_id = Contact::create(alice, "Bob", &bob_addr).await?;
let fiona_addr = "[email protected]";
let alice_fiona_contact_id = Contact::create(alice, "Fiona", fiona_addr).await?;

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?;
assert_eq!(get_chat_contacts(alice, alice_chat_id).await?.len(), 3);
assert_eq!(get_past_chat_contacts(alice, alice_chat_id).await?.len(), 0);

remove_contact_from_chat(alice, alice_chat_id, alice_fiona_contact_id).await?;
assert_eq!(get_chat_contacts(alice, alice_chat_id).await?.len(), 2);

// There should be no tombstone because the group is not promoted yet.
assert_eq!(get_past_chat_contacts(alice, alice_chat_id).await?.len(), 0);

let sent = alice.send_text(alice_chat_id, "Hello group!").await;
let payload = sent.payload();
assert_eq!(payload.contains("Hello group!"), true);
assert_eq!(payload.contains(&bob_addr), true);
assert_eq!(payload.contains(fiona_addr), false);

let bob_msg = bob.recv_msg(&sent).await;
let bob_chat_id = bob_msg.chat_id;
assert_eq!(get_chat_contacts(bob, bob_chat_id).await?.len(), 2);
assert_eq!(get_past_chat_contacts(bob, bob_chat_id).await?.len(), 0);

Ok(())
}
}

0 comments on commit 19a8416

Please sign in to comment.