[WIP] Message State Handling [Del] Removed Community Connection /

Decentralized support from Omikron Connection
This commit is contained in:
Alex-Emmet 2026-02-08 22:03:16 +01:00
commit fd05766fe9
2 changed files with 31 additions and 112 deletions

View file

@ -1,8 +1,7 @@
use crate::auth::local_auth;
use crate::gui::log_panel::{log_cv, log_message_format}; use crate::gui::log_panel::{log_cv, log_message_format};
use crate::users::contact::Contact; use crate::users::contact::Contact;
use crate::users::user_community_util::UserCommunityUtil; use crate::users::user_community_util::UserCommunityUtil;
use crate::util::chat_files::MessageState; use crate::util::chat_files::{MessageState, change_message_state};
use crate::util::chats_util::{get_user, mod_user}; use crate::util::chats_util::{get_user, mod_user};
use crate::util::crypto_util::{DataFormat, SecurePayload}; use crate::util::crypto_util::{DataFormat, SecurePayload};
use crate::util::file_util::{get_children, load_file, save_file}; use crate::util::file_util::{get_children, load_file, save_file};
@ -34,17 +33,8 @@ use uuid::Uuid;
pub static OMIKRON_CONNECTION: LazyLock<Arc<RwLock<Option<Arc<OmikronConnection>>>>> = pub static OMIKRON_CONNECTION: LazyLock<Arc<RwLock<Option<Arc<OmikronConnection>>>>> =
LazyLock::new(|| Arc::new(RwLock::new(None))); LazyLock::new(|| Arc::new(RwLock::new(None)));
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConnectionVariant {
Omikron,
ClientUnauthenticated,
ClientAuthenticated,
}
#[derive(Clone)] #[derive(Clone)]
pub struct OmikronConnection { pub struct OmikronConnection {
pub variant: Arc<RwLock<ConnectionVariant>>,
pub user_id: Arc<RwLock<i64>>,
pub(crate) writer: pub(crate) writer:
Arc<Mutex<Option<Box<dyn Sink<Message, Error = tungstenite::Error> + Send + Unpin>>>>, Arc<Mutex<Option<Box<dyn Sink<Message, Error = tungstenite::Error> + Send + Unpin>>>>,
waiting: Arc<DashMap<Uuid, Box<dyn Fn(CommunicationValue) + Send + Sync>>>, waiting: Arc<DashMap<Uuid, Box<dyn Fn(CommunicationValue) + Send + Sync>>>,
@ -56,8 +46,6 @@ pub struct OmikronConnection {
impl OmikronConnection { impl OmikronConnection {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
variant: Arc::new(RwLock::new(ConnectionVariant::Omikron)),
user_id: Arc::new(RwLock::new(0)),
writer: Arc::new(Mutex::new(None)), writer: Arc::new(Mutex::new(None)),
waiting: Arc::new(DashMap::new()), waiting: Arc::new(DashMap::new()),
last_ping: Arc::new(Mutex::new(-1)), last_ping: Arc::new(Mutex::new(-1)),
@ -70,8 +58,6 @@ impl OmikronConnection {
reader: SplitStream<tokio_tungstenite::WebSocketStream<TokioIo<Upgraded>>>, reader: SplitStream<tokio_tungstenite::WebSocketStream<TokioIo<Upgraded>>>,
) -> Arc<Self> { ) -> Arc<Self> {
let connection = Arc::new(Self { let connection = Arc::new(Self {
variant: Arc::new(RwLock::new(ConnectionVariant::ClientUnauthenticated)),
user_id: Arc::new(RwLock::new(0)),
writer: Arc::new(Mutex::new(Some(Box::new(writer) writer: Arc::new(Mutex::new(Some(Box::new(writer)
as Box<dyn Sink<Message, Error = tungstenite::Error> + Send + Unpin>))), as Box<dyn Sink<Message, Error = tungstenite::Error> + Send + Unpin>))),
waiting: Arc::new(DashMap::new()), waiting: Arc::new(DashMap::new()),
@ -207,13 +193,6 @@ impl OmikronConnection {
.await; .await;
} }
pub async fn set_variant(self: &Arc<Self>, variant: ConnectionVariant) {
*self.variant.write().await = variant;
}
pub async fn set_user_id(self: &Arc<Self>, user_id: i64) {
*self.user_id.write().await = user_id;
}
async fn spawn_listener( async fn spawn_listener(
self: &Arc<Self>, self: &Arc<Self>,
mut read_half: Box<dyn Stream<Item = Result<Message, tungstenite::Error>> + Send + Unpin>, mut read_half: Box<dyn Stream<Item = Result<Message, tungstenite::Error>> + Send + Unpin>,
@ -222,7 +201,6 @@ impl OmikronConnection {
let writer_out = self.writer.clone(); let writer_out = self.writer.clone();
let is_connected_out = self.is_connected.clone(); let is_connected_out = self.is_connected.clone();
let sel_out = self.clone(); let sel_out = self.clone();
let variant = self.variant.clone();
{ {
ACTIVE_TASKS.lock().unwrap().push("Listener".to_string()); ACTIVE_TASKS.lock().unwrap().push("Listener".to_string());
@ -237,7 +215,6 @@ impl OmikronConnection {
waiting_out.clone(), waiting_out.clone(),
writer_out.clone(), writer_out.clone(),
is_connected_out.clone(), is_connected_out.clone(),
variant.clone(),
); );
} }
*is_connected_out.lock().await = false; *is_connected_out.lock().await = false;
@ -260,7 +237,6 @@ impl OmikronConnection {
>, >,
>, >,
is_connected: Arc<Mutex<bool>>, is_connected: Arc<Mutex<bool>>,
variant: Arc<RwLock<ConnectionVariant>>,
) { ) {
tokio::spawn(async move { tokio::spawn(async move {
match msg { match msg {
@ -358,70 +334,6 @@ impl OmikronConnection {
return; return;
} }
let com = variant.read().await.clone();
if com == ConnectionVariant::ClientUnauthenticated {
if cv.is_type(CommunicationType::identification) {
// Extract user ID
let user_id: i64 = cv
.get_data(DataTypes::user_id)
.unwrap_or(&JsonValue::Null)
.as_i64()
.unwrap_or(0);
if user_id == 0 {
self.send_message(
&CommunicationValue::new(
CommunicationType::error_invalid_user_id,
)
.with_id(cv.get_id()),
)
.await;
return;
}
// Validate private key
if let Some(private_key_hash) = cv.get_data(DataTypes::private_key_hash)
{
log_message(format!("private_key_hash: {}", private_key_hash));
let is_valid = local_auth::is_private_key_valid(
&user_id,
&private_key_hash.to_string(),
);
if !is_valid {
log_message("Invalid private key");
self.send_message(
&CommunicationValue::new(
CommunicationType::error_invalid_private_key,
)
.with_id(cv.get_id()),
)
.await;
return;
}
} else {
log_message("Missing private key");
self.send_message(
&CommunicationValue::new(
CommunicationType::error_invalid_private_key,
)
.with_id(cv.get_id()),
)
.await;
return;
}
// Set identification data
self.set_user_id(user_id).await;
self.set_variant(ConnectionVariant::ClientAuthenticated)
.await;
let response =
CommunicationValue::new(CommunicationType::identification_response)
.with_id(cv.get_id());
self.send_message(&response).await;
}
}
// ************************************************ // // ************************************************ //
// Direct messages // // Direct messages //
// ************************************************ // // ************************************************ //
@ -452,17 +364,18 @@ impl OmikronConnection {
if cv.is_type(CommunicationType::message_other_iota) { if cv.is_type(CommunicationType::message_other_iota) {
let sender_id = &cv.get_sender(); let sender_id = &cv.get_sender();
let receiver_id = &cv.get_receiver(); let receiver_id = &cv.get_receiver();
let timestamp = cv
.get_data(DataTypes::send_time)
.unwrap_or(&JsonValue::new_object())
.as_i64()
.unwrap_or(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as i64,
);
chat_files::add_message( chat_files::add_message(
cv.get_data(DataTypes::send_time) timestamp as u128,
.unwrap_or(&JsonValue::new_object())
.as_i64()
.unwrap_or(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as i64,
) as u128,
false, false,
*receiver_id, *receiver_id,
*sender_id, *sender_id,
@ -489,14 +402,20 @@ impl OmikronConnection {
.await; .await;
if let Ok(user_resp) = user_resp { if let Ok(user_resp) = user_resp {
let ms: MessageState = MessageState::from_str( let ms = MessageState::from_str(
user_resp user_resp
.get_data(DataTypes::message_state) .get_data(DataTypes::message_state)
.unwrap_or(&JsonValue::Null) .unwrap_or(&JsonValue::Null)
.as_str() .as_str()
.unwrap_or(""), .unwrap_or(""),
) )
.upgrade(MessageState::Send); .upgrade(MessageState::Received);
let _ = change_message_state(
timestamp,
*receiver_id,
*sender_id,
ms.clone(),
);
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())
@ -524,7 +443,7 @@ impl OmikronConnection {
) )
.add_data( .add_data(
DataTypes::message_state, DataTypes::message_state,
JsonValue::from(MessageState::Send.as_str()), JsonValue::from(MessageState::Sent.as_str()),
), ),
) )
.await; .await;

View file

@ -9,24 +9,24 @@ use crate::gui::log_panel::log_message;
pub enum MessageState { pub enum MessageState {
Read, Read,
Received, Received,
Send, Sent,
Sending, Sending,
} }
impl MessageState { impl MessageState {
pub fn as_str(&self) -> &'static str { pub fn as_str(&self) -> &'static str {
match self { match self {
MessageState::Read => "READ", MessageState::Read => "read",
MessageState::Received => "RECEIVED", MessageState::Received => "received",
MessageState::Send => "SEND", MessageState::Sent => "sent",
MessageState::Sending => "SENDING", MessageState::Sending => "sending",
} }
} }
pub fn from_str(str: &str) -> Self { pub fn from_str(str: &str) -> Self {
match str.to_uppercase().as_str() { match str.to_uppercase().as_str() {
"READ" => MessageState::Read, "read" => MessageState::Read,
"RECEIVED" => MessageState::Received, "received" => MessageState::Received,
"SEND" => MessageState::Send, "sent" => MessageState::Sent,
_ => MessageState::Sending, _ => MessageState::Sending,
} }
} }
@ -35,8 +35,8 @@ impl MessageState {
Self::Read Self::Read
} else if other == Self::Received || self == Self::Received { } else if other == Self::Received || self == Self::Received {
Self::Received Self::Received
} else if other == Self::Send || self == Self::Send { } else if other == Self::Sent || self == Self::Sent {
Self::Send Self::Sent
} else { } else {
Self::Sending Self::Sending
} }