Can get information about rooms
This commit is contained in:
@@ -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<String>,
|
||||
members: Vec<OwnedUserId>,
|
||||
has_avatar: bool,
|
||||
}
|
||||
|
||||
impl APIRoomInfo {
|
||||
async fn from_room(r: &Room) -> anyhow::Result<Self> {
|
||||
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::<Vec<_>>(),
|
||||
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::<Vec<_>>()
|
||||
.await
|
||||
.into_iter()
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
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<RoomIdInPath>,
|
||||
) -> 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?),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user