fix(dms): keep a group DM's identity when an agent is removed

A removal releases a DM's identity only when the remaining members no
longer have it. Agents are not part of a DM of people's identity, so
kicking one leaves the DM as that group's DM; a DM of someone and their
agents, identified by all its members, is still released.

Signed-off-by: morgmart <98432065+morgmart@users.noreply.github.com>
Co-authored-by: Carl <c217fe6b9d958f41c3a5e030dccc7f626775a923089cb6491305eade75ea1f1b@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
morgmart
2026-11-05 12:15:49 -08:00
parent 19a856caed
commit d0315d5669
2 changed files with 81 additions and 13 deletions

View File

@@ -653,25 +653,50 @@ pub async fn grow_dm(
})
}
/// Stop `channel_id`, if it is a DM, from being any set of people's DM, after
/// a member was removed from it. Its fingerprint would otherwise name someone
/// no longer in it, so neither they nor the rest could reach that group again.
/// The DM keeps its history for those still in it; opening that group again
/// starts a new DM. The caller holds the channel's membership lock, as every
/// membership writer does, so this cannot race [`grow_dm`].
pub(crate) async fn release_dm_identity<'e>(
executor: impl sqlx::PgExecutor<'e>,
/// Keep `channel_id`'s fingerprint, if it is a DM, true to who is still in it
/// after a member was removed. When the remaining members no longer have that
/// fingerprint (a person left, or the DM was identified by all its members),
/// the DM stops being any group's DM: it keeps its history for those still in
/// it, and opening that group again starts a new DM. Removing an agent from a
/// DM of people changes nothing. The caller holds the channel's membership
/// lock, as every membership writer does, so this cannot race [`grow_dm`].
pub(crate) async fn release_dm_identity(
conn: &mut sqlx::PgConnection,
community_id: CommunityId,
channel_id: Uuid,
) -> Result<()> {
sqlx::query(
"UPDATE channels SET participant_hash = NULL \
WHERE community_id = $1 AND id = $2 AND channel_type = 'dm' \
AND participant_hash IS NOT NULL",
let stored: Option<Vec<u8>> = sqlx::query_scalar(
"SELECT participant_hash FROM channels \
WHERE community_id = $1 AND id = $2 AND channel_type = 'dm'",
)
.bind(community_id.as_uuid())
.bind(channel_id)
.execute(executor)
.fetch_optional(&mut *conn)
.await?
.flatten();
let Some(stored) = stored else {
return Ok(());
};
let members: Vec<Vec<u8>> = sqlx::query_scalar(
"SELECT pubkey FROM channel_members \
WHERE community_id = $1 AND channel_id = $2 AND removed_at IS NULL",
)
.bind(community_id.as_uuid())
.bind(channel_id)
.fetch_all(&mut *conn)
.await?;
let members: Vec<&[u8]> = members.iter().map(Vec::as_slice).collect();
let agents = agents_among(&mut *conn, community_id, &members).await?;
if dm_identity_hash(&members, &agents).as_slice() == stored.as_slice() {
return Ok(());
}
sqlx::query(
"UPDATE channels SET participant_hash = NULL \
WHERE community_id = $1 AND id = $2",
)
.bind(community_id.as_uuid())
.bind(channel_id)
.execute(&mut *conn)
.await?;
Ok(())
}

View File

@@ -614,3 +614,46 @@ async fn a_kick_from_a_dm_releases_its_identity() {
.expect("message the group again");
assert_ne!(again, dm);
}
/// Agents are not part of a DM of people's identity, so kicking one leaves
/// the DM as that group's DM; kicking an agent from a DM of someone and
/// their agent releases it, since all its members identify it.
/// Mutation: release on every removal → the group gets a new DM → RED;
/// never release when an agent is kicked → the owner's reopen finds the
/// emptied agent DM → RED.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn kicking_an_agent_keeps_a_group_dm_its_identity() {
let state = state().await;
let tenant = community(&state, "dm-kick-agent").await;
let (a, b, agent) = (Keys::generate(), Keys::generate(), Keys::generate());
make_agent(&state, &tenant, &agent, &a, "anyone").await;
let group = open(&state, &tenant, &a, &[&b, &agent])
.await
.expect("group");
let own = open(&state, &tenant, &a, &[&agent]).await.expect("own");
for dm in [group, own] {
state
.db
.deploy_kick_member(
tenant.community(),
dm,
&agent.public_key().to_bytes(),
&a.public_key().to_bytes(),
)
.await
.expect("kick");
}
assert_eq!(
open(&state, &tenant, &b, &[&a]).await.expect("reopen"),
group
);
assert_ne!(
open(&state, &tenant, &a, &[&agent])
.await
.expect("own again"),
own
);
}