From 8d8467a4eafd264adb9c710e0638c08ae547dec4 Mon Sep 17 00:00:00 2001 From: strawberry Date: Fri, 15 Mar 2024 22:40:12 -0400 Subject: [PATCH] add legacy v1 routes for the remaining media endpoints Signed-off-by: strawberry --- src/api/client_server/media.rs | 60 ++++++++++++++++++++++++++++++++++ src/database/mod.rs | 2 +- src/main.rs | 8 +++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/api/client_server/media.rs b/src/api/client_server/media.rs index e8fe0b69..380d4e3c 100644 --- a/src/api/client_server/media.rs +++ b/src/api/client_server/media.rs @@ -31,6 +31,22 @@ pub async fn get_media_config_route( }) } +/// # `GET /_matrix/media/v1/config` +/// +/// This is a legacy endpoint ("/v1/") that some very old homeservers and/or +/// clients may call. conduwuit adds these for compatibility purposes. +/// See +/// +/// Returns max upload size. +pub async fn get_media_config_v1_route( + _body: Ruma, +) -> Result> { + Ok(get_media_config::v3::Response { + upload_size: services().globals.max_request_size().into(), + } + .into()) +} + /// # `GET /_matrix/media/v3/preview_url` /// /// Returns URL preview. @@ -71,6 +87,50 @@ pub async fn get_media_preview_route( } } +/// # `GET /_matrix/media/v1/preview_url` +/// +/// This is a legacy endpoint ("/v1/") that some very old homeservers and/or +/// clients may call. conduwuit adds these for compatibility purposes. +/// See +/// +/// Returns URL preview. +pub async fn get_media_preview_v1_route( + body: Ruma, +) -> Result> { + let url = &body.url; + if !url_preview_allowed(url) { + return Err(Error::BadRequest(ErrorKind::Forbidden, "URL is not allowed to be previewed")); + } + + match get_url_preview(url).await { + Ok(preview) => { + let res = serde_json::value::to_raw_value(&preview).map_err(|e| { + error!("Failed to convert UrlPreviewData into a serde json value: {}", e); + Error::BadRequest( + ErrorKind::LimitExceeded { + retry_after_ms: Some(Duration::from_secs(5)), + }, + "Failed to generate a URL preview, try again later.", + ) + })?; + + Ok(get_media_preview::v3::Response::from_raw_value(res).into()) + }, + Err(e) => { + warn!("Failed to generate a URL preview: {e}"); + + // there doesn't seem to be an agreed-upon error code in the spec. + // the only response codes in the preview_url spec page are 200 and 429. + Err(Error::BadRequest( + ErrorKind::LimitExceeded { + retry_after_ms: Some(Duration::from_secs(5)), + }, + "Failed to generate a URL preview, try again later.", + )) + }, + } +} + /// # `POST /_matrix/media/v3/upload` /// /// Permanently save media in the server. diff --git a/src/database/mod.rs b/src/database/mod.rs index 22fd129e..994b9273 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1112,7 +1112,7 @@ impl KeyValueDatabase { _ = terminate.recv() => { debug!(target: "database-cleanup","Received SIGTERM"); } - }; + } #[cfg(not(unix))] { diff --git a/src/main.rs b/src/main.rs index 97dd6eb4..ed163519 100644 --- a/src/main.rs +++ b/src/main.rs @@ -668,6 +668,14 @@ fn routes() -> Router { .ruma_route(client_server::get_media_preview_route) .ruma_route(client_server::create_content_route) // legacy v1 media routes + .route( + "/_matrix/media/v1/url_preview", + get(client_server::get_media_preview_v1_route) + ) + .route( + "/_matrix/media/v1/config", + get(client_server::get_media_config_v1_route) + ) .route( "/_matrix/media/v1/upload", post(client_server::create_content_v1_route)