Parse Matrix media URL for clients

This commit is contained in:
2025-02-26 21:15:07 +01:00
parent fa4665280d
commit 6adc0c1fbb
8 changed files with 29 additions and 7 deletions

View File

@ -1,7 +1,7 @@
use crate::extractors::client_auth::APIClientAuth;
use crate::server::HttpResult;
use actix_web::{web, HttpResponse};
use ruma::api::client::{ media};
use ruma::api::client::media;
use ruma::OwnedServerName;
#[derive(serde::Deserialize)]

View File

@ -1,9 +1,10 @@
use crate::extractors::client_auth::APIClientAuth;
use crate::server::HttpResult;
use crate::utils::matrix_utils::parse_mxc_url;
use actix_web::{web, HttpResponse};
use ruma::api::client::state;
use ruma::events::StateEventType;
use ruma::OwnedRoomId;
use ruma::{OwnedRoomId, OwnedServerName};
#[derive(serde::Deserialize)]
pub struct RoomIDInPath {
@ -33,6 +34,8 @@ pub async fn name(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpRes
#[derive(serde::Serialize)]
struct GetRoomAvatarResponse {
url: String,
server_name: OwnedServerName,
media_id: String,
}
/// Get room avatar
@ -46,6 +49,13 @@ pub async fn avatar(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpR
.await?;
let avatar_url = res.content.get_field("url")?.unwrap_or("").to_string();
let Some((media_id, server_name)) = parse_mxc_url(&avatar_url) else {
return Ok(HttpResponse::InternalServerError().body("Invalid Matrix resource URL"));
};
Ok(HttpResponse::Ok().json(GetRoomAvatarResponse { url: avatar_url }))
Ok(HttpResponse::Ok().json(GetRoomAvatarResponse {
url: avatar_url.to_string(),
server_name,
media_id: media_id.to_string(),
}))
}