Solstice client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

116 lines
2.7 KiB

use anyhow::Context as AnyhowContext;
use solstice_proto::server::RoomJoinRequest;
use solstice_proto::ServerRequest;
use crate::context::Context;
use crate::message_handler::MessageHandler;
#[derive(Debug, Default)]
pub struct RoomJoinRequestHandler;
impl MessageHandler for RoomJoinRequestHandler {
type Message = String;
fn run(
self,
context: &Context,
room_name: &Self::Message,
) -> anyhow::Result<()> {
{
// TODO: Send response immediately if we already joined.
let mut guard = context.state.lock();
guard
.rooms
.start_joining(room_name)
.context("joining room")?;
}
context
.server_request_tx
.blocking_send(ServerRequest::RoomJoinRequest(RoomJoinRequest {
room_name: room_name.clone(),
}))
.context("sending server request")?;
Ok(())
}
fn name() -> String {
"RoomJoinRequestHandler".to_string()
}
}
#[cfg(test)]
mod tests {
use anyhow::Context;
use solstice_proto::server::{RoomJoinRequest, RoomListResponse};
use solstice_proto::ServerRequest;
use crate::context::ContextBundle;
use crate::message_handler::MessageHandler;
use crate::room::Membership;
use super::RoomJoinRequestHandler;
#[test]
fn run_failure() -> anyhow::Result<()> {
let mut bundle = ContextBundle::default();
RoomJoinRequestHandler::default()
.run(&bundle.context, &"bleep".to_string())
.unwrap_err();
// Room state has not changed.
assert_eq!(bundle.context.state.lock().rooms.get_room_list(), vec![]);
// Close the channel, so we can observe it was empty without hanging.
drop(bundle.context.server_request_tx);
assert_eq!(bundle.server_request_rx.blocking_recv(), None);
Ok(())
}
#[test]
fn run_success() -> anyhow::Result<()> {
let mut bundle = ContextBundle::default();
bundle
.context
.state
.lock()
.rooms
.set_room_list(RoomListResponse {
rooms: vec![("bleep".to_string(), 3)],
owned_private_rooms: vec![],
other_private_rooms: vec![],
operated_private_room_names: vec![],
});
RoomJoinRequestHandler::default()
.run(&bundle.context, &"bleep".to_string())
.context("running handler")?;
let request = bundle.server_request_rx.blocking_recv().unwrap();
// Room state has been altered to reflect the request.
assert_eq!(
bundle
.context
.state
.lock()
.rooms
.get_strict("bleep")
.context("getting room")?
.membership,
Membership::Joining
);
// The request is forwarded onwards.
assert_eq!(
request,
ServerRequest::RoomJoinRequest(RoomJoinRequest {
room_name: "bleep".to_string(),
})
);
Ok(())
}
}