diff --git a/crates/voelin-core/src/link/session_link.rs b/crates/voelin-core/src/link/session_link.rs index 18863de..8cd1541 100644 --- a/crates/voelin-core/src/link/session_link.rs +++ b/crates/voelin-core/src/link/session_link.rs @@ -308,6 +308,9 @@ struct Conn { internal_tx: mpsc::UnboundedSender, internal: mpsc::UnboundedReceiver, replica: Replica, + /// What the host offers this user (`Session::features`, as `HostUpdate`s change it). + host_features: Features, + /// What it offers on this server: the host's set narrowed by `Server.features`. features: Features, welcome: v1::Welcome, server: u64, @@ -348,7 +351,7 @@ impl Conn { ) -> Self { let (internal_tx, internal) = mpsc::unbounded_channel(); let welcome = session.welcome().clone(); - let features = session.features(); + let host_features = session.features(); let max_chat = welcome .limits .as_ref() @@ -366,7 +369,8 @@ impl Conn { internal_tx, internal, replica: Replica::new(SCHEMA_GENERATION), - features, + features: host_features.clone(), + host_features, welcome, server: 0, participant: 0, @@ -713,7 +717,7 @@ impl Conn { } Some(Body::Signal(signal)) => batch.signals.push(signal.clone()), Some(Body::HostUpdate(update)) => { - self.features = Features::new(update.features.clone()); + self.host_features = Features::new(update.features.clone()); } Some(Body::ScopeClosed(closed)) => { self.note_scope(closed.scope.as_ref(), &mut batch); @@ -727,9 +731,29 @@ impl Conn { _ => {} } self.replica.apply_push(push); + // The host's features changed, or the server's list that narrows them may + // have (the SERVER scope holds the `Server` entity). + let server_scope = match &push.body { + Some(Body::Change(c)) => c.scope.as_ref(), + Some(Body::SyncEnd(e)) => e.scope.as_ref(), + _ => None, + } + .and_then(ScopeId::from_proto) + .is_some_and(|scope| scope == ScopeId::Server(self.server)); + if server_scope || matches!(push.body, Some(Body::HostUpdate(_))) { + self.narrow_features(); + } batch } + /// The features of this server: what the host offers, narrowed by + /// `Server.features` (an empty list keeps them all, `docs/session-seam.md` + /// 4.7). + fn narrow_features(&mut self) { + let listed = self.replica.server(self.server).map(|s| s.features.as_slice()); + self.features = self.host_features.narrowed(listed.unwrap_or_default()); + } + fn note_scope(&self, scope: Option<&v1::Scope>, batch: &mut Batch) { match scope.and_then(ScopeId::from_proto) { Some(ScopeId::Server(id) | ScopeId::Live(id)) if id == self.server => batch.view = true, diff --git a/crates/voelin-core/src/link/tests.rs b/crates/voelin-core/src/link/tests.rs index f7be1d4..b9714ed 100644 --- a/crates/voelin-core/src/link/tests.rs +++ b/crates/voelin-core/src/link/tests.rs @@ -2136,3 +2136,120 @@ async fn a_poke_from_a_sender_with_no_unique_id_outside_the_snapshot_is_named() rig.play(poke("ghost two")); assert_eq!(named(&mut rig).await, (None, "ghost two".to_owned())); } + +async fn event_of( + events: &mut mpsc::UnboundedReceiver, + mut f: impl FnMut(LinkEvent) -> Option, +) -> T { + within("an event", async { + loop { + if let Some(t) = f(events.recv().await?) { + return Some(t); + } + } + }) + .await +} + +/// `Server.features` narrows what the host offers (protocol 1.3, +/// `docs/session-seam.md` 4.7), and a `HostUpdate` changes what is narrowed. +#[tokio::test(flavor = "multi_thread")] +async fn the_features_of_a_server_are_the_hosts_narrowed_by_the_servers_list() { + use v1::entity::Kind; + use voelin_proto::test_util::{FakeConnector, eventually}; + + let connector = FakeConnector::new(); + let host = connector.host(); + let put_server = |features: &[&str]| { + let server = v1::Server { + id: 1, + name: "Narrow Host".into(), + uid: "narrow-host".into(), + default_channel_id: 10, + features: features.iter().map(|f| (*f).to_owned()).collect(), + ..Default::default() + }; + host.put(v1::Entity { kind: Some(Kind::Server(server)) }).unwrap(); + }; + put_server(&["voice", "chat"]); + let lobby = v1::Channel { + id: 10, + server_id: 1, + kind: v1::ChannelKind::Voice as i32, + name: "Lobby".into(), + ..Default::default() + }; + host.put(v1::Entity { kind: Some(Kind::Channel(lobby)) }).unwrap(); + + let mut backends = Backends::empty(); + backends.register(Arc::new(connector.clone())); + let link = SessionLink::new(backends); + let ctx = LocalCtx::standalone().link; + let mut bus = ctx.events.subscribe(); + let (cmd_tx, cmd_rx) = mpsc::unbounded_channel(); + let (event_tx, mut events) = mpsc::unbounded_channel(); + link.spawn(ConnectOptions::new("fake://host", "tester"), ctx, cmd_rx, event_tx); + + // The host offers files, the server does not. + let capabilities = event_of(&mut events, |e| match e { + LinkEvent::Connected { capabilities, .. } => Some(capabilities), + _ => None, + }) + .await; + assert!(!capabilities.file_transfer, "the server's list leaves files out"); + let handle = connector.last_session().expect("a session"); + let file_lists = || { + let requests = handle.requests(); + requests + .iter() + .filter(|logged| match &logged.request.op { + Some(v1::request::Op::List(list)) => list.kind == v1::EntityKind::File as i32, + _ => false, + }) + .count() + }; + let ask = |request| { + let dir = remote(10, None, "/"); + cmd_tx.send(LinkCmd::ListFiles { request, dir }).unwrap(); + }; + let refusal = "not offered by this server: files"; + let refused = + |event: &Event| matches!(event, Event::FileList { result: Err(why), .. } if why == refusal); + + // Refused here: nothing reaches the host. + ask(1); + loop { + let event = within("the refusal", async { bus.recv().await.ok() }).await; + if refused(&event) { + break; + } + } + assert_eq!(file_lists(), 0); + + // The server lifts its list (an empty one keeps all the host's): files work. + put_server(&[]); + let mut request = 1; + eventually(WAIT, "a file list to reach the host", || { + request += 1; + ask(request); + file_lists() > 0 + }) + .await; + + // The host narrows what it offers this user. + let update = v1::HostUpdate { features: vec!["voice".into(), "chat".into()], limits: None }; + handle.push(v1::Push { body: Some(v1::push::Body::HostUpdate(update)) }).unwrap(); + within("the narrower set to apply", async { + loop { + request += 1; + ask(request); + tokio::time::sleep(Duration::from_millis(20)).await; + while let Ok(event) = bus.try_recv() { + if refused(&event) { + return Some(()); + } + } + } + }) + .await; +}