From f8e510bdb6096fcc987256a2c2cd5944a0671ce8 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Thu, 27 Aug 2026 20:32:30 +0200 Subject: [PATCH] More voice stuff --- src/protocol/mod.rs | 34 ++++++++-------------- src/protocol/voice.rs | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 22 deletions(-) create mode 100644 src/protocol/voice.rs diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index f8e1814..9cb3a1b 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -8,6 +8,7 @@ use crate::{data::messages::StoredMessage, server::Server, types::ClientMeta}; pub mod initialize; pub mod message; pub mod user; +pub mod voice; #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "method")] @@ -52,6 +53,11 @@ pub enum ClientMethod { pubkey: String, }, + UserLeftVoice { + channel_id: String, + pubkey: String, + }, + Speaking { pubkey: String, }, @@ -103,6 +109,8 @@ pub enum ServerMethod { JoinVoice { channel_id: String, }, + + LeaveVoice, } pub async fn read_loop( @@ -164,29 +172,11 @@ pub async fn read_loop( } ServerMethod::JoinVoice { channel_id } => { - { - let pin = rand::random::() % (1 << 53); + voice::join(server, verifying_key, socket, channel_id).await?; + } - server - .voice_pins - .lock() - .await - .insert(pin, (verifying_key, channel_id.clone())); - - socket - .send(&ClientMethod::JoinVoice { - channel_id: channel_id.clone(), - pin, - }) - .await?; - } - - server - .broadcast(&ClientMethod::UserJoinedVoice { - channel_id, - pubkey: crate::crypto::to_string(&verifying_key), - }) - .await?; + ServerMethod::LeaveVoice => { + voice::leave(server, verifying_key).await?; } } } diff --git a/src/protocol/voice.rs b/src/protocol/voice.rs new file mode 100644 index 0000000..563225b --- /dev/null +++ b/src/protocol/voice.rs @@ -0,0 +1,67 @@ +use std::sync::Arc; + +use ed25519_dalek::VerifyingKey; + +use crate::{protocol::ClientMethod, server::Server}; + +pub async fn join( + server: &Arc, + verifying_key: VerifyingKey, + socket: &Arc, + channel_id: String, +) -> anyhow::Result<()> { + { + let pin = rand::random::() % (1 << 53); + + server + .voice_pins + .lock() + .await + .insert(pin, (verifying_key, channel_id.clone())); + + socket + .send(&ClientMethod::JoinVoice { + channel_id: channel_id.clone(), + pin, + }) + .await?; + } + + server + .broadcast(&ClientMethod::UserJoinedVoice { + channel_id, + pubkey: crate::crypto::to_string(&verifying_key), + }) + .await?; + + Ok(()) +} + +pub async fn leave(server: &Arc, verifying_key: VerifyingKey) -> anyhow::Result<()> { + let Some(channel_id) = ({ + let clients = server.clients.lock().await; + + let Some(client) = clients.get(&verifying_key) else { + return Ok(()); + }; + + client.voice.lock().await.take().map(|v| v.channel_id) + }) else { + return Ok(()); + }; + + server + .voice_pins + .lock() + .await + .retain(|_, v| v.0 != verifying_key); + + server + .broadcast(&ClientMethod::UserLeftVoice { + channel_id, + pubkey: crate::crypto::to_string(&verifying_key), + }) + .await?; + + Ok(()) +}