diff --git a/src/data/communication.rs b/src/data/communication.rs index 0620c36..2f8c689 100644 --- a/src/data/communication.rs +++ b/src/data/communication.rs @@ -355,22 +355,22 @@ impl CommunicationValue { object! { id: self.id.to_string(), type: format!("{:?}", self.comm_type), - sender: self.sender.to_string(), - receiver: self.receiver.to_string(), + sender: self.sender, + receiver: self.receiver, data: jdata } } else if self.sender > 0 { object! { id: self.id.to_string(), type: format!("{:?}", self.comm_type), - sender: self.sender.to_string(), + sender: self.sender, data: jdata } } else if self.receiver > 0 { object! { id: self.id.to_string(), type: format!("{:?}", self.comm_type), - receiver: self.receiver.to_string(), + receiver: self.receiver, data: jdata } } else { diff --git a/src/rho/client_connection.rs b/src/rho/client_connection.rs index 8f97671..20f90a4 100644 --- a/src/rho/client_connection.rs +++ b/src/rho/client_connection.rs @@ -357,9 +357,8 @@ impl ClientConnection { /// Forward message to Iota async fn forward_to_iota(&self, cv: CommunicationValue) { - let user_id = self.get_user_id().await; if let Some(rho_conn) = self.get_rho_connection().await { - let updated_cv = cv.with_sender(user_id); + let updated_cv = cv.with_sender(self.get_user_id().await); rho_conn.message_to_iota(updated_cv).await; } } diff --git a/src/rho/iota_connection.rs b/src/rho/iota_connection.rs index 1293328..53352ac 100644 --- a/src/rho/iota_connection.rs +++ b/src/rho/iota_connection.rs @@ -15,7 +15,6 @@ use std::{ use tokio::sync::RwLock; use tokio_util::compat::Compat; use tungstenite::Utf8Bytes; -use uuid::Uuid; use super::{rho_connection::RhoConnection, rho_manager}; use crate::{ @@ -273,21 +272,35 @@ impl IotaConnection { async fn handle_forward_message(&self, cv: CommunicationValue) { let receiver_id = cv.get_receiver(); let sender_id = cv.get_sender(); - if !self.get_user_ids().await.contains(&sender_id) { - self.send_message(CommunicationValue::new( - CommunicationType::error_invalid_user_id, - )) - .await; - return; - } - if let Some(target_rho) = rho_manager::get_rho_con_for_user(receiver_id).await { - target_rho.message_to_iota(cv).await; + if self.get_user_ids().await.contains(&receiver_id) { + if let Some(target_rho) = self.get_rho_connection().await { + target_rho.message_to_iota(cv).await; + } else { + let error = CommunicationValue::new(CommunicationType::error) + .with_id(cv.get_id()) + .with_sender(cv.get_sender()); + self.send_message(error).await; + } + } else if self.get_user_ids().await.contains(&sender_id) { + if let Some(target_rho) = rho_manager::get_rho_con_for_user(receiver_id).await { + target_rho.message_to_iota(cv).await; + } else { + let error = CommunicationValue::new(CommunicationType::error_no_iota) + .with_id(cv.get_id()) + .with_sender(cv.get_sender()); + self.send_message(error).await; + } } else { - let error = CommunicationValue::new(CommunicationType::error_no_iota) - .with_id(cv.get_id()) - .with_sender(cv.get_sender()); - self.send_message(error).await; + self.send_message( + CommunicationValue::new(CommunicationType::error_invalid_user_id).add_data( + DataTypes::error_type, + JsonValue::String( + "You are sending to another User without authority.".to_string(), + ), + ), + ) + .await; } } diff --git a/src/rho/rho_connection.rs b/src/rho/rho_connection.rs index 6905c0c..aac69de 100644 --- a/src/rho/rho_connection.rs +++ b/src/rho/rho_connection.rs @@ -126,15 +126,12 @@ impl RhoConnection { pub async fn message_to_client(&self, cv: CommunicationValue) { let connections = self.client_connections.read().await; for connection in connections.iter() { - connection.send_message(&cv).await; + if connection.get_user_id().await == cv.receiver { + connection.send_message(&cv).await; + } } } - /// Send message to Iota as string - pub async fn message_to_iota_str(&self, message: &str) { - self.iota_connection.send_message_str(message).await; - } - /// Send message to Iota pub async fn message_to_iota(&self, cv: CommunicationValue) { self.iota_connection.send_message(cv).await;