Simplify route to get room information

This commit is contained in:
Pierre HUBERT 2025-02-27 20:18:18 +01:00
parent 3640f72d73
commit d68e3eca3b
2 changed files with 43 additions and 37 deletions

View File

@ -57,11 +57,7 @@ async fn main() -> std::io::Result<()> {
.route("/api", web::get().to(api::api_home)) .route("/api", web::get().to(api::api_home))
.route("/api", web::post().to(api::api_home)) .route("/api", web::post().to(api::api_home))
.route("/api/account/whoami", web::get().to(api::account::who_am_i)) .route("/api/account/whoami", web::get().to(api::account::who_am_i))
.route("/api/room/{room_id}/name", web::get().to(api::room::name)) .route("/api/room/{room_id}", web::get().to(api::room::info))
.route(
"/api/room/{room_id}/avatar",
web::get().to(api::room::avatar),
)
.route( .route(
"/api/media/{server_name}/{media_id}/download", "/api/media/{server_name}/{media_id}/download",
web::get().to(api::media::download), web::get().to(api::media::download),

View File

@ -1,10 +1,11 @@
use crate::extractors::client_auth::APIClientAuth; use crate::extractors::client_auth::APIClientAuth;
use crate::server::HttpResult; use crate::server::{HttpFailure, HttpResult};
use crate::utils::matrix_utils::ApiMxcURI; use crate::utils::matrix_utils::ApiMxcURI;
use actix_web::{web, HttpResponse}; use actix_web::{web, HttpResponse};
use ruma::api::client::state; use ruma::api::client::state;
use ruma::events::StateEventType; use ruma::events::StateEventType;
use ruma::{OwnedMxcUri, OwnedRoomId}; use ruma::{OwnedMxcUri, OwnedRoomId};
use serde::de::DeserializeOwned;
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
pub struct RoomIDInPath { pub struct RoomIDInPath {
@ -12,44 +13,53 @@ pub struct RoomIDInPath {
} }
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
struct GetRoomNameResponse { struct GetRoomInfoResponse {
name: String, name: Option<String>,
avatar: Option<ApiMxcURI>,
} }
/// Get room name /// Get a room information
pub async fn name(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpResult { async fn get_room_info<E: DeserializeOwned>(
auth: &APIClientAuth,
room_id: OwnedRoomId,
event_type: StateEventType,
field: &str,
) -> anyhow::Result<Option<E>, HttpFailure> {
let res = auth let res = auth
.send_request(state::get_state_events_for_key::v3::Request::new( .send_request(state::get_state_events_for_key::v3::Request::new(
path.room_id.clone(), room_id,
StateEventType::RoomName, event_type,
String::default(), String::default(),
)) ))
.await?; .await?;
Ok(HttpResponse::Ok().json(GetRoomNameResponse { Ok(res.content.get_field(field)?)
name: res.content.get_field("name")?.unwrap_or("").to_string(), }
/// Get room information
pub async fn info(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpResult {
let room_name: Option<String> = get_room_info(
&auth,
path.room_id.clone(),
StateEventType::RoomName,
"name",
)
.await
.ok()
.flatten();
let room_avatar: Option<OwnedMxcUri> = get_room_info(
&auth,
path.room_id.clone(),
StateEventType::RoomAvatar,
"url",
)
.await
.ok()
.flatten();
Ok(HttpResponse::Ok().json(GetRoomInfoResponse {
name: room_name,
avatar: room_avatar.map(ApiMxcURI),
})) }))
} }
#[derive(serde::Serialize)]
struct GetRoomAvatarResponse(ApiMxcURI);
/// Get room avatar
pub async fn avatar(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpResult {
let res = auth
.send_request(state::get_state_events_for_key::v3::Request::new(
path.room_id.clone(),
StateEventType::RoomAvatar,
String::default(),
))
.await?;
let avatar_url: Option<OwnedMxcUri> = res.content.get_field("url")?;
match avatar_url {
None => Ok(HttpResponse::NotFound().body("No avatar found for this room.")),
Some(avatar_url) => {
Ok(HttpResponse::Ok().json(GetRoomAvatarResponse(ApiMxcURI(avatar_url))))
}
}
}