Automatically disconnect user when token is invalid

This commit is contained in:
2025-11-06 21:18:27 +01:00
parent 1ba5372468
commit 8bbbe7022f
5 changed files with 99 additions and 15 deletions

View File

@@ -1,14 +1,17 @@
use crate::broadcast_messages::{BroadcastMessage, BroadcastSender};
use crate::matrix_connection::matrix_client::MatrixClient;
use crate::users::UserEmail;
use ractor::{Actor, ActorProcessingErr, ActorRef, RpcReplyPort};
use std::collections::HashMap;
pub struct MatrixManagerState {
pub broadcast_sender: BroadcastSender,
pub clients: HashMap<UserEmail, MatrixClient>,
}
pub enum MatrixManagerMsg {
GetClient(UserEmail, RpcReplyPort<anyhow::Result<MatrixClient>>),
DisconnectClient(UserEmail),
}
pub struct MatrixManagerActor;
@@ -16,21 +19,22 @@ pub struct MatrixManagerActor;
impl Actor for MatrixManagerActor {
type Msg = MatrixManagerMsg;
type State = MatrixManagerState;
type Arguments = ();
type Arguments = BroadcastSender;
async fn pre_start(
&self,
_myself: ActorRef<Self::Msg>,
_args: Self::Arguments,
args: Self::Arguments,
) -> Result<Self::State, ActorProcessingErr> {
Ok(MatrixManagerState {
broadcast_sender: args,
clients: HashMap::new(),
})
}
async fn handle(
&self,
_myself: ActorRef<Self::Msg>,
myself: ActorRef<Self::Msg>,
message: Self::Msg,
state: &mut Self::State,
) -> Result<(), ActorProcessingErr> {
@@ -41,7 +45,7 @@ impl Actor for MatrixManagerActor {
None => {
// Generate client if required
log::info!("Building new client for {:?}", &email);
match MatrixClient::build_client(&email).await {
match MatrixClient::build_client(myself, &email).await {
Ok(c) => {
state.clients.insert(email.clone(), c.clone());
Ok(c)
@@ -56,6 +60,21 @@ impl Actor for MatrixManagerActor {
log::warn!("Failed to send client information: {e}")
}
}
MatrixManagerMsg::DisconnectClient(email) => {
if let Some(c) = state.clients.remove(&email) {
if let Err(e) = c.disconnect().await {
log::error!("Failed to disconnect client: {e}");
}
if let Err(e) = state
.broadcast_sender
.send(BroadcastMessage::UserDisconnected(email))
{
log::warn!(
"Failed to notify that user has been disconnected from Matrix! {e}"
);
}
}
}
}
Ok(())
}