diff --git a/.agents/features/client-state.md b/.agents/features/client-state.md index 4155ddc..aa5e7c5 100644 --- a/.agents/features/client-state.md +++ b/.agents/features/client-state.md @@ -91,6 +91,11 @@ The F21 calls stay, for apps and tests without persistence: `send` = `compose` + relay's 7 days (the library has no clock). Without it an unclaimed invite's mailbox is fetched for ever. +10. **A forgotten contact leaves nothing behind** (`forget_contact`, added 2026-11-16 for Amicitia's + *Remove person*): its name is gone from the snapshot, its MLS group from storage (putting the + contact back into the snapshot is refused), and its mailbox is no longer fetched. The other side + is not told. + Planted bugs, for the review: `receive` acknowledges inside the call; `compose` posts; `restore` skips the storage map; `finish` acknowledges before it delivers; a restored client starts `next_contact` at 0, so two contacts share an id. diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index e7b0b1b..ed42938 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -301,6 +301,21 @@ impl Client { 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 { + 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. diff --git a/crates/client/tests/state.rs b/crates/client/tests/state.rs index 9e00a88..4ede982 100644 --- a/crates/client/tests/state.rs +++ b/crates/client/tests/state.rs @@ -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"); +}