fix: update users instead of replacing

also track but filter out local user
This commit is contained in:
əlemi 2024-02-21 18:36:50 +01:00
parent 02c631c9e2
commit 7f0516dd1e
2 changed files with 28 additions and 6 deletions

View file

@ -63,6 +63,23 @@ pub struct UserProperties {
pub recording: bool,
}
impl User {
pub fn update(&mut self, state: crate::tcp::proto::UserState) {
if state.session() != self.session { tracing::warn!("updating with different session??") }
if let Some(actor) = state.actor { self.actor = actor }
if let Some(name) = state.name { self.name = name }
if let Some(user_id) = state.user_id { self.user_id = Some(user_id) }
if let Some(comment) = state.comment { self.comment = Some(comment) }
if let Some(mute) = state.mute { self.properties.mute = mute }
if let Some(deaf) = state.deaf { self.properties.deaf = deaf }
if let Some(suppress) = state.suppress { self.properties.suppress = suppress }
if let Some(self_mute) = state.self_mute { self.properties.self_mute = self_mute }
if let Some(self_deaf) = state.self_deaf { self.properties.self_deaf = self_deaf }
if let Some(priority_speaker) = state.priority_speaker { self.properties.priority_speaker = priority_speaker }
if let Some(recording) = state.recording { self.properties.recording = recording }
}
}
impl From<crate::tcp::proto::UserState> for User {
fn from(value: crate::tcp::proto::UserState) -> Self {

View file

@ -7,6 +7,7 @@ use crate::{model::User, tcp::{control::ControlChannel, proto}, udp::proto::{Pin
#[derive(Debug)]
pub struct Session {
users: RwLock<HashMap<u32, User>>,
username: String,
host: String,
sync: watch::Receiver<bool>,
drop: mpsc::Sender<()>,
@ -61,7 +62,12 @@ impl Session {
}
pub async fn users(&self) -> Vec<User> {
self.users.read().await.borrow().values().cloned().collect()
self.users.read().await
.borrow()
.values()
.filter(|u| u.name != self.username)
.cloned()
.collect()
}
pub fn host(&self) -> String {
@ -94,13 +100,12 @@ impl Session {
channel.send(pkt).await?;
}
let (tx, mut rx) = mpsc::channel(1);
let (ready_tx, ready) = watch::channel(false);
let (drop, mut stop) = mpsc::channel(1);
let (ready, sync) = watch::channel(false);
let s = Arc::new(Session {
username, drop, sync,
users : RwLock::new(HashMap::new()),
sync: ready,
drop: tx,
host: host.to_string(),
});
@ -108,7 +113,7 @@ impl Session {
let chan = channel.clone();
tokio::spawn(async move {
loop {
match rx.try_recv() {
match stop.try_recv() {
Ok(()) => break,
Err(mpsc::error::TryRecvError::Empty) => {},
Err(mpsc::error::TryRecvError::Disconnected) => break tracing::warn!("all session dropped without stopping this task, stopping..."),