diff --git a/crates/voelin-core/src/link/tests.rs b/crates/voelin-core/src/link/tests.rs index 426df25..f7be1d4 100644 --- a/crates/voelin-core/src/link/tests.rs +++ b/crates/voelin-core/src/link/tests.rs @@ -2111,3 +2111,28 @@ async fn a_transfer_waits_for_the_server_for_as_long_as_the_caller_does() { tokio::time::sleep(Duration::from_millis(50)).await; assert!(reply.is_closed(), "the link's job outlived its caller"); } + +/// A sender with no unique id who is not on the snapshot is named all the +/// same: the name the link gave goes into LIVE as a user of its own. +#[tokio::test(flavor = "multi_thread")] +async fn a_poke_from_a_sender_with_no_unique_id_outside_the_snapshot_is_named() { + let mut rig = Rig::new(everything()).await; + let poke = |name: &str| LinkEvent::Poke { + from: 77, + from_uid: None, + from_name: name.into(), + message: "boo".into(), + }; + async fn named(rig: &mut Rig) -> (Option, String) { + rig.event(|e| match e { + LinkEvent::Poke { from: 77, from_uid, from_name, .. } => Some((from_uid, from_name)), + _ => None, + }) + .await + } + rig.play(poke("ghost")); + assert_eq!(named(&mut rig).await, (None, "ghost".to_owned())); + // The link gives the client id to whoever connects next: the name follows. + rig.play(poke("ghost two")); + assert_eq!(named(&mut rig).await, (None, "ghost two".to_owned())); +} diff --git a/crates/voelin-core/src/ts_session/driver.rs b/crates/voelin-core/src/ts_session/driver.rs index bd2efec..1f342a7 100644 --- a/crates/voelin-core/src/ts_session/driver.rs +++ b/crates/voelin-core/src/ts_session/driver.rs @@ -881,15 +881,33 @@ impl Driver { let participant = client.map_or(0, u32::from); match uid.filter(|u| !u.is_empty()) { Some(uid) => (self.extra_user(uid, name), participant), - None if client.is_some() => { - // A sender with no unique id: by its client id. - let id = ids::synthetic_user(&format!("client:{}", participant)); - (id, participant) - } + None if client.is_some() => (self.extra_client(participant, name), participant), None => (0, participant), } } + /// A user for a sender with no unique id who is not on the snapshot, + /// made from its client id and put into LIVE with the name the link + /// gave, so that whoever reads the poke or the message can name it. The + /// link hands a client id to whoever connects next: the name follows the + /// sender. + fn extra_client(&mut self, client: u32, name: &str) -> u64 { + let id = ids::synthetic_user(&format!("client:{client}")); + if name.is_empty() { + return id; + } + let user = entities::client_user(id, name, self.cfg.ts6); + if self.extras.get(&id) != Some(&user) { + self.extras.insert(id, user.clone()); + let entity = entity(Kind::User(user)); + if let Some(key) = Key::of(&entity) { + self.live.insert(key, entity.clone()); + self.commit(ScopeId::Live(SERVER_ID), vec![entity], Vec::new()); + } + } + id + } + /// A user for a unique id the snapshot does not hold, put into LIVE. fn extra_user(&mut self, uid: &str, name: &str) -> u64 { let id = ids::synthetic_user(uid); diff --git a/crates/voelin-core/src/ts_session/entities.rs b/crates/voelin-core/src/ts_session/entities.rs index e881b45..512a665 100644 --- a/crates/voelin-core/src/ts_session/entities.rs +++ b/crates/voelin-core/src/ts_session/entities.rs @@ -246,6 +246,12 @@ pub(crate) fn bare_user(uid: &str, name: &str, ts6: bool) -> v1::User { ) } +/// A user who is on no snapshot and has no unique id (the sender of a poke +/// or a message that the link names by its client id only): `id` and name. +pub(crate) fn client_user(id: u64, name: &str, ts6: bool) -> v1::User { + user(&ClientInfo { nickname: name.to_owned(), ..Default::default() }, id, ts6) +} + /// Everything the snapshot `presence` says, as entities of the scopes of /// server 1. `ts6`: the server is TeamSpeak 6 (the kind of its unique ids). pub(crate) fn build(presence: &Presence, ts6: bool, own_client: Option) -> Built {