Compare commits

...
6 changed files with 176 additions and 102 deletions

View file

@ -1,13 +1,12 @@
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_terms::{Doc, get_current_docs, get_newest_docs, TermsType as Type}; use iota_cli::ui::UI;
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();
@ -211,7 +210,6 @@ 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,7 +45,9 @@ pub async fn load_from_tu(username: &str) -> Result<(), ()> {
Ok(()) Ok(())
} }
pub fn add_user(user: UserProfile) { USERS.lock().unwrap().push(user); } pub fn add_user(user: UserProfile) {
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 iota_util::file_util::{has_file, load_file, used_dir_space};
use base64::{Engine as _, engine::general_purpose}; use base64::{Engine as _, engine::general_purpose};
use iota_util::file_util::{has_file, load_file, used_dir_space};
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,5 +1,3 @@
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,15 +569,19 @@ 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;
// persist message for the receiver (storage_owner = receiver_id) let is_local = iota_storage::users::user_manager::get_user(receiver_id).is_some();
chat_files::add_message(
timestamp_u128, if is_local {
false, // persist message for the receiver (storage_owner = receiver_id)
receiver_id as i64, chat_files::add_message(
sender_id as i64, timestamp_u128,
&content, false,
height, receiver_id as i64,
); 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(
@ -595,96 +599,168 @@ 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;
// Build a live-delivery message for the local client (recipient) if !is_local {
let user_forward = CommunicationValue::new(CommunicationType::message_live) let fw_msg = CommunicationValue::new(CommunicationType::message_other_iota)
.with_id(cv.get_id()) .with_id(cv.get_id())
.with_receiver(receiver_id as u64) .with_receiver(receiver_id as u64)
.add_data(DataTypes::send_time, DataValue::Number(timestamp_i64)) .with_sender(sender_id as u64)
.add_data(DataTypes::content, DataValue::Str(content.clone())) .add_data(DataTypes::height, DataValue::Number(height))
.add_data(DataTypes::sender_id, DataValue::Number(sender_id as i64)) .add_data(DataTypes::content, DataValue::Str(content))
.add_data(DataTypes::height, DataValue::Number(height)); .add_data(DataTypes::send_time, DataValue::Number(timestamp_i64));
// Attempt delivery and await a response from the local client let other_iota_resp = self
let user_resp = self .clone()
.clone() .await_response(&fw_msg, Some(Duration::from_secs(10)))
.await_response(&user_forward, Some(Duration::from_secs(10))) .await;
.await;
if let Ok(user_resp) = user_resp { if let Ok(resp) = other_iota_resp {
let ms_raw = user_resp let ms_raw = 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);
// update stored message state for receiver let _ = chat_files::change_message_state(
let _ = chat_files::change_message_state( timestamp_i64,
timestamp_i64, sender_id as i64,
receiver_id as i64, receiver_id as i64,
sender_id as i64, ms.clone(),
ms.clone(), );
);
// update stored message state for sender self.send_message(
let _ = chat_files::change_message_state( &CommunicationValue::new(CommunicationType::message_state)
timestamp_i64, .with_id(cv.get_id())
sender_id as i64, .with_receiver(sender_id as u64)
receiver_id as i64, .with_sender(receiver_id as u64)
ms.clone(), .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 {
let _ = chat_files::change_message_state(
timestamp_i64,
sender_id as i64,
receiver_id as i64,
MessageState::Sent,
);
// notify original sender about the delivered/read state self.send_message(
self.send_message( &CommunicationValue::new(CommunicationType::message_state)
&CommunicationValue::new(CommunicationType::message_state) .with_id(cv.get_id())
.with_id(cv.get_id()) .with_receiver(sender_id as u64)
.with_receiver(sender_id as u64) .with_sender(receiver_id as u64)
.with_sender(receiver_id as u64) .add_data(
.add_data( DataTypes::chat_partner_id,
DataTypes::chat_partner_id, DataValue::Number(receiver_id as i64),
DataValue::Number(receiver_id as i64), )
) .add_data(DataTypes::send_time, DataValue::Number(timestamp_i64))
.add_data(DataTypes::send_time, DataValue::Number(timestamp_i64)) .add_data(
.add_data( DataTypes::message_state,
DataTypes::message_state, DataValue::Str(MessageState::Sent.as_str().to_string()),
DataValue::Str(ms.as_str().to_string()), ),
), )
) .await;
.await; }
return;
} else { } else {
// Delivery failed or timed out; mark as Sent // Build a live-delivery message for the local client (recipient)
let _ = chat_files::change_message_state( let user_forward = CommunicationValue::new(CommunicationType::message_live)
timestamp_i64, .with_id(cv.get_id())
receiver_id as i64, .with_receiver(receiver_id as u64)
sender_id as i64, .add_data(DataTypes::send_time, DataValue::Number(timestamp_i64))
MessageState::Sent, .add_data(DataTypes::content, DataValue::Str(content.clone()))
); .add_data(DataTypes::sender_id, DataValue::Number(sender_id as i64))
.add_data(DataTypes::height, DataValue::Number(height));
let _ = chat_files::change_message_state( // Attempt delivery and await a response from the local client
timestamp_i64, let user_resp = self
sender_id as i64, .clone()
receiver_id as i64, .await_response(&user_forward, Some(Duration::from_secs(10)))
MessageState::Sent, .await;
);
// notify sender if let Ok(user_resp) = user_resp {
self.send_message( let ms_raw = user_resp
&CommunicationValue::new(CommunicationType::message_state) .get_data(DataTypes::message_state)
.with_id(cv.get_id()) .as_string()
.with_receiver(sender_id as u64) .unwrap_or_else(|| "".to_string());
.with_sender(receiver_id as u64) let ms = MessageState::from_str(&ms_raw).upgrade(MessageState::Received);
.add_data(DataTypes::send_time, DataValue::Number(timestamp_i64))
.add_data( // update stored message state for receiver
DataTypes::chat_partner_id, let _ = chat_files::change_message_state(
DataValue::Number(sender_id as i64), timestamp_i64,
) receiver_id as i64,
.add_data( sender_id as i64,
DataTypes::message_state, ms.clone(),
DataValue::Str(MessageState::Sent.as_str().to_string()), );
),
) // update stored message state for sender
.await; let _ = chat_files::change_message_state(
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 iota_state::APP_STATE;
use iota_logger::log;
use dashmap::DashMap; use dashmap::DashMap;
use iota_logger::log;
use iota_state::APP_STATE;
use std::sync::LazyLock; use std::sync::LazyLock;
use std::time::Instant; use std::time::Instant;
use tokio::time::Duration; use tokio::time::Duration;