Compare commits

..
6 changed files with 102 additions and 176 deletions

View file

@ -1,12 +1,13 @@
use std::sync::Arc; use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::oneshot;
use iota_cli::ui::UI;
use iota_cli::screens::terms_checker::{TermsCheckerScreen, UserChoice}; use iota_cli::screens::terms_checker::{TermsCheckerScreen, UserChoice};
use iota_cli::screens::terms_updater::{TermsUpdaterScreen, UpdateDecision}; use iota_cli::screens::terms_updater::{TermsUpdaterScreen, UpdateDecision};
use iota_cli::ui::UI; use iota_terms::{Doc, get_current_docs, get_newest_docs, TermsType as Type};
use iota_terms::{Doc, TermsType as Type, get_current_docs, get_newest_docs};
use iota_util::file_util::{load_file, save_file}; use iota_util::file_util::{load_file, save_file};
use tokio::sync::oneshot;
pub async fn check(ui: Arc<UI>) -> (bool, bool) { pub async fn check(ui: Arc<UI>) -> (bool, bool) {
let mut state = ConsentState::load_state(); let mut state = ConsentState::load_state();
@ -210,6 +211,7 @@ async fn get_updates() -> Option<(
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ConsentState { pub struct ConsentState {
pub eula: Option<Doc>, pub eula: Option<Doc>,

View file

@ -45,9 +45,7 @@ pub async fn load_from_tu(username: &str) -> Result<(), ()> {
Ok(()) Ok(())
} }
pub fn add_user(user: UserProfile) { pub fn add_user(user: UserProfile) { USERS.lock().unwrap().push(user); }
USERS.lock().unwrap().push(user);
}
pub fn get_user_by_username(username: &str) -> Option<UserProfile> { pub fn get_user_by_username(username: &str) -> Option<UserProfile> {
USERS USERS
.lock() .lock()

View file

@ -1,7 +1,7 @@
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use base64::{Engine as _, engine::general_purpose};
use iota_util::file_util::{has_file, load_file, used_dir_space}; use iota_util::file_util::{has_file, load_file, used_dir_space};
use base64::{Engine as _, engine::general_purpose};
use json::{JsonValue, object}; use json::{JsonValue, object};
use rand::Rng; use rand::Rng;
use rand::rngs::OsRng; use rand::rngs::OsRng;

View file

@ -1,3 +1,5 @@
pub mod terms_getter; pub mod terms_getter;
pub use terms_getter::Type as TermsType; pub use terms_getter::Type as TermsType;

View file

@ -569,19 +569,15 @@ impl OmikronConnection {
let height = cv.get_data(DataTypes::height).as_number().unwrap_or(0) as i64; let height = cv.get_data(DataTypes::height).as_number().unwrap_or(0) as i64;
let is_local = iota_storage::users::user_manager::get_user(receiver_id).is_some(); // persist message for the receiver (storage_owner = receiver_id)
chat_files::add_message(
if is_local { timestamp_u128,
// persist message for the receiver (storage_owner = receiver_id) false,
chat_files::add_message( receiver_id as i64,
timestamp_u128, sender_id as i64,
false, &content,
receiver_id as i64, height,
sender_id as i64, );
&content,
height,
);
}
// persist message for the sender (storage_owner = sender_id) // persist message for the sender (storage_owner = sender_id)
chat_files::add_message( chat_files::add_message(
@ -599,168 +595,96 @@ impl OmikronConnection {
.with_receiver(sender_id as u64); .with_receiver(sender_id as u64);
self.send_message(&conf_msg).await; self.send_message(&conf_msg).await;
if !is_local { // Build a live-delivery message for the local client (recipient)
let fw_msg = CommunicationValue::new(CommunicationType::message_other_iota) let user_forward = CommunicationValue::new(CommunicationType::message_live)
.with_id(cv.get_id()) .with_id(cv.get_id())
.with_receiver(receiver_id as u64) .with_receiver(receiver_id as u64)
.with_sender(sender_id as u64) .add_data(DataTypes::send_time, DataValue::Number(timestamp_i64))
.add_data(DataTypes::height, DataValue::Number(height)) .add_data(DataTypes::content, DataValue::Str(content.clone()))
.add_data(DataTypes::content, DataValue::Str(content)) .add_data(DataTypes::sender_id, DataValue::Number(sender_id as i64))
.add_data(DataTypes::send_time, DataValue::Number(timestamp_i64)); .add_data(DataTypes::height, DataValue::Number(height));
let other_iota_resp = self // Attempt delivery and await a response from the local client
.clone() let user_resp = self
.await_response(&fw_msg, Some(Duration::from_secs(10))) .clone()
.await; .await_response(&user_forward, Some(Duration::from_secs(10)))
.await;
if let Ok(resp) = other_iota_resp { if let Ok(user_resp) = user_resp {
let ms_raw = resp let ms_raw = user_resp
.get_data(DataTypes::message_state) .get_data(DataTypes::message_state)
.as_string() .as_string()
.unwrap_or_else(|| "".to_string()); .unwrap_or_else(|| "".to_string());
let ms = MessageState::from_str(&ms_raw).upgrade(MessageState::Received); let ms = MessageState::from_str(&ms_raw).upgrade(MessageState::Received);
let _ = chat_files::change_message_state( // update stored message state for receiver
timestamp_i64, let _ = chat_files::change_message_state(
sender_id as i64, timestamp_i64,
receiver_id as i64, receiver_id as i64,
ms.clone(), sender_id as i64,
); ms.clone(),
);
self.send_message( // update stored message state for sender
&CommunicationValue::new(CommunicationType::message_state) let _ = chat_files::change_message_state(
.with_id(cv.get_id()) timestamp_i64,
.with_receiver(sender_id as u64) sender_id as i64,
.with_sender(receiver_id as u64) receiver_id as i64,
.add_data( ms.clone(),
DataTypes::chat_partner_id, );
DataValue::Number(receiver_id as i64),
)
.add_data(DataTypes::send_time, DataValue::Number(timestamp_i64))
.add_data(
DataTypes::message_state,
DataValue::Str(ms.as_str().to_string()),
),
)
.await;
} else {
let _ = chat_files::change_message_state(
timestamp_i64,
sender_id as i64,
receiver_id as i64,
MessageState::Sent,
);
self.send_message( // notify original sender about the delivered/read state
&CommunicationValue::new(CommunicationType::message_state) self.send_message(
.with_id(cv.get_id()) &CommunicationValue::new(CommunicationType::message_state)
.with_receiver(sender_id as u64) .with_id(cv.get_id())
.with_sender(receiver_id as u64) .with_receiver(sender_id as u64)
.add_data( .with_sender(receiver_id as u64)
DataTypes::chat_partner_id, .add_data(
DataValue::Number(receiver_id as i64), DataTypes::chat_partner_id,
) DataValue::Number(receiver_id as i64),
.add_data(DataTypes::send_time, DataValue::Number(timestamp_i64)) )
.add_data( .add_data(DataTypes::send_time, DataValue::Number(timestamp_i64))
DataTypes::message_state, .add_data(
DataValue::Str(MessageState::Sent.as_str().to_string()), DataTypes::message_state,
), DataValue::Str(ms.as_str().to_string()),
) ),
.await; )
} .await;
return;
} else { } else {
// Build a live-delivery message for the local client (recipient) // Delivery failed or timed out; mark as Sent
let user_forward = CommunicationValue::new(CommunicationType::message_live) let _ = chat_files::change_message_state(
.with_id(cv.get_id()) timestamp_i64,
.with_receiver(receiver_id as u64) receiver_id as i64,
.add_data(DataTypes::send_time, DataValue::Number(timestamp_i64)) sender_id as i64,
.add_data(DataTypes::content, DataValue::Str(content.clone())) MessageState::Sent,
.add_data(DataTypes::sender_id, DataValue::Number(sender_id as i64)) );
.add_data(DataTypes::height, DataValue::Number(height));
// Attempt delivery and await a response from the local client let _ = chat_files::change_message_state(
let user_resp = self timestamp_i64,
.clone() sender_id as i64,
.await_response(&user_forward, Some(Duration::from_secs(10))) receiver_id as i64,
.await; MessageState::Sent,
);
if let Ok(user_resp) = user_resp { // notify sender
let ms_raw = user_resp self.send_message(
.get_data(DataTypes::message_state) &CommunicationValue::new(CommunicationType::message_state)
.as_string() .with_id(cv.get_id())
.unwrap_or_else(|| "".to_string()); .with_receiver(sender_id as u64)
let ms = MessageState::from_str(&ms_raw).upgrade(MessageState::Received); .with_sender(receiver_id as u64)
.add_data(DataTypes::send_time, DataValue::Number(timestamp_i64))
// update stored message state for receiver .add_data(
let _ = chat_files::change_message_state( DataTypes::chat_partner_id,
timestamp_i64, DataValue::Number(sender_id as i64),
receiver_id as i64, )
sender_id as i64, .add_data(
ms.clone(), DataTypes::message_state,
); DataValue::Str(MessageState::Sent.as_str().to_string()),
),
// update stored message state for sender )
let _ = chat_files::change_message_state( .await;
timestamp_i64,
sender_id as i64,
receiver_id as i64,
ms.clone(),
);
// notify original sender about the delivered/read state
self.send_message(
&CommunicationValue::new(CommunicationType::message_state)
.with_id(cv.get_id())
.with_receiver(sender_id as u64)
.with_sender(receiver_id as u64)
.add_data(
DataTypes::chat_partner_id,
DataValue::Number(receiver_id as i64),
)
.add_data(DataTypes::send_time, DataValue::Number(timestamp_i64))
.add_data(
DataTypes::message_state,
DataValue::Str(ms.as_str().to_string()),
),
)
.await;
} else {
// Delivery failed or timed out; mark as Sent
let _ = chat_files::change_message_state(
timestamp_i64,
receiver_id as i64,
sender_id as i64,
MessageState::Sent,
);
let _ = chat_files::change_message_state(
timestamp_i64,
sender_id as i64,
receiver_id as i64,
MessageState::Sent,
);
// notify sender
self.send_message(
&CommunicationValue::new(CommunicationType::message_state)
.with_id(cv.get_id())
.with_receiver(sender_id as u64)
.with_sender(receiver_id as u64)
.add_data(DataTypes::send_time, DataValue::Number(timestamp_i64))
.add_data(
DataTypes::chat_partner_id,
DataValue::Number(sender_id as i64),
)
.add_data(
DataTypes::message_state,
DataValue::Str(MessageState::Sent.as_str().to_string()),
),
)
.await;
}
return;
} }
return;
} }
if cv.is_type(CommunicationType::message_other_iota) { if cv.is_type(CommunicationType::message_other_iota) {

View file

@ -1,7 +1,7 @@
use crate::omikron_connection::OmikronConnection; use crate::omikron_connection::OmikronConnection;
use dashmap::DashMap;
use iota_logger::log;
use iota_state::APP_STATE; use iota_state::APP_STATE;
use iota_logger::log;
use dashmap::DashMap;
use std::sync::LazyLock; use std::sync::LazyLock;
use std::time::Instant; use std::time::Instant;
use tokio::time::Duration; use tokio::time::Duration;