More voice stuff
This commit is contained in:
+11
-21
@@ -8,6 +8,7 @@ use crate::{data::messages::StoredMessage, server::Server, types::ClientMeta};
|
|||||||
pub mod initialize;
|
pub mod initialize;
|
||||||
pub mod message;
|
pub mod message;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
pub mod voice;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
#[serde(tag = "method")]
|
#[serde(tag = "method")]
|
||||||
@@ -52,6 +53,11 @@ pub enum ClientMethod {
|
|||||||
pubkey: String,
|
pubkey: String,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
UserLeftVoice {
|
||||||
|
channel_id: String,
|
||||||
|
pubkey: String,
|
||||||
|
},
|
||||||
|
|
||||||
Speaking {
|
Speaking {
|
||||||
pubkey: String,
|
pubkey: String,
|
||||||
},
|
},
|
||||||
@@ -103,6 +109,8 @@ pub enum ServerMethod {
|
|||||||
JoinVoice {
|
JoinVoice {
|
||||||
channel_id: String,
|
channel_id: String,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
LeaveVoice,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn read_loop(
|
pub async fn read_loop(
|
||||||
@@ -164,29 +172,11 @@ pub async fn read_loop(
|
|||||||
}
|
}
|
||||||
|
|
||||||
ServerMethod::JoinVoice { channel_id } => {
|
ServerMethod::JoinVoice { channel_id } => {
|
||||||
{
|
voice::join(server, verifying_key, socket, channel_id).await?;
|
||||||
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
|
ServerMethod::LeaveVoice => {
|
||||||
.broadcast(&ClientMethod::UserJoinedVoice {
|
voice::leave(server, verifying_key).await?;
|
||||||
channel_id,
|
|
||||||
pubkey: crate::crypto::to_string(&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