Skip to content

Commit

Permalink
client-api: Fix timeout field in media download endpoint requests
Browse files Browse the repository at this point in the history
According to the stabilization spec PR.
  • Loading branch information
zecakeh committed May 4, 2023
1 parent c917123 commit 7af3e00
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 15 deletions.
16 changes: 16 additions & 0 deletions crates/ruma-client-api/src/media.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
}
21 changes: 13 additions & 8 deletions crates/ruma-client-api/src/media/get_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<UInt>,
pub timeout_ms: Duration,
}

/// Response type for the `get_media_content` endpoint.
Expand Down Expand Up @@ -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(),
}
}

Expand Down
29 changes: 28 additions & 1 deletion crates/ruma-client-api/src/media/get_content_as_filename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 13 additions & 6 deletions crates/ruma-client-api/src/media/get_content_thumbnail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<UInt>,
pub timeout_ms: Duration,
}

/// Response type for the `get_content_thumbnail` endpoint.
Expand Down Expand Up @@ -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(),
}
}

Expand Down

0 comments on commit 7af3e00

Please sign in to comment.