From b744265242616414c1b356d9a849664c9e99fab8 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 21 Nov 2025 17:14:23 +0100 Subject: [PATCH] Can get single profile information --- .../matrix/matrix_profile_controller.rs | 40 +++++++++++++++++++ .../matrix/matrix_room_controller.rs | 6 +-- .../src/controllers/matrix/mod.rs | 1 + matrixgw_backend/src/main.rs | 7 +++- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 matrixgw_backend/src/controllers/matrix/matrix_profile_controller.rs diff --git a/matrixgw_backend/src/controllers/matrix/matrix_profile_controller.rs b/matrixgw_backend/src/controllers/matrix/matrix_profile_controller.rs new file mode 100644 index 0000000..c0d6935 --- /dev/null +++ b/matrixgw_backend/src/controllers/matrix/matrix_profile_controller.rs @@ -0,0 +1,40 @@ +use crate::controllers::HttpResult; +use crate::extractors::matrix_client_extractor::MatrixClientExtractor; +use actix_web::{HttpResponse, web}; +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 { + display_name: Option, + avatar: Option, +} + +impl ProfileResponse { + pub fn from(r: get_profile::v3::Response) -> anyhow::Result { + Ok(Self { + 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(profile)?)) +} diff --git a/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs b/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs index cc6c22b..927fe6b 100644 --- a/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs +++ b/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs @@ -3,7 +3,7 @@ use crate::controllers::matrix::media_controller; use crate::extractors::matrix_client_extractor::MatrixClientExtractor; use actix_web::{HttpRequest, HttpResponse, web}; use futures_util::{StreamExt, stream}; -use matrix_sdk::ruma::{OwnedRoomId, OwnedUserId}; +use matrix_sdk::ruma::{OwnedMxcUri, OwnedRoomId, OwnedUserId}; use matrix_sdk::{Room, RoomMemberships}; #[derive(serde::Serialize)] @@ -11,7 +11,7 @@ pub struct APIRoomInfo { id: OwnedRoomId, name: Option, members: Vec, - has_avatar: bool, + avatar: Option, } impl APIRoomInfo { @@ -25,7 +25,7 @@ impl APIRoomInfo { .into_iter() .map(|r| r.user_id().to_owned()) .collect::>(), - has_avatar: r.avatar_url().is_some(), + avatar: r.avatar_url(), }) } } diff --git a/matrixgw_backend/src/controllers/matrix/mod.rs b/matrixgw_backend/src/controllers/matrix/mod.rs index 5ce5ba4..8c43048 100644 --- a/matrixgw_backend/src/controllers/matrix/mod.rs +++ b/matrixgw_backend/src/controllers/matrix/mod.rs @@ -1,2 +1,3 @@ +pub mod matrix_profile_controller; pub mod matrix_room_controller; pub mod media_controller; diff --git a/matrixgw_backend/src/main.rs b/matrixgw_backend/src/main.rs index 321f59c..4d1939a 100644 --- a/matrixgw_backend/src/main.rs +++ b/matrixgw_backend/src/main.rs @@ -9,7 +9,7 @@ use actix_web::{App, HttpServer, web}; use matrixgw_backend::app_config::AppConfig; use matrixgw_backend::broadcast_messages::BroadcastMessage; use matrixgw_backend::constants; -use matrixgw_backend::controllers::matrix::matrix_room_controller; +use matrixgw_backend::controllers::matrix::{matrix_profile_controller, matrix_room_controller}; use matrixgw_backend::controllers::{ auth_controller, matrix_link_controller, matrix_sync_thread_controller, server_controller, tokens_controller, ws_controller, @@ -148,6 +148,11 @@ async fn main() -> std::io::Result<()> { "/api/matrix/room/{id}/avatar", web::get().to(matrix_room_controller::room_avatar), ) + // Matrix profile controller + .route( + "/api/matrix/profile/{user_id}", + web::get().to(matrix_profile_controller::get_profile), + ) }) .workers(4) .bind(&AppConfig::get().listen_address)?