Simplify route to get room information
This commit is contained in:
parent
3640f72d73
commit
d68e3eca3b
@ -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),
|
||||||
|
@ -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(
|
||||||
|
room_id,
|
||||||
|
event_type,
|
||||||
|
String::default(),
|
||||||
|
))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(res.content.get_field(field)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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(),
|
path.room_id.clone(),
|
||||||
StateEventType::RoomName,
|
StateEventType::RoomName,
|
||||||
String::default(),
|
"name",
|
||||||
))
|
)
|
||||||
.await?;
|
.await
|
||||||
|
.ok()
|
||||||
|
.flatten();
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().json(GetRoomNameResponse {
|
let room_avatar: Option<OwnedMxcUri> = get_room_info(
|
||||||
name: res.content.get_field("name")?.unwrap_or("").to_string(),
|
&auth,
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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(),
|
path.room_id.clone(),
|
||||||
StateEventType::RoomAvatar,
|
StateEventType::RoomAvatar,
|
||||||
String::default(),
|
"url",
|
||||||
))
|
)
|
||||||
.await?;
|
.await
|
||||||
|
.ok()
|
||||||
|
.flatten();
|
||||||
|
|
||||||
let avatar_url: Option<OwnedMxcUri> = res.content.get_field("url")?;
|
Ok(HttpResponse::Ok().json(GetRoomInfoResponse {
|
||||||
|
name: room_name,
|
||||||
match avatar_url {
|
avatar: room_avatar.map(ApiMxcURI),
|
||||||
None => Ok(HttpResponse::NotFound().body("No avatar found for this room.")),
|
}))
|
||||||
Some(avatar_url) => {
|
|
||||||
Ok(HttpResponse::Ok().json(GetRoomAvatarResponse(ApiMxcURI(avatar_url))))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user