From 2c828b482edce789f07b5dd93407a457507dcad5 Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Wed, 5 Jun 2024 00:10:36 +0200 Subject: [PATCH] core: ADT for results of room joins --- crates/lavina-core/src/player.rs | 29 +++++++++++++++++++---------- crates/projection-irc/src/lib.rs | 27 ++++++++++++++++----------- src/http.rs | 9 ++++++--- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/crates/lavina-core/src/player.rs b/crates/lavina-core/src/player.rs index da4c615..bd869d7 100644 --- a/crates/lavina-core/src/player.rs +++ b/crates/lavina-core/src/player.rs @@ -78,9 +78,9 @@ impl PlayerConnection { /// Handled in [Player::change_room_topic]. #[tracing::instrument(skip(self, new_topic), name = "PlayerConnection::change_topic")] - pub async fn change_topic(&mut self, room_id: RoomId, new_topic: Str) -> Result<()> { + pub async fn change_topic(&mut self, room_id: RoomId, new_topic: Str) -> Result { let (promise, deferred) = oneshot(); - let cmd = ClientCommand::ChangeTopic { + let cmd = ClientCommand::ChangeRoomTopic { room_id, new_topic, promise, @@ -207,10 +207,10 @@ pub enum ClientCommand { body: Str, promise: Promise>, }, - ChangeTopic { + ChangeRoomTopic { room_id: RoomId, new_topic: Str, - promise: Promise>, + promise: Promise>, }, GetRooms { promise: Promise>>, @@ -242,6 +242,11 @@ pub enum JoinResult { Banned, } +pub enum ChangeRoomTopicResult { + Success, + NoSuchRoom, +} + pub enum SendMessageResult { Success(DateTime), NoSuchRoom, @@ -510,7 +515,7 @@ impl Player { let result = self.send_room_message(connection_id, room_id, body).await; let _ = promise.send(result); } - ClientCommand::ChangeTopic { + ClientCommand::ChangeRoomTopic { room_id, new_topic, promise, @@ -680,11 +685,15 @@ impl Player { } #[tracing::instrument(skip(self, new_topic), name = "Player::change_room_topic")] - async fn change_room_topic(&mut self, connection_id: ConnectionId, room_id: RoomId, new_topic: Str) -> Result<()> { + async fn change_room_topic( + &mut self, + connection_id: ConnectionId, + room_id: RoomId, + new_topic: Str, + ) -> Result { let Some(room) = self.my_rooms.get(&room_id) else { - tracing::info!("Room with ID {room_id:?} not found"); - // TODO - return Ok(()); + tracing::debug!("Room with ID {room_id:?} not found"); + return Ok(ChangeRoomTopicResult::NoSuchRoom); }; match room { RoomRef::Local(room) => { @@ -701,7 +710,7 @@ impl Player { } let update = Updates::RoomTopicChanged { room_id, new_topic }; self.broadcast_update(update, connection_id).await; - Ok(()) + Ok(ChangeRoomTopicResult::Success) } #[tracing::instrument(skip(self), name = "Player::get_rooms")] diff --git a/crates/projection-irc/src/lib.rs b/crates/projection-irc/src/lib.rs index 1dcea2a..c31696d 100644 --- a/crates/projection-irc/src/lib.rs +++ b/crates/projection-irc/src/lib.rs @@ -750,18 +750,23 @@ async fn handle_incoming_message( match chan { Chan::Global(chan) => { let room_id = RoomId::try_from(chan)?; - user_handle.change_topic(room_id.clone(), topic.clone()).await?; - ServerMessage { - tags: vec![], - sender: Some(config.server_name.clone()), - body: ServerMessageBody::N332Topic { - client: user.nickname.clone(), - chat: Chan::Global(room_id.as_inner().clone()), - topic, - }, + let res = user_handle.change_topic(room_id.clone(), topic.clone()).await?; + match res { + ChangeRoomTopicResult::Success => { + ServerMessage { + tags: vec![], + sender: Some(config.server_name.clone()), + body: ServerMessageBody::N332Topic { + client: user.nickname.clone(), + chat: Chan::Global(room_id.as_inner().clone()), + topic, + }, + } + .write_async(writer) + .await?; + } + ChangeRoomTopicResult::NoSuchRoom => {} } - .write_async(writer) - .await?; writer.flush().await?; } Chan::Local(_) => {} diff --git a/src/http.rs b/src/http.rs index 9bc189d..6e5ade4 100644 --- a/src/http.rs +++ b/src/http.rs @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize}; use tokio::net::TcpListener; use lavina_core::auth::UpdatePasswordResult; -use lavina_core::player::{PlayerConnectionResult, PlayerId, SendMessageResult}; +use lavina_core::player::{ChangeRoomTopicResult, PlayerConnectionResult, PlayerId, SendMessageResult}; use lavina_core::prelude::*; use lavina_core::room::RoomId; use lavina_core::terminator::Terminator; @@ -204,8 +204,11 @@ async fn endpoint_set_room_topic( return Ok(player_not_found()); } }; - connection.change_topic(room_id, req.topic.into()).await?; - Ok(empty_204_request()) + let res = connection.change_topic(room_id, req.topic.into()).await?; + match res { + ChangeRoomTopicResult::Success => Ok(empty_204_request()), + ChangeRoomTopicResult::NoSuchRoom => Ok(room_not_found()), + } } fn endpoint_not_found() -> Response> {