From cfaaef68b7bf407b46819330da17293adaf78b59 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 11 Mar 2022 22:51:30 +0100 Subject: [PATCH] Can now turn optionally User WS Controller as async functions --- src/controllers/user_ws_controller.rs | 12 ++-- src/user_ws_routes.rs | 83 +++++++++++---------------- 2 files changed, 37 insertions(+), 58 deletions(-) diff --git a/src/controllers/user_ws_controller.rs b/src/controllers/user_ws_controller.rs index 8fa32b7..18ee901 100644 --- a/src/controllers/user_ws_controller.rs +++ b/src/controllers/user_ws_controller.rs @@ -25,7 +25,7 @@ use crate::data::user_ws_message::UserWsMessage; use crate::data::user_ws_request_handler::{UserWsRequestHandler, UserWsResponseType}; use crate::helpers::{account_helper, events_helper}; use crate::helpers::events_helper::Event; -use crate::user_ws_routes::find_user_ws_route; +use crate::user_ws_routes::exec_user_ws_route; use crate::utils::crypt_utils::rand_str; use crate::utils::date_utils::time; @@ -266,13 +266,9 @@ impl WsSession { args, ); - let result = match find_user_ws_route(&incoming_msg.title) { - None => { - handler.not_found("Route not found!".to_string()) - } - Some(r) => { - (r.handler)(&mut handler) - } + let result = match exec_user_ws_route(&incoming_msg.title, &mut handler).await { + None => handler.not_found("Route not found!".to_string()), + Some(r) => r }; if !handler.has_response() { diff --git a/src/user_ws_routes.rs b/src/user_ws_routes.rs index b49883c..7c5486a 100644 --- a/src/user_ws_routes.rs +++ b/src/user_ws_routes.rs @@ -5,59 +5,42 @@ use crate::controllers::{calls_controller, conversations_controller, forez_controller, likes_controller, user_ws_actions}; use crate::data::error::Res; use crate::data::user_ws_request_handler::UserWsRequestHandler; +use crate::routes::RequestResult; pub type WsRequestProcess = Box Res>; -/// WebSocket route -pub struct UserWsRoute { - pub route: String, - pub handler: WsRequestProcess, -} - -impl UserWsRoute { - pub fn new(route: &str, handler: H) -> UserWsRoute - where H: 'static + Fn(&mut UserWsRequestHandler) -> Res { - UserWsRoute { - route: route.to_string(), - handler: Box::new(handler), - } - } -} - -/// Get the list of available WebSocket routes -pub fn get_user_ws_routes() -> Vec { - vec![ - // Main controller - UserWsRoute::new("$main/set_incognito", user_ws_actions::set_incognito), - UserWsRoute::new("$main/register_conv", user_ws_actions::register_conv), - UserWsRoute::new("$main/unregister_conv", user_ws_actions::unregister_conv), - UserWsRoute::new("$main/register_post", user_ws_actions::register_post), - UserWsRoute::new("$main/unregister_post", user_ws_actions::unregister_post), - - // Likes controller - UserWsRoute::new("likes/update", likes_controller::update), - - // Conversations controller - UserWsRoute::new("conversations/is_writing", conversations_controller::member_is_writing), - - // Calls controller - UserWsRoute::new("calls/config", calls_controller::get_config), - UserWsRoute::new("calls/join", calls_controller::join_call), - UserWsRoute::new("calls/leave", calls_controller::leave_call), - UserWsRoute::new("calls/members", calls_controller::get_members_list), - UserWsRoute::new("calls/signal", calls_controller::on_client_signal), - UserWsRoute::new("calls/mark_ready", calls_controller::mark_user_ready), - UserWsRoute::new("calls/request_offer", calls_controller::request_offer), - UserWsRoute::new("calls/stop_streaming", calls_controller::stop_streaming), - - // Presence controller - UserWsRoute::new("forez_presence/list", forez_controller::get_list), - UserWsRoute::new("forez_presence/add_day", forez_controller::add_day), - UserWsRoute::new("forez_presence/del_day", forez_controller::del_day), - ] -} /// Search for a route -pub fn find_user_ws_route(uri: &str) -> Option { - get_user_ws_routes().into_iter().find(|r| r.route == uri) +pub async fn exec_user_ws_route(uri: &str, handler: &mut UserWsRequestHandler) -> Option { + match uri { + // Main controller + "$main/set_incognito" => Some(user_ws_actions::set_incognito(handler)), + "$main/register_conv" => Some(user_ws_actions::register_conv(handler)), + "$main/unregister_conv" => Some(user_ws_actions::unregister_conv(handler)), + "$main/register_post" => Some(user_ws_actions::register_post(handler)), + "$main/unregister_post" => Some(user_ws_actions::unregister_post(handler)), + + // Likes controller + "likes/update" => Some(likes_controller::update(handler)), + + // Conversations controller + "conversations/is_writing" => Some(conversations_controller::member_is_writing(handler)), + + // Calls controller + "calls/config" => Some(calls_controller::get_config(handler)), + "calls/join" => Some(calls_controller::join_call(handler)), + "calls/leave" => Some(calls_controller::leave_call(handler)), + "calls/members" => Some(calls_controller::get_members_list(handler)), + "calls/signal" => Some(calls_controller::on_client_signal(handler)), + "calls/mark_ready" => Some(calls_controller::mark_user_ready(handler)), + "calls/request_offer" => Some(calls_controller::request_offer(handler)), + "calls/stop_streaming" => Some(calls_controller::stop_streaming(handler)), + + // Presence controller + "forez_presence/list" => Some(forez_controller::get_list(handler)), + "forez_presence/add_day" => Some(forez_controller::add_day(handler)), + "forez_presence/del_day" => Some(forez_controller::del_day(handler)), + + _ => None, + } } \ No newline at end of file