More voice stuff
This commit is contained in:
+12
-22
@@ -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::<u64>() % (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?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use ed25519_dalek::VerifyingKey;
|
||||
|
||||
use crate::{protocol::ClientMethod, server::Server};
|
||||
|
||||
pub async fn join(
|
||||
server: &Arc<Server>,
|
||||
verifying_key: VerifyingKey,
|
||||
socket: &Arc<crate::ws::EnclaveWebSocket>,
|
||||
channel_id: String,
|
||||
) -> anyhow::Result<()> {
|
||||
{
|
||||
let pin = rand::random::<u64>() % (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<Server>, 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(())
|
||||
}
|
||||
Reference in New Issue
Block a user