2021-02-05 12:21:10 +00:00
|
|
|
//! # WebSocket routes
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
2021-02-08 17:24:28 +00:00
|
|
|
use crate::controllers::{calls_controller, likes_controller, user_ws_actions};
|
2021-02-05 12:21:10 +00:00
|
|
|
use crate::data::error::Res;
|
2021-02-05 13:24:00 +00:00
|
|
|
use crate::data::user_ws_request_handler::UserWsRequestHandler;
|
2021-02-05 12:21:10 +00:00
|
|
|
|
2021-02-05 13:24:00 +00:00
|
|
|
pub type WsRequestProcess = Box<dyn Fn(&mut UserWsRequestHandler) -> Res>;
|
2021-02-05 12:21:10 +00:00
|
|
|
|
|
|
|
/// WebSocket route
|
2021-02-05 12:25:27 +00:00
|
|
|
pub struct UserWsRoute {
|
2021-02-05 12:21:10 +00:00
|
|
|
pub route: String,
|
|
|
|
pub handler: WsRequestProcess,
|
|
|
|
}
|
|
|
|
|
2021-02-05 12:25:27 +00:00
|
|
|
impl UserWsRoute {
|
|
|
|
pub fn new<H>(route: &str, handler: H) -> UserWsRoute
|
2021-02-05 13:24:00 +00:00
|
|
|
where H: 'static + Fn(&mut UserWsRequestHandler) -> Res {
|
2021-02-05 12:25:27 +00:00
|
|
|
UserWsRoute {
|
2021-02-05 12:21:10 +00:00
|
|
|
route: route.to_string(),
|
|
|
|
handler: Box::new(handler),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the list of available WebSocket routes
|
2021-02-05 12:25:27 +00:00
|
|
|
pub fn get_user_ws_routes() -> Vec<UserWsRoute> {
|
2021-02-05 12:45:40 +00:00
|
|
|
vec![
|
2021-02-05 13:24:00 +00:00
|
|
|
// Main controller
|
|
|
|
UserWsRoute::new("$main/set_incognito", user_ws_actions::set_incognito),
|
2021-02-06 07:55:14 +00:00
|
|
|
UserWsRoute::new("$main/register_conv", user_ws_actions::register_conv),
|
|
|
|
UserWsRoute::new("$main/unregister_conv", user_ws_actions::unregister_conv),
|
2021-02-06 09:16:09 +00:00
|
|
|
UserWsRoute::new("$main/register_post", user_ws_actions::register_post),
|
|
|
|
UserWsRoute::new("$main/unregister_post", user_ws_actions::unregister_post),
|
2021-02-05 13:24:00 +00:00
|
|
|
|
2021-02-05 12:45:40 +00:00
|
|
|
// Likes controller
|
2021-02-08 17:24:28 +00:00
|
|
|
UserWsRoute::new("likes/update", likes_controller::update),
|
|
|
|
|
|
|
|
// Calls controller
|
|
|
|
UserWsRoute::new("calls/config", calls_controller::get_config),
|
2021-02-10 16:16:52 +00:00
|
|
|
UserWsRoute::new("calls/join", calls_controller::join_call),
|
2021-02-10 16:32:14 +00:00
|
|
|
UserWsRoute::new("calls/members", calls_controller::get_members_list),
|
2021-02-05 12:45:40 +00:00
|
|
|
]
|
2021-02-05 12:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Search for a route
|
2021-02-05 12:25:27 +00:00
|
|
|
pub fn find_user_ws_route(uri: &str) -> Option<UserWsRoute> {
|
|
|
|
get_user_ws_routes().into_iter().find(|r| r.route == uri)
|
2021-02-05 12:21:10 +00:00
|
|
|
}
|