From 5c762eaf2310d72b7c5c7d2ba497d94c6441126a Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Tue, 13 Oct 2020 20:47:57 +0200 Subject: rust: Understand _push leave --- rust/src/main.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/rust/src/main.rs b/rust/src/main.rs index 0d5767f..f7a2660 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -252,6 +252,21 @@ impl App { Ok(()) } + async fn leave_room(self: &Arc, room: &Word) -> io::Result<()> { + debug!("leave_room({})", room); + + { + let mut state = self.state.lock().await; + if state.currentroom == *room { + state.currentroom = STATUS_ROOM.clone(); + } + state.roomlist.retain(|r| r != room); + state.rooms.remove(room); + } + + self.full_redraw().await + } + async fn put_members(self: &Arc, room: &Word, ms: Vec) -> io::Result<()> { debug!("put_members {} #{}", room, ms.len()); let mut state = self.state.lock().await; @@ -266,6 +281,13 @@ impl App { state.full_redraw() } + async fn remove_member(self: &Arc, room: &Word, user: Word) -> io::Result<()> { + debug!("remove_member {} {}", room, user); + let mut state = self.state.lock().await; + state.rooms.get_mut(room).unwrap().members.retain(|u| *u != user); + state.full_redraw() + } + async fn put_history(self: &Arc, room: &Word, hist: Vec) -> io::Result<()> { debug!("put_history {} #{}", room, hist.len()); let mut state = self.state.lock().await; @@ -335,6 +357,14 @@ impl App { self.add_member(&room, username.clone()).await.unwrap(); self.append_history_item(&room, HItem::Service(format!("Join: <{}>", username), false)).await.unwrap(); } + PushMessage::Leave { roomname: room, username: user } => { + if user == self.username { + self.leave_room(&room).await.unwrap(); + } else { + self.remove_member(&room, user.clone()).await.unwrap(); + self.append_history_item(&room, HItem::Service(format!("Leave: <{}>", user), false)).await.unwrap(); + } + } PushMessage::Message(msg) => { let mention = contains_mention(&msg.message, &self.username); if self.decide_push_notify(&msg.roomname, mention).await { -- cgit v1.2.3-70-g09d2