From 0b7076d52063b03250141de069b28ce1947c1012 Mon Sep 17 00:00:00 2001 From: JenChieh Date: Thu, 13 Jun 2024 02:54:01 -0700 Subject: [PATCH] feat: wip say --- src/handler/file.rs | 17 +++++++++++++++-- src/room.rs | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/handler/file.rs b/src/handler/file.rs index 907cbb5..234eff0 100644 --- a/src/handler/file.rs +++ b/src/handler/file.rs @@ -229,11 +229,24 @@ pub mod say { return; } + let username = client.user().unwrap().username.clone(); + + let file = data_str(json, "file").unwrap(); + let file = no_room_path(&room, &file); let message = data_str(json, "message").unwrap(); - room.broadcast_json(&serde_json::json!({ + let peers = room.peers_by_file(&room, &file); + + let params = &serde_json::json!({ "method": METHOD, + "username": username, // Who speak this message? + "file": file, "message": message, - })); + "status": "success", + }); + + for (_addr, _sender) in peers.iter() { + let _ = _sender.send(params.to_string()); + } } } diff --git a/src/room.rs b/src/room.rs index 0c094af..2d29672 100644 --- a/src/room.rs +++ b/src/room.rs @@ -65,6 +65,28 @@ impl Room { &mut self.peers } + /// Return peers by file. + /// + /// # Arguments + /// + /// * `file` - The file path. + pub fn peers_by_file(&self, room: &Room, file: &String) -> HashMap<&SocketAddr, &UnboundedSender> { + let file = no_room_path(&room, &file); + + let mut data = HashMap::new(); + + for (addr, sender) in self.peers.iter() { + let client = self.get_client(addr).unwrap(); + let path = no_room_path(&room, client.get_path()); + if path == file { + continue; + } + data.insert(addr, sender); + } + + data + } + /// Return the sender. /// /// # Arguments