From 10d70c6055fdddf9b05a41f1448aeb40e006e8ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Thu, 4 May 2023 12:02:24 +0200 Subject: [PATCH] client-api: Fix timeout field in media download endpoint requests According to the stabilization spec PR. --- crates/ruma-client-api/src/media.rs | 16 ++++++++++ .../ruma-client-api/src/media/get_content.rs | 21 +++++++++----- .../src/media/get_content_as_filename.rs | 29 ++++++++++++++++++- .../src/media/get_content_thumbnail.rs | 19 ++++++++---- 4 files changed, 70 insertions(+), 15 deletions(-) diff --git a/crates/ruma-client-api/src/media.rs b/crates/ruma-client-api/src/media.rs index 250560c6..1ef455cb 100644 --- a/crates/ruma-client-api/src/media.rs +++ b/crates/ruma-client-api/src/media.rs @@ -1,5 +1,8 @@ //! Endpoints for the media repository. +#[cfg(feature = "unstable-msc2246")] +use std::time::Duration; + pub mod create_content; #[cfg(feature = "unstable-msc2246")] pub mod create_content_async; @@ -10,3 +13,16 @@ pub mod get_content_as_filename; pub mod get_content_thumbnail; pub mod get_media_config; pub mod get_media_preview; + +/// The default duration that the client should be willing to wait to start receiving data. +#[cfg(feature = "unstable-msc2246")] +fn default_download_timeout() -> Duration { + Duration::from_secs(20) +} + +/// Whether the given duration is the default duration that the client should be willing to wait to +/// start receiving data. +#[cfg(feature = "unstable-msc2246")] +fn is_default_download_timeout(timeout: &Duration) -> bool { + timeout.as_secs() == 20 +} diff --git a/crates/ruma-client-api/src/media/get_content.rs b/crates/ruma-client-api/src/media/get_content.rs index 1c178e17..6f5d0a5e 100644 --- a/crates/ruma-client-api/src/media/get_content.rs +++ b/crates/ruma-client-api/src/media/get_content.rs @@ -7,9 +7,10 @@ pub mod v3 { //! //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixmediav3downloadservernamemediaid - use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE}; #[cfg(feature = "unstable-msc2246")] - use js_int::UInt; + use std::time::Duration; + + use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE}; use ruma_common::{ api::{request, response, Metadata}, metadata, IdParseError, MxcUri, OwnedServerName, @@ -48,18 +49,22 @@ pub mod v3 { )] pub allow_remote: bool, - /// How long to wait for the media to be uploaded + /// The maximum duration that the client is willing to wait to start receiving data, in the + /// case that the content has not yet been uploaded. + /// + /// The default value is 20 seconds. /// /// This uses the unstable prefix in - /// [MSC2246](https://github.com/matrix-org/matrix-spec-proposals/pull/2246) + /// [MSC2246](https://github.com/matrix-org/matrix-spec-proposals/pull/2246). #[ruma_api(query)] #[cfg(feature = "unstable-msc2246")] #[serde( - default, - skip_serializing_if = "ruma_common::serde::is_default", + with = "ruma_common::serde::duration::ms", + default = "crate::media::default_download_timeout", + skip_serializing_if = "crate::media::is_default_download_timeout", rename = "fi.mau.msc2246.max_stall_ms" )] - pub max_stall_ms: Option, + pub timeout_ms: Duration, } /// Response type for the `get_media_content` endpoint. @@ -99,7 +104,7 @@ pub mod v3 { server_name, allow_remote: true, #[cfg(feature = "unstable-msc2246")] - max_stall_ms: None, + timeout_ms: crate::media::default_download_timeout(), } } diff --git a/crates/ruma-client-api/src/media/get_content_as_filename.rs b/crates/ruma-client-api/src/media/get_content_as_filename.rs index e569b3a9..ef97ada0 100644 --- a/crates/ruma-client-api/src/media/get_content_as_filename.rs +++ b/crates/ruma-client-api/src/media/get_content_as_filename.rs @@ -7,6 +7,9 @@ pub mod v3 { //! //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixmediav3downloadservernamemediaidfilename + #[cfg(feature = "unstable-msc2246")] + use std::time::Duration; + use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE}; use ruma_common::{ api::{request, response, Metadata}, @@ -49,6 +52,23 @@ pub mod v3 { skip_serializing_if = "ruma_common::serde::is_true" )] pub allow_remote: bool, + + /// The maximum duration that the client is willing to wait to start receiving data, in the + /// case that the content has not yet been uploaded. + /// + /// The default value is 20 seconds. + /// + /// This uses the unstable prefix in + /// [MSC2246](https://github.com/matrix-org/matrix-spec-proposals/pull/2246). + #[ruma_api(query)] + #[cfg(feature = "unstable-msc2246")] + #[serde( + with = "ruma_common::serde::duration::ms", + default = "crate::media::default_download_timeout", + skip_serializing_if = "crate::media::is_default_download_timeout", + rename = "fi.mau.msc2246.max_stall_ms" + )] + pub timeout_ms: Duration, } /// Response type for the `get_media_content_as_filename` endpoint. @@ -83,7 +103,14 @@ pub mod v3 { impl Request { /// Creates a new `Request` with the given media ID, server name and filename. pub fn new(media_id: String, server_name: OwnedServerName, filename: String) -> Self { - Self { media_id, server_name, filename, allow_remote: true } + Self { + media_id, + server_name, + filename, + allow_remote: true, + #[cfg(feature = "unstable-msc2246")] + timeout_ms: crate::media::default_download_timeout(), + } } /// Creates a new `Request` with the given url and filename. diff --git a/crates/ruma-client-api/src/media/get_content_thumbnail.rs b/crates/ruma-client-api/src/media/get_content_thumbnail.rs index b1ef4c41..a5cfe2cd 100644 --- a/crates/ruma-client-api/src/media/get_content_thumbnail.rs +++ b/crates/ruma-client-api/src/media/get_content_thumbnail.rs @@ -7,6 +7,9 @@ pub mod v3 { //! //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixmediav3thumbnailservernamemediaid + #[cfg(feature = "unstable-msc2246")] + use std::time::Duration; + use http::header::CONTENT_TYPE; use js_int::UInt; use ruma_common::{ @@ -66,18 +69,22 @@ pub mod v3 { )] pub allow_remote: bool, - /// How long to wait for the media to be uploaded + /// The maximum duration that the client is willing to wait to start receiving data, in the + /// case that the content has not yet been uploaded. + /// + /// The default value is 20 seconds. /// /// This uses the unstable prefix in - /// [MSC2246](https://github.com/matrix-org/matrix-spec-proposals/pull/2246) + /// [MSC2246](https://github.com/matrix-org/matrix-spec-proposals/pull/2246). #[ruma_api(query)] #[cfg(feature = "unstable-msc2246")] #[serde( - default, - skip_serializing_if = "ruma_common::serde::is_default", + with = "ruma_common::serde::duration::ms", + default = "crate::media::default_download_timeout", + skip_serializing_if = "crate::media::is_default_download_timeout", rename = "fi.mau.msc2246.max_stall_ms" )] - pub max_stall_ms: Option, + pub timeout_ms: Duration, } /// Response type for the `get_content_thumbnail` endpoint. @@ -117,7 +124,7 @@ pub mod v3 { height, allow_remote: true, #[cfg(feature = "unstable-msc2246")] - max_stall_ms: None, + timeout_ms: crate::media::default_download_timeout(), } }