use crate::controllers::HttpResult; use crate::extractors::matrix_client_extractor::MatrixClientExtractor; use actix_web::{HttpResponse, web}; use futures_util::{StreamExt, stream}; use matrix_sdk::ruma::api::client::profile::{AvatarUrl, DisplayName, get_profile}; use matrix_sdk::ruma::{OwnedMxcUri, OwnedUserId}; #[derive(serde::Deserialize)] pub struct UserIDInPath { user_id: OwnedUserId, } #[derive(serde::Serialize)] struct ProfileResponse { user_id: OwnedUserId, display_name: Option, avatar: Option, } impl ProfileResponse { pub fn from(user_id: OwnedUserId, r: get_profile::v3::Response) -> anyhow::Result { Ok(Self { user_id, display_name: r.get_static::()?, avatar: r.get_static::()?, }) } } /// Get user profile pub async fn get_profile( client: MatrixClientExtractor, path: web::Path, ) -> HttpResult { let profile = client .client .client .account() .fetch_user_profile_of(&path.user_id) .await?; Ok(HttpResponse::Ok().json(ProfileResponse::from(path.user_id.clone(), profile)?)) } /// Get multiple users profiles pub async fn get_multiple(client: MatrixClientExtractor) -> HttpResult { let users = client.auth.decode_json_body::>()?; let list = stream::iter(users) .then(async |user_id| { client .client .client .account() .fetch_user_profile_of(&user_id) .await .map(|r| ProfileResponse::from(user_id, r)) }) .collect::>() .await .into_iter() .collect::, _>>()? .into_iter() .collect::, _>>()?; Ok(HttpResponse::Ok().json(list)) }