2021-02-05 12:21:10 +00:00
|
|
|
//! # User Web Socket Request handler
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
use crate::api_data::http_error::HttpError;
|
|
|
|
use crate::controllers::routes::RequestResult;
|
|
|
|
use crate::data::base_request_handler::{BaseRequestHandler, RequestValue};
|
2021-02-10 16:32:14 +00:00
|
|
|
use crate::data::conversation::ConvID;
|
|
|
|
use crate::data::error::{Res, ResultBoxError};
|
2021-02-05 12:21:10 +00:00
|
|
|
use crate::data::user::UserID;
|
2021-02-10 16:16:52 +00:00
|
|
|
use crate::data::user_ws_connection::UserWsConnection;
|
2021-02-05 12:21:10 +00:00
|
|
|
|
2021-02-05 13:24:00 +00:00
|
|
|
pub enum UserWsResponseType {
|
2021-02-05 12:21:10 +00:00
|
|
|
SUCCESS,
|
|
|
|
ERROR,
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:24:00 +00:00
|
|
|
pub struct UserWsResponse {
|
|
|
|
pub r#type: UserWsResponseType,
|
2021-02-05 12:21:10 +00:00
|
|
|
pub content: serde_json::Value,
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:24:00 +00:00
|
|
|
pub struct UserWsRequestHandler {
|
2021-02-10 16:16:52 +00:00
|
|
|
connection: UserWsConnection,
|
2021-02-05 12:21:10 +00:00
|
|
|
args: HashMap<String, RequestValue>,
|
2021-02-05 13:24:00 +00:00
|
|
|
response: Option<UserWsResponse>,
|
2021-02-05 12:21:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:24:00 +00:00
|
|
|
impl UserWsRequestHandler {
|
2021-02-10 16:16:52 +00:00
|
|
|
pub fn new(connection: &UserWsConnection, args: HashMap<String, String>) -> UserWsRequestHandler {
|
2021-02-05 13:24:00 +00:00
|
|
|
UserWsRequestHandler {
|
2021-02-05 12:21:10 +00:00
|
|
|
connection: connection.clone(),
|
|
|
|
args: args.into_iter().map(|f| (f.0, RequestValue::String(f.1))).collect(),
|
|
|
|
response: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a response has been set
|
|
|
|
pub fn has_response(&self) -> bool {
|
|
|
|
self.response.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the response to the request
|
2021-02-05 13:24:00 +00:00
|
|
|
pub fn response(mut self) -> UserWsResponse {
|
2021-02-05 12:21:10 +00:00
|
|
|
if !self.has_response() {
|
|
|
|
self.success("Request done.").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.response.unwrap();
|
|
|
|
}
|
2021-02-05 13:24:00 +00:00
|
|
|
|
|
|
|
/// Update information about the WebSocket connection
|
2021-02-10 16:16:52 +00:00
|
|
|
pub fn update_conn<H>(&mut self, do_updates: H) -> ResultBoxError where H: FnOnce(&mut UserWsConnection) {
|
2021-02-05 13:24:00 +00:00
|
|
|
self.connection = self.connection.clone().replace(do_updates);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-10 16:32:14 +00:00
|
|
|
|
|
|
|
/// Get the ID of a call included in a WebSocket request
|
|
|
|
pub fn post_call_id(&mut self, name: &str) -> Res<ConvID> {
|
|
|
|
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)
|
|
|
|
}
|
2021-02-05 12:21:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:24:00 +00:00
|
|
|
impl BaseRequestHandler for UserWsRequestHandler {
|
2021-02-05 12:21:10 +00:00
|
|
|
fn post_parameter_opt(&self, name: &str) -> Option<&RequestValue> {
|
|
|
|
self.args.get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_response<T: Serialize>(&mut self, response: T) -> RequestResult {
|
2021-02-05 13:24:00 +00:00
|
|
|
self.response = Some(UserWsResponse {
|
|
|
|
r#type: UserWsResponseType::SUCCESS,
|
2021-02-05 12:21:10 +00:00
|
|
|
content: serde_json::to_value(response)?,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_error(&mut self, error: HttpError) {
|
2021-02-05 13:24:00 +00:00
|
|
|
self.response = Some(UserWsResponse {
|
|
|
|
r#type: UserWsResponseType::ERROR,
|
2021-02-05 12:21:10 +00:00
|
|
|
content: serde_json::Value::String(error.error.message),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remote_ip(&self) -> String {
|
|
|
|
self.connection.remote_ip.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn user_id_opt_ref(&self) -> Option<&UserID> {
|
|
|
|
Some(&self.connection.user_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn user_id(&self) -> ResultBoxError<UserID> {
|
|
|
|
Ok(self.connection.user_id.clone())
|
|
|
|
}
|
|
|
|
}
|