mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-26 15:29:21 +00:00
Can toggle incognito mode
This commit is contained in:
parent
b25628454a
commit
35a77a729e
@ -19,4 +19,5 @@ pub mod movies_controller;
|
|||||||
pub mod virtual_directory_controller;
|
pub mod virtual_directory_controller;
|
||||||
pub mod web_app_controller;
|
pub mod web_app_controller;
|
||||||
pub mod calls_controller;
|
pub mod calls_controller;
|
||||||
pub mod user_ws_routes;
|
pub mod user_ws_routes;
|
||||||
|
pub mod user_ws_actions;
|
15
src/controllers/user_ws_actions.rs
Normal file
15
src/controllers/user_ws_actions.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
//! # User WebSocket actions
|
||||||
|
//!
|
||||||
|
//! This module contains all the base action that can be executed by the WebSocket
|
||||||
|
|
||||||
|
use crate::data::base_request_handler::BaseRequestHandler;
|
||||||
|
use crate::data::error::Res;
|
||||||
|
use crate::data::user_ws_request_handler::UserWsRequestHandler;
|
||||||
|
|
||||||
|
/// Update incognito status of the connection
|
||||||
|
pub fn set_incognito(r: &mut UserWsRequestHandler) -> Res {
|
||||||
|
let new_state = r.post_bool("enable")?;
|
||||||
|
r.update_conn(|c| c.incognito = new_state)?;
|
||||||
|
|
||||||
|
r.success("Updated.")
|
||||||
|
}
|
@ -23,7 +23,7 @@ use crate::data::error::{ExecError, Res, ResultBoxError};
|
|||||||
use crate::data::http_request_handler::HttpRequestHandler;
|
use crate::data::http_request_handler::HttpRequestHandler;
|
||||||
use crate::data::user::UserID;
|
use crate::data::user::UserID;
|
||||||
use crate::data::user_ws_message::UserWsMessage;
|
use crate::data::user_ws_message::UserWsMessage;
|
||||||
use crate::data::user_ws_request_handler::{WsRequestHandler, WsResponseType};
|
use crate::data::user_ws_request_handler::{UserWsRequestHandler, UserWsResponseType};
|
||||||
use crate::utils::crypt_utils::rand_str;
|
use crate::utils::crypt_utils::rand_str;
|
||||||
use crate::utils::date_utils::time;
|
use crate::utils::date_utils::time;
|
||||||
|
|
||||||
@ -106,12 +106,37 @@ mod ws_connections_list {
|
|||||||
use crate::controllers::user_ws_controller::WsSession;
|
use crate::controllers::user_ws_controller::WsSession;
|
||||||
use crate::data::user::UserID;
|
use crate::data::user::UserID;
|
||||||
|
|
||||||
|
/// This structure contains information about an active connection
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct WsConnection {
|
pub struct WsConnection {
|
||||||
pub user_id: UserID,
|
pub user_id: UserID,
|
||||||
pub client_id: u32,
|
pub client_id: u32,
|
||||||
pub remote_ip: String,
|
pub remote_ip: String,
|
||||||
pub session: actix::Addr<WsSession>,
|
pub session: actix::Addr<WsSession>,
|
||||||
|
pub incognito: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WsConnection {
|
||||||
|
/// Change some of the properties of the connection
|
||||||
|
pub fn replace<H>(mut self, do_update: H) -> Self where H: FnOnce(&mut Self) {
|
||||||
|
let list = get_ws_connections_list();
|
||||||
|
let mut list = list.lock().unwrap();
|
||||||
|
|
||||||
|
for i in 0..list.len() {
|
||||||
|
if !list[i].session.eq(&self.session) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
do_update(&mut self);
|
||||||
|
list[i] = self.clone();
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
drop(list);
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
@ -176,6 +201,8 @@ pub fn get_token(r: &mut HttpRequestHandler) -> ResultBoxError {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct WsSession {
|
pub struct WsSession {
|
||||||
|
// NOTE : apart from hb, the values here won't change !
|
||||||
|
|
||||||
user_id: UserID,
|
user_id: UserID,
|
||||||
|
|
||||||
// Client used for the connection
|
// Client used for the connection
|
||||||
@ -231,7 +258,7 @@ impl WsSession {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut handler = WsRequestHandler::new(
|
let mut handler = UserWsRequestHandler::new(
|
||||||
&find_connection(ctx.address()).ok_or(ExecError::boxed_new("Connection not found!"))?,
|
&find_connection(ctx.address()).ok_or(ExecError::boxed_new("Connection not found!"))?,
|
||||||
args,
|
args,
|
||||||
);
|
);
|
||||||
@ -260,8 +287,8 @@ impl WsSession {
|
|||||||
Ok(UserWsMessage {
|
Ok(UserWsMessage {
|
||||||
id: incoming_msg.id,
|
id: incoming_msg.id,
|
||||||
title: match response.r#type {
|
title: match response.r#type {
|
||||||
WsResponseType::SUCCESS => "success".to_string(),
|
UserWsResponseType::SUCCESS => "success".to_string(),
|
||||||
WsResponseType::ERROR => "error".to_string(),
|
UserWsResponseType::ERROR => "error".to_string(),
|
||||||
},
|
},
|
||||||
data: response.content,
|
data: response.content,
|
||||||
})
|
})
|
||||||
@ -281,6 +308,7 @@ impl Actor for WsSession {
|
|||||||
client_id: self.client_id,
|
client_id: self.client_id,
|
||||||
remote_ip: self.remote_ip.clone(),
|
remote_ip: self.remote_ip.clone(),
|
||||||
session: ctx.address(),
|
session: ctx.address(),
|
||||||
|
incognito: self.incognito,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,11 +2,11 @@
|
|||||||
//!
|
//!
|
||||||
//! @author Pierre Hubert
|
//! @author Pierre Hubert
|
||||||
|
|
||||||
use crate::controllers::likes_controller;
|
use crate::controllers::{likes_controller, user_ws_actions};
|
||||||
use crate::data::error::Res;
|
use crate::data::error::Res;
|
||||||
use crate::data::user_ws_request_handler::WsRequestHandler;
|
use crate::data::user_ws_request_handler::UserWsRequestHandler;
|
||||||
|
|
||||||
pub type WsRequestProcess = Box<dyn Fn(&mut WsRequestHandler) -> Res>;
|
pub type WsRequestProcess = Box<dyn Fn(&mut UserWsRequestHandler) -> Res>;
|
||||||
|
|
||||||
/// WebSocket route
|
/// WebSocket route
|
||||||
pub struct UserWsRoute {
|
pub struct UserWsRoute {
|
||||||
@ -16,7 +16,7 @@ pub struct UserWsRoute {
|
|||||||
|
|
||||||
impl UserWsRoute {
|
impl UserWsRoute {
|
||||||
pub fn new<H>(route: &str, handler: H) -> UserWsRoute
|
pub fn new<H>(route: &str, handler: H) -> UserWsRoute
|
||||||
where H: 'static + Fn(&mut WsRequestHandler) -> Res {
|
where H: 'static + Fn(&mut UserWsRequestHandler) -> Res {
|
||||||
UserWsRoute {
|
UserWsRoute {
|
||||||
route: route.to_string(),
|
route: route.to_string(),
|
||||||
handler: Box::new(handler),
|
handler: Box::new(handler),
|
||||||
@ -27,6 +27,9 @@ impl UserWsRoute {
|
|||||||
/// Get the list of available WebSocket routes
|
/// Get the list of available WebSocket routes
|
||||||
pub fn get_user_ws_routes() -> Vec<UserWsRoute> {
|
pub fn get_user_ws_routes() -> Vec<UserWsRoute> {
|
||||||
vec![
|
vec![
|
||||||
|
// Main controller
|
||||||
|
UserWsRoute::new("$main/set_incognito", user_ws_actions::set_incognito),
|
||||||
|
|
||||||
// Likes controller
|
// Likes controller
|
||||||
UserWsRoute::new("likes/update", likes_controller::update)
|
UserWsRoute::new("likes/update", likes_controller::update)
|
||||||
]
|
]
|
||||||
|
@ -11,25 +11,25 @@ use crate::data::base_request_handler::{BaseRequestHandler, RequestValue};
|
|||||||
use crate::data::error::ResultBoxError;
|
use crate::data::error::ResultBoxError;
|
||||||
use crate::data::user::UserID;
|
use crate::data::user::UserID;
|
||||||
|
|
||||||
pub enum WsResponseType {
|
pub enum UserWsResponseType {
|
||||||
SUCCESS,
|
SUCCESS,
|
||||||
ERROR,
|
ERROR,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WsResponse {
|
pub struct UserWsResponse {
|
||||||
pub r#type: WsResponseType,
|
pub r#type: UserWsResponseType,
|
||||||
pub content: serde_json::Value,
|
pub content: serde_json::Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WsRequestHandler {
|
pub struct UserWsRequestHandler {
|
||||||
connection: WsConnection,
|
connection: WsConnection,
|
||||||
args: HashMap<String, RequestValue>,
|
args: HashMap<String, RequestValue>,
|
||||||
response: Option<WsResponse>,
|
response: Option<UserWsResponse>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WsRequestHandler {
|
impl UserWsRequestHandler {
|
||||||
pub fn new(connection: &WsConnection, args: HashMap<String, String>) -> WsRequestHandler {
|
pub fn new(connection: &WsConnection, args: HashMap<String, String>) -> UserWsRequestHandler {
|
||||||
WsRequestHandler {
|
UserWsRequestHandler {
|
||||||
connection: connection.clone(),
|
connection: connection.clone(),
|
||||||
args: args.into_iter().map(|f| (f.0, RequestValue::String(f.1))).collect(),
|
args: args.into_iter().map(|f| (f.0, RequestValue::String(f.1))).collect(),
|
||||||
response: None,
|
response: None,
|
||||||
@ -42,23 +42,30 @@ impl WsRequestHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get the response to the request
|
/// Get the response to the request
|
||||||
pub fn response(mut self) -> WsResponse {
|
pub fn response(mut self) -> UserWsResponse {
|
||||||
if !self.has_response() {
|
if !self.has_response() {
|
||||||
self.success("Request done.").unwrap();
|
self.success("Request done.").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.response.unwrap();
|
return self.response.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update information about the WebSocket connection
|
||||||
|
pub fn update_conn<H>(&mut self, do_updates: H) -> ResultBoxError where H: FnOnce(&mut WsConnection) {
|
||||||
|
self.connection = self.connection.clone().replace(do_updates);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BaseRequestHandler for WsRequestHandler {
|
impl BaseRequestHandler for UserWsRequestHandler {
|
||||||
fn post_parameter_opt(&self, name: &str) -> Option<&RequestValue> {
|
fn post_parameter_opt(&self, name: &str) -> Option<&RequestValue> {
|
||||||
self.args.get(name)
|
self.args.get(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_response<T: Serialize>(&mut self, response: T) -> RequestResult {
|
fn set_response<T: Serialize>(&mut self, response: T) -> RequestResult {
|
||||||
self.response = Some(WsResponse {
|
self.response = Some(UserWsResponse {
|
||||||
r#type: WsResponseType::SUCCESS,
|
r#type: UserWsResponseType::SUCCESS,
|
||||||
content: serde_json::to_value(response)?,
|
content: serde_json::to_value(response)?,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -66,8 +73,8 @@ impl BaseRequestHandler for WsRequestHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn set_error(&mut self, error: HttpError) {
|
fn set_error(&mut self, error: HttpError) {
|
||||||
self.response = Some(WsResponse {
|
self.response = Some(UserWsResponse {
|
||||||
r#type: WsResponseType::ERROR,
|
r#type: UserWsResponseType::ERROR,
|
||||||
content: serde_json::Value::String(error.error.message),
|
content: serde_json::Value::String(error.error.message),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user