From 436b483fb7a977fba8d9b240edd87488f7dcb4c3 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Wed, 30 Apr 2025 08:15:42 +0300 Subject: [PATCH] fix: `ya pub-to 0` checks if any peer is able to receive the message Issue ===== In https://github.com/sxyazi/yazi/issues/2680, `ya pub-to` received the ability to check if the target peer is able to receive the message that is being sent. However, if the target peer is `0` (sending to all peers), the implementation failed. Solution ======== Check if any peer is able to receive the message when the target peer is `0`. --- yazi-dds/src/client.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/yazi-dds/src/client.rs b/yazi-dds/src/client.rs index 430cff7d..22ab1548 100644 --- a/yazi-dds/src/client.rs +++ b/yazi-dds/src/client.rs @@ -100,15 +100,28 @@ impl Client { ); } - match peers.get(&receiver).map(|p| p.able(kind)) { - Some(true) => {} + if receiver == 0 { + // check if any peer is able to receive the message + let any_able = + peers.keys().any(|peer| Self::receiver_able(kind, peer, &peers).is_ok()).then_some(()); + if any_able.is_none() { + bail!("No peer is able to receive `{kind}` messages."); + } + } else { + Self::receiver_able(kind, &receiver, &peers)?; + } + + Ok(()) + } + + fn receiver_able(kind: &str, receiver: &Id, peers: &HashMap) -> Result<()> { + match peers.get(receiver).map(|p| p.able(kind)) { + Some(true) => Ok(()), Some(false) => { bail!("Receiver `{receiver}` does not have the ability to receive `{kind}` messages.") } None => bail!("Receiver `{receiver}` not found. Check if the receiver is running."), } - - Ok(()) } /// Connect to an existing server and listen in on the messages that are being