From 934e6a4cc191060c8bebc9ef34e0940301af88d0 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 21 Nov 2025 17:49:41 +0100 Subject: [PATCH] Can get multiple profiles information --- .../matrix/matrix_profile_controller.rs | 31 +++++++++++++++++-- matrixgw_backend/src/main.rs | 4 +++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/matrixgw_backend/src/controllers/matrix/matrix_profile_controller.rs b/matrixgw_backend/src/controllers/matrix/matrix_profile_controller.rs index c0d6935..a5cea9e 100644 --- a/matrixgw_backend/src/controllers/matrix/matrix_profile_controller.rs +++ b/matrixgw_backend/src/controllers/matrix/matrix_profile_controller.rs @@ -1,6 +1,7 @@ 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}; @@ -11,13 +12,15 @@ pub struct UserIDInPath { #[derive(serde::Serialize)] struct ProfileResponse { + user_id: OwnedUserId, display_name: Option, avatar: Option, } impl ProfileResponse { - pub fn from(r: get_profile::v3::Response) -> anyhow::Result { + 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::()?, }) @@ -36,5 +39,29 @@ pub async fn get_profile( .fetch_user_profile_of(&path.user_id) .await?; - Ok(HttpResponse::Ok().json(ProfileResponse::from(profile)?)) + 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)) } diff --git a/matrixgw_backend/src/main.rs b/matrixgw_backend/src/main.rs index 4d1939a..64810f0 100644 --- a/matrixgw_backend/src/main.rs +++ b/matrixgw_backend/src/main.rs @@ -153,6 +153,10 @@ async fn main() -> std::io::Result<()> { "/api/matrix/profile/{user_id}", web::get().to(matrix_profile_controller::get_profile), ) + .route( + "/api/matrix/profile/get_multiple", + web::post().to(matrix_profile_controller::get_multiple), + ) }) .workers(4) .bind(&AppConfig::get().listen_address)?