Ready to implement sync thread logic

This commit is contained in:
2025-11-18 22:17:39 +01:00
parent 5c13cffe08
commit c9b703bea3
9 changed files with 126 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
use crate::broadcast_messages::{BroadcastMessage, BroadcastSender};
use crate::matrix_connection::matrix_client::MatrixClient;
use crate::matrix_connection::sync_thread::{MatrixSyncTaskID, start_sync_thread};
use crate::users::UserEmail;
use ractor::{Actor, ActorProcessingErr, ActorRef, RpcReplyPort};
use std::collections::HashMap;
@@ -7,11 +8,13 @@ use std::collections::HashMap;
pub struct MatrixManagerState {
pub broadcast_sender: BroadcastSender,
pub clients: HashMap<UserEmail, MatrixClient>,
pub running_sync_threads: HashMap<UserEmail, MatrixSyncTaskID>,
}
pub enum MatrixManagerMsg {
GetClient(UserEmail, RpcReplyPort<anyhow::Result<MatrixClient>>),
DisconnectClient(UserEmail),
StartSyncThread(UserEmail),
}
pub struct MatrixManagerActor;
@@ -29,6 +32,7 @@ impl Actor for MatrixManagerActor {
Ok(MatrixManagerState {
broadcast_sender: args,
clients: HashMap::new(),
running_sync_threads: Default::default(),
})
}
@@ -62,6 +66,15 @@ impl Actor for MatrixManagerActor {
}
MatrixManagerMsg::DisconnectClient(email) => {
if let Some(c) = state.clients.remove(&email) {
// Stop sync thread (if running)
if let Some(id) = state.running_sync_threads.remove(&email) {
state
.broadcast_sender
.send(BroadcastMessage::StopSyncThread(id))
.ok();
}
// Disconnect client
if let Err(e) = c.disconnect().await {
log::error!("Failed to disconnect client: {e}");
}
@@ -75,6 +88,26 @@ impl Actor for MatrixManagerActor {
}
}
}
MatrixManagerMsg::StartSyncThread(email) => {
// Do nothing if task is already running
if state.running_sync_threads.contains_key(&email) {
log::debug!("Not starting sync thread for {email:?} as it is already running");
return Ok(());
}
let Some(client) = state.clients.get(&email) else {
log::warn!(
"Cannot start sync thread for {email:?} because client is not initialized!"
);
return Ok(());
};
// Start thread
log::debug!("Starting sync thread for {email:?}");
let thread_id =
start_sync_thread(client.clone(), state.broadcast_sender.clone()).await?;
state.running_sync_threads.insert(email, thread_id);
}
}
Ok(())
}