Can get spaces of user

This commit is contained in:
2025-11-21 18:38:12 +01:00
parent 35b53fee5c
commit d23190f9d2
2 changed files with 37 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ use crate::controllers::matrix::matrix_media_controller;
use crate::extractors::matrix_client_extractor::MatrixClientExtractor; use crate::extractors::matrix_client_extractor::MatrixClientExtractor;
use actix_web::{HttpRequest, HttpResponse, web}; use actix_web::{HttpRequest, HttpResponse, web};
use futures_util::{StreamExt, stream}; use futures_util::{StreamExt, stream};
use matrix_sdk::room::ParentSpace;
use matrix_sdk::ruma::{OwnedMxcUri, OwnedRoomId, OwnedUserId}; use matrix_sdk::ruma::{OwnedMxcUri, OwnedRoomId, OwnedUserId};
use matrix_sdk::{Room, RoomMemberships}; use matrix_sdk::{Room, RoomMemberships};
@@ -12,10 +13,28 @@ pub struct APIRoomInfo {
name: Option<String>, name: Option<String>,
members: Vec<OwnedUserId>, members: Vec<OwnedUserId>,
avatar: Option<OwnedMxcUri>, avatar: Option<OwnedMxcUri>,
is_space: bool,
parents: Vec<OwnedRoomId>,
} }
impl APIRoomInfo { impl APIRoomInfo {
async fn from_room(r: &Room) -> anyhow::Result<Self> { async fn from_room(r: &Room) -> anyhow::Result<Self> {
let parent_spaces = r
.parent_spaces()
.await?
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.filter_map(|d| match d {
ParentSpace::Reciprocal(r) | ParentSpace::WithPowerlevel(r) => {
Some(r.room_id().to_owned())
}
_ => None,
})
.collect::<Vec<_>>();
Ok(Self { Ok(Self {
id: r.room_id().to_owned(), id: r.room_id().to_owned(),
name: r.name(), name: r.name(),
@@ -26,6 +45,8 @@ impl APIRoomInfo {
.map(|r| r.user_id().to_owned()) .map(|r| r.user_id().to_owned())
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
avatar: r.avatar_url(), 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)) 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::<Vec<_>>()
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
Ok(HttpResponse::Ok().json(list))
}
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
pub struct RoomIdInPath { pub struct RoomIdInPath {
id: OwnedRoomId, id: OwnedRoomId,

View File

@@ -142,6 +142,10 @@ async fn main() -> std::io::Result<()> {
"/api/matrix/room/joined", "/api/matrix/room/joined",
web::get().to(matrix_room_controller::joined_rooms), web::get().to(matrix_room_controller::joined_rooms),
) )
.route(
"/api/matrix/room/joined_spaces",
web::get().to(matrix_room_controller::get_joined_spaces),
)
.route( .route(
"/api/matrix/room/{id}", "/api/matrix/room/{id}",
web::get().to(matrix_room_controller::single_room_info), web::get().to(matrix_room_controller::single_room_info),