Start Matrix client authentication
This commit is contained in:
62
matrixgw_backend/src/matrix_connection/matrix_manager.rs
Normal file
62
matrixgw_backend/src/matrix_connection/matrix_manager.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
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 clients: HashMap<UserEmail, MatrixClient>,
|
||||
}
|
||||
|
||||
pub enum MatrixManagerMsg {
|
||||
GetClient(UserEmail, RpcReplyPort<anyhow::Result<MatrixClient>>),
|
||||
}
|
||||
|
||||
pub struct MatrixManagerActor;
|
||||
|
||||
impl Actor for MatrixManagerActor {
|
||||
type Msg = MatrixManagerMsg;
|
||||
type State = MatrixManagerState;
|
||||
type Arguments = ();
|
||||
|
||||
async fn pre_start(
|
||||
&self,
|
||||
_myself: ActorRef<Self::Msg>,
|
||||
_args: Self::Arguments,
|
||||
) -> Result<Self::State, ActorProcessingErr> {
|
||||
Ok(MatrixManagerState {
|
||||
clients: HashMap::new(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn handle(
|
||||
&self,
|
||||
_myself: ActorRef<Self::Msg>,
|
||||
message: Self::Msg,
|
||||
state: &mut Self::State,
|
||||
) -> Result<(), ActorProcessingErr> {
|
||||
match message {
|
||||
// Get client information
|
||||
MatrixManagerMsg::GetClient(email, port) => {
|
||||
let res = port.send(match state.clients.get(&email) {
|
||||
None => {
|
||||
// Generate client if required
|
||||
log::info!("Building new client for {:?}", &email);
|
||||
match MatrixClient::build_client(&email).await {
|
||||
Ok(c) => {
|
||||
state.clients.insert(email.clone(), c.clone());
|
||||
Ok(c)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
Some(c) => Ok(c.clone()),
|
||||
});
|
||||
|
||||
if let Err(e) = res {
|
||||
log::warn!("Failed to send client information: {e}")
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user