50 lines
1.7 KiB
Rust
50 lines
1.7 KiB
Rust
use crate::extractors::auth_extractor::AuthExtractor;
|
|
use crate::matrix_connection::matrix_client::MatrixClient;
|
|
use crate::matrix_connection::matrix_manager::MatrixManagerMsg;
|
|
use crate::users::ExtendedUserInfo;
|
|
use actix_web::dev::Payload;
|
|
use actix_web::{FromRequest, HttpRequest, web};
|
|
use ractor::ActorRef;
|
|
|
|
pub struct MatrixClientExtractor {
|
|
pub auth: AuthExtractor,
|
|
pub client: MatrixClient,
|
|
}
|
|
|
|
impl MatrixClientExtractor {
|
|
pub async fn to_extended_user_info(&self) -> anyhow::Result<ExtendedUserInfo> {
|
|
Ok(ExtendedUserInfo {
|
|
user: self.auth.user.clone(),
|
|
matrix_user_id: self.client.client.user_id().map(|id| id.to_string()),
|
|
matrix_device_id: self.client.client.device_id().map(|id| id.to_string()),
|
|
matrix_recovery_state: self.client.recovery_state(),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl FromRequest for MatrixClientExtractor {
|
|
type Error = actix_web::Error;
|
|
type Future = futures_util::future::LocalBoxFuture<'static, Result<Self, Self::Error>>;
|
|
|
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
|
let req = req.clone();
|
|
let mut payload = payload.take();
|
|
Box::pin(async move {
|
|
let auth = AuthExtractor::from_request(&req, &mut payload).await?;
|
|
|
|
let matrix_manager_actor =
|
|
web::Data::<ActorRef<MatrixManagerMsg>>::from_request(&req, &mut Payload::None)
|
|
.await?;
|
|
let client = ractor::call!(
|
|
matrix_manager_actor,
|
|
MatrixManagerMsg::GetClient,
|
|
auth.user.email.clone()
|
|
)
|
|
.expect("Failed to query manager actor!")
|
|
.expect("Failed to get client!");
|
|
|
|
Ok(Self { auth, client })
|
|
})
|
|
}
|
|
}
|