diff --git a/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs b/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs index 67295a7..4344877 100644 --- a/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs +++ b/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs @@ -3,6 +3,7 @@ use crate::controllers::matrix::matrix_media_controller; use crate::extractors::matrix_client_extractor::MatrixClientExtractor; use actix_web::{HttpRequest, HttpResponse, web}; use futures_util::{StreamExt, stream}; +use matrix_sdk::room::ParentSpace; use matrix_sdk::ruma::{OwnedMxcUri, OwnedRoomId, OwnedUserId}; use matrix_sdk::{Room, RoomMemberships}; @@ -12,10 +13,28 @@ pub struct APIRoomInfo { name: Option, members: Vec, avatar: Option, + is_space: bool, + parents: Vec, } impl APIRoomInfo { async fn from_room(r: &Room) -> anyhow::Result { + let parent_spaces = r + .parent_spaces() + .await? + .collect::>() + .await + .into_iter() + .collect::, _>>()? + .into_iter() + .filter_map(|d| match d { + ParentSpace::Reciprocal(r) | ParentSpace::WithPowerlevel(r) => { + Some(r.room_id().to_owned()) + } + _ => None, + }) + .collect::>(); + Ok(Self { id: r.room_id().to_owned(), name: r.name(), @@ -26,6 +45,8 @@ impl APIRoomInfo { .map(|r| r.user_id().to_owned()) .collect::>(), avatar: r.avatar_url(), + is_space: r.is_space(), + parents: parent_spaces, }) } } @@ -42,6 +63,18 @@ pub async fn joined_rooms(client: MatrixClientExtractor) -> HttpResult { Ok(HttpResponse::Ok().json(list)) } +/// Get joined spaces rooms of user +pub async fn get_joined_spaces(client: MatrixClientExtractor) -> HttpResult { + let list = stream::iter(client.client.client.joined_space_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, diff --git a/matrixgw_backend/src/main.rs b/matrixgw_backend/src/main.rs index 2a88c8c..6aa4e83 100644 --- a/matrixgw_backend/src/main.rs +++ b/matrixgw_backend/src/main.rs @@ -142,6 +142,10 @@ async fn main() -> std::io::Result<()> { "/api/matrix/room/joined", web::get().to(matrix_room_controller::joined_rooms), ) + .route( + "/api/matrix/room/joined_spaces", + web::get().to(matrix_room_controller::get_joined_spaces), + ) .route( "/api/matrix/room/{id}", web::get().to(matrix_room_controller::single_room_info),