[client] F22a: forget_contact — drops the group, the name and the mailbox, for Remove person
ci / check (pull_request) Has been cancelled

This commit is contained in:
2026-11-16 02:08:24 +01:00
parent 4f78101735
commit 3ac2075ec6
3 changed files with 78 additions and 0 deletions
+15
View File
@@ -301,6 +301,21 @@ impl<T: Transport> Client<T> {
self.welcome_boxes.retain(|w| w.invite != *invite);
}
/// Forgets a contact: its MLS group is deleted from storage, its name and tokens from the
/// client, and its mailbox is no longer fetched. The other side is not told, and anything it
/// still posts waits on the relay until it expires. Save the snapshot after it. Returns whether
/// there was such a contact.
pub fn forget_contact(&mut self, contact: ContactId) -> Result<bool, ClientError> {
let Some(index) = self.contacts.iter().position(|c| c.id == contact) else {
return Ok(false);
};
let mut c = self.contacts.remove(index);
c.group
.delete(self.provider.storage())
.map_err(|_| ClientError::State)?;
Ok(true)
}
/// Step 2, on the other device, staged: claims the KeyPackage (which uses the link up) and
/// creates our mailbox, then makes the group. The Welcome and our `hello` come back for the
/// app to deliver after saving.
+58
View File
@@ -428,3 +428,61 @@ fn a_forgotten_invite_is_no_longer_fetched() {
"the Welcome and hello wait, never fetched"
);
}
fn contains(haystack: &[u8], needle: &[u8]) -> bool {
!needle.is_empty() && haystack.windows(needle.len()).any(|w| w == needle)
}
/// Forgetting a contact leaves no name in the snapshot, stops fetching its mailbox, and survives a
/// restart; the other contacts are untouched.
#[test]
fn a_forgotten_contact_leaves_nothing_behind() {
let relay = MemoryTransport::default();
let (mut eva, eva_side, mut jan, jan_side) = pair(&relay);
let mut petr = Client::new("Petr Zapomenutý", relay.clone()).expect("petr");
let invite = eva.create_invite().expect("invite");
let petr_side = petr.accept_invite(&invite).expect("accept");
let Some(Event::Joined {
contact: forgotten, ..
}) = eva.poll().expect("poll").first().cloned()
else {
panic!("no join")
};
petr.poll().expect("poll");
let before: serde_json::Value =
serde_json::from_slice(&eva.snapshot().expect("snapshot")).expect("json");
assert!(before.to_string().contains("Petr Zapomenutý"));
let petr_state = before["contacts"][1].clone();
assert_eq!(eva.forget_contact(forgotten), Ok(true));
assert_eq!(eva.forget_contact(forgotten), Ok(false), "only once");
let saved = eva.snapshot().expect("snapshot");
assert!(
!contains(&saved, "Petr Zapomenutý".as_bytes()),
"the name is gone"
);
// The group's MLS state is gone from storage too: putting the contact back is refused.
let mut back: serde_json::Value = serde_json::from_slice(&saved).expect("json");
back["contacts"]
.as_array_mut()
.expect("contacts")
.push(petr_state);
let back = serde_json::to_vec(&back).expect("json");
assert!(matches!(
C::restore(&back, relay.clone()),
Err(ClientError::State)
));
let mut eva = Client::restore(&saved, relay.clone()).expect("restore");
assert_eq!(eva.contact_name(forgotten), None);
petr.send(petr_side, "slyšíš mě?")
.expect("the relay still takes it");
jan.send(jan_side, "já ano").expect("send");
assert_eq!(texts(eva.poll().expect("poll")), ["já ano"]);
assert_eq!(relay.waiting_blobs(), 1, "Petr's message was never fetched");
assert_eq!(
eva.compose(forgotten, "x"),
Err(ClientError::UnknownContact)
);
eva.send(eva_side, "Jan still works").expect("send");
}