diff --git a/src/api_data/call_member_info.rs b/src/api_data/call_member_info.rs new file mode 100644 index 0000000..e1da090 --- /dev/null +++ b/src/api_data/call_member_info.rs @@ -0,0 +1,23 @@ +//! # Call member information +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::data::user::UserID; +use crate::data::user_ws_connection::ActiveCall; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct CallMemberInfo { + userID: u64, + ready: bool, +} + +impl CallMemberInfo { + pub fn new(user_id: &UserID, call: &ActiveCall) -> Self { + Self { + userID: user_id.id(), + ready: call.ready, + } + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index f1375e7..cab165c 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -58,4 +58,5 @@ pub mod account_image_settings_api; pub mod res_create_custom_emoji; pub mod res_get_ws_token; pub mod user_calls_config; -pub mod joined_call_message; \ No newline at end of file +pub mod joined_call_message; +pub mod call_member_info; \ No newline at end of file diff --git a/src/controllers/calls_controller.rs b/src/controllers/calls_controller.rs index 974d429..9221871 100644 --- a/src/controllers/calls_controller.rs +++ b/src/controllers/calls_controller.rs @@ -4,6 +4,7 @@ use std::collections::HashMap; +use crate::api_data::call_member_info::CallMemberInfo; use crate::api_data::joined_call_message::JoinedCallMessage; use crate::api_data::user_calls_config::UserCallsConfig; use crate::controllers::routes::RequestResult; @@ -61,6 +62,23 @@ pub fn join_call(r: &mut UserWsRequestHandler) -> RequestResult { Ok(()) } +/// Get the list of members of a call +pub fn get_members_list(r: &mut UserWsRequestHandler) -> RequestResult { + let conv_id = r.post_call_id("callID")?; + let mut list = vec![]; + + user_ws_controller::foreach_connection(|conn| { + if conn.is_having_call_with_conversation(&conv_id) { + list.push(CallMemberInfo::new(&conn.user_id, &conn.active_call.as_ref().unwrap())); + } + + Ok(()) + })?; + + r.set_response(list) +} + + /// Events handler pub fn handle_event(e: &events_helper::Event) -> Res { match e { diff --git a/src/controllers/user_ws_controller.rs b/src/controllers/user_ws_controller.rs index f7c9443..eeab586 100644 --- a/src/controllers/user_ws_controller.rs +++ b/src/controllers/user_ws_controller.rs @@ -560,6 +560,16 @@ pub fn disconnect_user_from_all_sockets(user_id: &UserID) -> Res { Ok(()) } +/// Do something with all active connections +pub fn foreach_connection(mut f: F) -> Res + where F: FnMut(&UserWsConnection) -> Res { + for conn in get_ws_connections_list().lock().unwrap().iter() { + f(conn)?; + } + + Ok(()) +} + /// Events handler pub fn handle_event(e: &events_helper::Event) -> Res { match e { diff --git a/src/controllers/user_ws_routes.rs b/src/controllers/user_ws_routes.rs index f3fe605..82eff11 100644 --- a/src/controllers/user_ws_routes.rs +++ b/src/controllers/user_ws_routes.rs @@ -40,6 +40,7 @@ pub fn get_user_ws_routes() -> Vec { // Calls controller UserWsRoute::new("calls/config", calls_controller::get_config), UserWsRoute::new("calls/join", calls_controller::join_call), + UserWsRoute::new("calls/members", calls_controller::get_members_list), ] } diff --git a/src/data/user_ws_request_handler.rs b/src/data/user_ws_request_handler.rs index de48f5b..cf74ad9 100644 --- a/src/data/user_ws_request_handler.rs +++ b/src/data/user_ws_request_handler.rs @@ -7,7 +7,8 @@ use serde::Serialize; use crate::api_data::http_error::HttpError; use crate::controllers::routes::RequestResult; use crate::data::base_request_handler::{BaseRequestHandler, RequestValue}; -use crate::data::error::ResultBoxError; +use crate::data::conversation::ConvID; +use crate::data::error::{Res, ResultBoxError}; use crate::data::user::UserID; use crate::data::user_ws_connection::UserWsConnection; @@ -56,6 +57,17 @@ impl UserWsRequestHandler { Ok(()) } + + /// Get the ID of a call included in a WebSocket request + pub fn post_call_id(&mut self, name: &str) -> Res { + let conv_id = self.post_u64(name)?; + + if !self.connection.is_having_call_with_conversation(&conv_id) { + self.forbidden("You do not belong to this call!".to_string())?; + } + + Ok(conv_id) + } } impl BaseRequestHandler for UserWsRequestHandler {