diff --git a/client/src/handlers/mod.rs b/client/src/handlers/mod.rs index 92bfcd0..df340d3 100644 --- a/client/src/handlers/mod.rs +++ b/client/src/handlers/mod.rs @@ -1,3 +1,5 @@ mod set_privileged_users_handler; +mod set_room_list_handler; pub use set_privileged_users_handler::SetPrivilegedUsersHandler; +pub use set_room_list_handler::SetRoomListHandler; diff --git a/client/src/handlers/set_room_list_handler.rs b/client/src/handlers/set_room_list_handler.rs new file mode 100644 index 0000000..abea6eb --- /dev/null +++ b/client/src/handlers/set_room_list_handler.rs @@ -0,0 +1,68 @@ +use std::io; + +use solstice_proto::server::RoomListResponse; + +use crate::context::Context; +use crate::message_handler::MessageHandler; + +#[derive(Debug, Default)] +pub struct SetRoomListHandler; + +impl MessageHandler for SetRoomListHandler { + fn run( + self, + context: &Context, + message: &RoomListResponse, + ) -> io::Result<()> { + let response = (*message).clone(); + context.state.lock().rooms.set_room_list(response); + Ok(()) + } + + fn name() -> String { + "SetRoomListHandler".to_string() + } +} + +#[cfg(test)] +mod tests { + use solstice_proto::server::RoomListResponse; + + use crate::context::ContextBundle; + use crate::message_handler::MessageHandler; + use crate::room::{Room, Visibility}; + + use super::SetRoomListHandler; + + // Cannot get the compiler to be satisfied when borrowing the name... + fn room_name(pair: &(String, Room)) -> String { + pair.0.clone() + } + + #[test] + fn run_sets_room_list() { + let bundle = ContextBundle::default(); + + let response = RoomListResponse { + rooms: vec![("potato".to_string(), 123), ("apple".to_string(), 42)], + owned_private_rooms: vec![], + other_private_rooms: vec![], + operated_private_room_names: vec![], + }; + + SetRoomListHandler::default() + .run(&bundle.context, &response) + .unwrap(); + + let mut rooms = bundle.context.state.lock().rooms.get_room_list(); + rooms.sort_by_key(room_name); + + assert_eq!( + rooms, + vec![ + ("apple".to_string(), Room::new(Visibility::Public, 42)), + ("potato".to_string(), Room::new(Visibility::Public, 123)), + ] + ); + } +} diff --git a/client/src/room.rs b/client/src/room.rs index ceff9c9..93f18dc 100644 --- a/client/src/room.rs +++ b/client/src/room.rs @@ -67,7 +67,7 @@ pub struct Room { impl Room { /// Creates a new room with the given visibility and user count. - fn new(visibility: Visibility, user_count: usize) -> Self { + pub fn new(visibility: Visibility, user_count: usize) -> Self { Room { membership: Membership::NonMember, visibility: visibility,