From ecbe4885c1a1f071b5349ae58013d197515c9032 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 21 Nov 2025 14:52:21 +0100 Subject: [PATCH] Can get information about rooms --- .../matrix/matrix_room_controller.rs | 58 +++++++++++++++++++ .../src/controllers/matrix/mod.rs | 1 + matrixgw_backend/src/controllers/mod.rs | 1 + matrixgw_backend/src/main.rs | 10 ++++ .../src/matrix_connection/matrix_client.rs | 2 +- 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs create mode 100644 matrixgw_backend/src/controllers/matrix/mod.rs diff --git a/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs b/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs new file mode 100644 index 0000000..f960f78 --- /dev/null +++ b/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs @@ -0,0 +1,58 @@ +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::{OwnedRoomId, OwnedUserId}; +use matrix_sdk::{Room, RoomMemberships}; + +#[derive(serde::Serialize)] +pub struct APIRoomInfo { + id: OwnedRoomId, + name: Option, + members: Vec, + has_avatar: bool, +} + +impl APIRoomInfo { + async fn from_room(r: &Room) -> anyhow::Result { + Ok(Self { + id: r.room_id().to_owned(), + name: r.name(), + members: r + .members(RoomMemberships::ACTIVE) + .await? + .into_iter() + .map(|r| r.user_id().to_owned()) + .collect::>(), + has_avatar: r.avatar_url().is_some(), + }) + } +} + +/// Get the list of joined rooms of the user +pub async fn joined_rooms(client: MatrixClientExtractor) -> HttpResult { + let list = stream::iter(client.client.client.joined_rooms()) + .then(async |room| APIRoomInfo::from_room(&room).await) + .collect::>() + .await + .into_iter() + .collect::, _>>()?; + + Ok(HttpResponse::Ok().json(list)) +} + +#[derive(serde::Deserialize)] +pub struct RoomIdInPath { + id: OwnedRoomId, +} + +/// Get the list of joined rooms of the user +pub async fn single_room_info( + client: MatrixClientExtractor, + path: web::Path, +) -> HttpResult { + Ok(match client.client.client.get_room(&path.id) { + None => HttpResponse::NotFound().json("Room not found"), + Some(r) => HttpResponse::Ok().json(APIRoomInfo::from_room(&r).await?), + }) +} diff --git a/matrixgw_backend/src/controllers/matrix/mod.rs b/matrixgw_backend/src/controllers/matrix/mod.rs new file mode 100644 index 0000000..6742d04 --- /dev/null +++ b/matrixgw_backend/src/controllers/matrix/mod.rs @@ -0,0 +1 @@ +pub mod matrix_room_controller; diff --git a/matrixgw_backend/src/controllers/mod.rs b/matrixgw_backend/src/controllers/mod.rs index f457c29..0384002 100644 --- a/matrixgw_backend/src/controllers/mod.rs +++ b/matrixgw_backend/src/controllers/mod.rs @@ -3,6 +3,7 @@ use actix_web::{HttpResponse, ResponseError}; use std::error::Error; pub mod auth_controller; +pub mod matrix; pub mod matrix_link_controller; pub mod matrix_sync_thread_controller; pub mod server_controller; diff --git a/matrixgw_backend/src/main.rs b/matrixgw_backend/src/main.rs index 75a52ef..fc9c805 100644 --- a/matrixgw_backend/src/main.rs +++ b/matrixgw_backend/src/main.rs @@ -9,6 +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::{ auth_controller, matrix_link_controller, matrix_sync_thread_controller, server_controller, tokens_controller, ws_controller, @@ -134,6 +135,15 @@ async fn main() -> std::io::Result<()> { web::get().to(matrix_sync_thread_controller::status), ) .service(web::resource("/api/ws").route(web::get().to(ws_controller::ws))) + // Matrix room controller + .route( + "/api/matrix/room/joined", + web::get().to(matrix_room_controller::joined_rooms), + ) + .route( + "/api/matrix/room/{id}", + web::get().to(matrix_room_controller::single_room_info), + ) }) .workers(4) .bind(&AppConfig::get().listen_address)? diff --git a/matrixgw_backend/src/matrix_connection/matrix_client.rs b/matrixgw_backend/src/matrix_connection/matrix_client.rs index 488773b..4ec021a 100644 --- a/matrixgw_backend/src/matrix_connection/matrix_client.rs +++ b/matrixgw_backend/src/matrix_connection/matrix_client.rs @@ -91,7 +91,7 @@ pub struct FinishMatrixAuth { pub struct MatrixClient { manager: ActorRef, pub email: UserEmail, - client: Client, + pub client: Client, } impl MatrixClient {