use crate::controllers::HttpResult; use crate::extractors::auth_extractor::AuthExtractor; use crate::extractors::matrix_client_extractor::MatrixClientExtractor; use crate::matrix_connection::matrix_client::FinishMatrixAuth; use crate::matrix_connection::matrix_manager::MatrixManagerMsg; use actix_web::{HttpResponse, web}; use ractor::ActorRef; #[derive(serde::Serialize)] struct StartAuthResponse { url: String, } /// Start user authentication on Matrix server pub async fn start_auth(client: MatrixClientExtractor) -> HttpResult { let url = client.client.initiate_login().await?.to_string(); Ok(HttpResponse::Ok().json(StartAuthResponse { url })) } /// Finish user authentication on Matrix server pub async fn finish_auth(client: MatrixClientExtractor) -> HttpResult { match client .client .finish_login(client.auth.decode_json_body::()?) .await { Ok(_) => Ok(HttpResponse::Accepted().finish()), Err(e) => { log::error!("Failed to finish Matrix authentication: {e}"); Err(e.into()) } } } /// Logout user from Matrix server pub async fn logout( auth: AuthExtractor, manager: web::Data>, ) -> HttpResult { manager .cast(MatrixManagerMsg::DisconnectClient(auth.user.email)) .expect("Failed to communicate with matrix manager!"); Ok(HttpResponse::Ok().finish()) } #[derive(serde::Deserialize)] struct SetRecoveryKeyRequest { key: String, } /// Set recovery key of user pub async fn set_recovery_key(client: MatrixClientExtractor) -> HttpResult { let key = client.auth.decode_json_body::()?.key; client.client.set_recovery_key(&key).await?; Ok(HttpResponse::Accepted().finish()) }