garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

cartopolis-agent · 2026-11-28T18:37:30Z

feat(sim): a call from a chat hubbed elsewhere wakes a closed app

A member whose chat's hub lives on another server heard a call ring only
while their app was open: the hub relays the roster to their home, which
hands it to connected devices, and nobody pushed to a closed one. The hub
knows no member's push address, so the home now does it: when the hub's
roster is the first join's (one member, sent on behalf of the joiner),
the home sends a call push to the people it names, minus the caller if
they live here and whoever blocked them, as the calls task does for a
call on one server. No wire change: an older hub's roster rings the same.
crates/simulator/src/chat.rs          | 37 +++++++++++++++++++++++++++++++
 crates/simulator/src/remote/calls.rs  | 41 +++++++++++++++++++++++++++++++++++
 crates/simulator/src/remote/routes.rs | 27 ++++++++++++++++-------
 3 files changed, 97 insertions(+), 8 deletions(-)

diff --git a/crates/simulator/src/chat.rs b/crates/simulator/src/chat.rs
index a4e5e1ba..6449b2ee 100644
--- a/crates/simulator/src/chat.rs
+++ b/crates/simulator/src/chat.rs
@@ -261,6 +261,24 @@ impl ChatHub {
         });
     }
 
+    /// A call started in a chat another server is the hub of: wake the closed
+    /// apps of `accounts`, as the calls task does for a call here. Every one
+    /// but the caller, connected or not, because a call's push is what the
+    /// app's receiver shows as ringing whether the app is open or shut.
+    /// `caller` is their `accounts.id` when they live here, else empty.
+    pub fn wake_for_call(&self, accounts: &[String], caller: &str, caller_name: &str) {
+        let Some(wake) = self.wake.get() else { return };
+        if accounts.iter().all(|a| a == caller) {
+            return;
+        }
+        let _ = wake.send(crate::push::Ring {
+            kind: crate::push::Kind::Call,
+            caller: caller.to_owned(),
+            caller_name: caller_name.to_owned(),
+            members: accounts.to_vec(),
+        });
+    }
+
     /// Deliver a frame to **one** connected private-chat device of any of
     /// `accounts`, and say whether there was one. For a job one device should
     /// do rather than every device racing to (`ChannelJoinRequest`: each
@@ -578,6 +596,25 @@ mod tests {
         assert!(rx.try_recv().is_err());
     }
 
+    /// A call from elsewhere rings like one here: every member named, and
+    /// nothing when the only one named is the caller.
+    #[test]
+    fn a_call_from_elsewhere_wakes_the_members_here() {
+        let hub = ChatHub::new();
+        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
+        hub.set_wake(tx);
+        let members: Vec<String> = ["ann", "bob"].map(String::from).to_vec();
+
+        hub.wake_for_call(&members, "", "cy@rug.example");
+        let ring = rx.try_recv().unwrap();
+        assert_eq!(ring.kind, crate::push::Kind::Call);
+        assert_eq!(ring.members, ["ann", "bob"]);
+        assert_eq!(ring.caller_name, "cy@rug.example");
+
+        hub.wake_for_call(&members[..1], "ann", "ann");
+        assert!(rx.try_recv().is_err());
+    }
+
     #[test]
     fn radius_is_a_sphere_not_an_axis() {
         // A listener directly above is as far as one to the side.
diff --git a/crates/simulator/src/remote/calls.rs b/crates/simulator/src/remote/calls.rs
index d080cb5d..c9beae3c 100644
--- a/crates/simulator/src/remote/calls.rs
+++ b/crates/simulator/src/remote/calls.rs
@@ -344,6 +344,15 @@ pub fn call_message(frame: &[u8]) -> Option<(NetMessage, Option<Vec<u8>>)> {
     Some((msg, group))
 }
 
+/// Whether a roster the hub sent on behalf of `from` says a call has just
+/// started: the hub's calls task sends one with the joiner's name at each
+/// join, and the first one in is alone in it. That is when the hub wakes its
+/// own members' closed apps (`crate::calls`), so it is when a home wakes its.
+#[must_use]
+pub fn starts_a_call(msg: &NetMessage, from: &str) -> bool {
+    matches!(msg, NetMessage::CallRoster { members, .. } if members.len() == 1) && !from.is_empty()
+}
+
 /// Write the names in a message from `hub` as a device on `here` expects
 /// them: bare for people here, full addresses for everyone else.
 pub fn localise(msg: &mut NetMessage, hub: &str, here: &str) {
@@ -405,6 +414,38 @@ mod tests {
         assert_eq!(names, ["alice@hub.example", "bob"]);
     }
 
+    /// Only the roster of the first one in, sent for their joining, rings: not
+    /// the second joiner's, nor a departure's or a meeting change's.
+    #[test]
+    fn only_the_first_join_starts_a_call() {
+        let member = |username: &str| CallMember {
+            participant: 1,
+            username: username.into(),
+            device: Uuid::nil(),
+            role: CallRole::Member,
+            hand: None,
+            held: false,
+            sharing: false,
+            muted: false,
+            recording: false,
+            guest: false,
+            room: 0,
+            agent: false,
+        };
+        let roster = |names: &[&str]| NetMessage::CallRoster {
+            group: address::group_id("hub.example", [0; 16]),
+            members: names.iter().map(|n| member(n)).collect(),
+            settings: CallSettings::default(),
+            waiting: Vec::new(),
+            spotlight: None,
+            rooms: None,
+        };
+        assert!(starts_a_call(&roster(&["alice"]), "alice"));
+        assert!(!starts_a_call(&roster(&["alice", "bob"]), "bob"));
+        assert!(!starts_a_call(&roster(&["alice"]), ""));
+        assert!(!starts_a_call(&roster(&[]), ""));
+    }
+
     #[test]
     fn a_hub_may_send_call_messages_only() {
         let frame = |m: &NetMessage| cartopolis_core::encode_app_frame(m).unwrap();
diff --git a/crates/simulator/src/remote/routes.rs b/crates/simulator/src/remote/routes.rs
index 1e40bb40..8b84746d 100644
--- a/crates/simulator/src/remote/routes.rs
+++ b/crates/simulator/src/remote/routes.rs
@@ -844,27 +844,38 @@ async fn call_signal(
     if !matches!(msg, NetMessage::CallRoster { .. }) {
         return refuse(StatusCode::BAD_REQUEST, "only a roster goes to people");
     }
+    let from = if s.from.is_empty() {
+        String::new()
+    } else {
+        address::display(&address::qualify(&s.from, &server), &link.server).to_lowercase()
+    };
     let names = s.to;
-    let accounts = match db(&state, move |c| {
+    let caller_name = from.clone();
+    let (accounts, caller) = match db(&state, move |c| {
         let mut ids = Vec::new();
         for n in &names {
             if let Some(a) = here(c, n).map_err(storage)? {
                 ids.push(a.id);
             }
         }
-        Ok(ids)
+        // A caller who lives here is named too, and is not to be woken.
+        let caller = if caller_name.is_empty() {
+            None
+        } else {
+            here(c, &caller_name).map_err(storage)?
+        };
+        Ok((ids, caller.map(|a| a.id).unwrap_or_default()))
     })
     .await
     {
-        Ok(ids) => ids,
+        Ok(v) => v,
         Err(r) => return r,
     };
-    let from = if s.from.is_empty() {
-        String::new()
-    } else {
-        address::display(&address::qualify(&s.from, &server), &link.server).to_lowercase()
-    };
     state.chat.deliver_private(&accounts, &from, false, &frame);
+    // A closed app hears no roster: the call's first join wakes it.
+    if calls::starts_a_call(&msg, &from) {
+        state.chat.wake_for_call(&accounts, &caller, &from);
+    }
     Json(Done {}).into_response()
 }