1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-03-14 01:42:37 +00:00
comunicapiv3/src/controllers/calls_controller.rs

79 lines
2.6 KiB
Rust
Raw Normal View History

2020-07-13 11:49:14 +02:00
//! # Calls controller
//!
//! @author Pierre Hubert
use std::collections::HashMap;
2021-02-10 17:16:52 +01:00
use crate::api_data::joined_call_message::JoinedCallMessage;
2021-02-08 18:24:28 +01:00
use crate::api_data::user_calls_config::UserCallsConfig;
2020-07-13 11:49:14 +02:00
use crate::controllers::routes::RequestResult;
2021-02-10 17:16:52 +01:00
use crate::controllers::user_ws_controller;
2021-02-05 09:11:30 +01:00
use crate::data::base_request_handler::BaseRequestHandler;
2021-02-08 18:24:28 +01:00
use crate::data::config::conf;
2021-02-10 17:16:52 +01:00
use crate::data::error::{ExecError, Res};
2020-07-13 11:49:14 +02:00
use crate::data::http_request_handler::HttpRequestHandler;
2021-02-10 17:16:52 +01:00
use crate::data::user_ws_connection::ActiveCall;
use crate::data::user_ws_message::UserWsMessage;
2021-02-08 18:24:28 +01:00
use crate::data::user_ws_request_handler::UserWsRequestHandler;
2021-02-10 17:16:52 +01:00
use crate::helpers::{calls_helper, conversations_helper, events_helper};
use crate::helpers::events_helper::Event;
2020-07-13 11:49:14 +02:00
/// Get legacy call configuration
pub fn get_legacy_config(r: &mut HttpRequestHandler) -> RequestResult {
let mut map = HashMap::new();
map.insert("enabled", false);
r.set_response(map)
2021-02-08 18:24:28 +01:00
}
/// Get calls configuration
pub fn get_config(r: &mut UserWsRequestHandler) -> RequestResult {
// TODO : check whether the user is the member of a call or not
if let Some(conf) = conf().rtc_relay.as_ref()
{
return r.set_response(UserCallsConfig::new(conf));
}
r.internal_error(ExecError::boxed_new("Missing calls configuration!"))
2021-02-10 17:16:52 +01:00
}
/// Join a call
pub fn join_call(r: &mut UserWsRequestHandler) -> RequestResult {
let conv_id = r.post_conv_id("convID")?;
// Check if the conversation can have a call
let conv = conversations_helper::get_single(conv_id, r.user_id_ref()?)?;
if !calls_helper::can_have_call(&conv) {
r.forbidden("This conversation can not be used to make calls!".to_string())?;
}
// TODO : Remove any previous call linked to current connection + any other active connection
// of current user to current conversation call
r.update_conn(|r| r.active_call = Some(ActiveCall {
conv_id,
ready: false,
}))?;
// Propagate event
events_helper::propagate_event(&Event::UserJoinedCall(conv_id, r.user_id()?))?;
Ok(())
}
/// Events handler
pub fn handle_event(e: &events_helper::Event) -> Res {
match e {
Event::UserJoinedCall(conv_id, user_id) => {
user_ws_controller::send_message_to_specific_connections(
|c| c.is_having_call_with_conversation(conv_id) && &c.user_id != user_id,
|_| UserWsMessage::no_id_message("user_joined_call", JoinedCallMessage::new(conv_id, user_id)),
None::<fn(&_) -> _>,
)?;
}
_ => {}
}
Ok(())
2020-07-13 11:49:14 +02:00
}