diff --git a/src/controllers/calls_controller.rs b/src/controllers/calls_controller.rs index 2fe19bc..155e117 100644 --- a/src/controllers/calls_controller.rs +++ b/src/controllers/calls_controller.rs @@ -15,7 +15,7 @@ use crate::api_data::user_calls_config::UserCallsConfig; use crate::controllers::routes::RequestResult; use crate::controllers::user_ws_controller; use crate::data::base_request_handler::BaseRequestHandler; -use crate::data::call_signal::{CallSignal, IceCandidate, NewUserCallSignal, SdpType}; +use crate::data::call_signal::{CallSignal, IceCandidate, NewUserCallSignal, SdpType, UserCallOfferRequest}; use crate::data::config::conf; use crate::data::conversation::ConvID; use crate::data::error::{ExecError, Res}; @@ -290,6 +290,25 @@ pub fn mark_user_ready(r: &mut UserWsRequestHandler) -> Res { r.success("Information propagated.") } +/// Request an offer from the server +pub fn request_offer(r: &mut UserWsRequestHandler) -> Res { + let call_id = r.post_call_id("callID")?; + + // The ID of the user we stream the audio / video from + let peer_id = r.post_call_peer_id(&call_id, "peerID")?; + + if !peer_id.is_valid() || peer_id == r.user_id_ref()? { + r.forbidden("You can not request an offer for yourself!".to_string())?; + } + + events_helper::propagate_event(&Event::UserRequestedCallOffer(&UserCallOfferRequest { + call_hash: gen_call_hash(&call_id, &peer_id), + user_id: r.user_id()?, + }))?; + + r.success("Request sent") +} + /// Make the user leave the call pub fn make_user_leave_call(conv_id: &ConvID, connection: &UserWsConnection) -> Res { diff --git a/src/controllers/rtc_relay_controller.rs b/src/controllers/rtc_relay_controller.rs index 5d26b69..05edaef 100644 --- a/src/controllers/rtc_relay_controller.rs +++ b/src/controllers/rtc_relay_controller.rs @@ -60,6 +60,14 @@ struct CallUserSignal { data: CallUserSignalData, } +#[allow(non_snake_case)] +#[derive(Serialize)] +struct OfferRequest { + title: String, + callHash: String, + peerId: String, +} + /// Current WebSocket connection static mut ACTIVE_RTC_CONNECTION: Option> = None; @@ -277,6 +285,14 @@ pub fn handle_event(e: &events_helper::Event) -> Res { })?; } + Event::UserRequestedCallOffer(request) => { + send_message_to_relay(OfferRequest { + title: "request_offer".to_string(), + callHash: request.call_hash.to_string(), + peerId: request.user_id.id().to_string(), + })?; + } + _ => {} } diff --git a/src/controllers/user_ws_routes.rs b/src/controllers/user_ws_routes.rs index ef2064b..39f33f6 100644 --- a/src/controllers/user_ws_routes.rs +++ b/src/controllers/user_ws_routes.rs @@ -44,6 +44,7 @@ pub fn get_user_ws_routes() -> Vec { 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), ] } diff --git a/src/data/call_signal.rs b/src/data/call_signal.rs index 3490dc4..9d90ac7 100644 --- a/src/data/call_signal.rs +++ b/src/data/call_signal.rs @@ -42,6 +42,12 @@ pub struct NewRtcRelayMessage { pub data: String, } +/// Request of a user for a call +pub struct UserCallOfferRequest { + pub call_hash: String, + pub user_id: UserID, +} + impl SdpType { pub fn from_str(val: &str) -> Res { match val { diff --git a/src/helpers/events_helper.rs b/src/helpers/events_helper.rs index e773361..f09418c 100644 --- a/src/helpers/events_helper.rs +++ b/src/helpers/events_helper.rs @@ -6,7 +6,7 @@ use crate::controllers::{calls_controller, comments_controller, conversations_controller, notifications_controller, rtc_relay_controller, user_ws_controller}; use crate::data::api_client::APIClient; -use crate::data::call_signal::{NewRtcRelayMessage, NewUserCallSignal}; +use crate::data::call_signal::{NewRtcRelayMessage, NewUserCallSignal, UserCallOfferRequest}; use crate::data::comment::Comment; use crate::data::conversation::ConvID; use crate::data::conversation_message::ConversationMessage; @@ -62,6 +62,9 @@ pub enum Event<'a> { /// Got a new RTC relay message NewRTCRelayMessage(&'a NewRtcRelayMessage), + /// User requested an offer for a call + UserRequestedCallOffer(&'a UserCallOfferRequest), + /// No event None, }