client-api: Move some media types and helpers to ruma-common

Without breaking changes.
This commit is contained in:
Kévin Commaille 2024-06-17 19:46:09 +02:00 committed by Kévin Commaille
parent f9ae582b27
commit 586668806e
9 changed files with 61 additions and 53 deletions

View File

@ -44,8 +44,8 @@ pub mod v1 {
#[ruma_api(query)] #[ruma_api(query)]
#[serde( #[serde(
with = "ruma_common::serde::duration::ms", with = "ruma_common::serde::duration::ms",
default = "crate::media::default_download_timeout", default = "ruma_common::media::default_download_timeout",
skip_serializing_if = "crate::media::is_default_download_timeout" skip_serializing_if = "ruma_common::media::is_default_download_timeout"
)] )]
pub timeout_ms: Duration, pub timeout_ms: Duration,
} }
@ -70,7 +70,11 @@ pub mod v1 {
impl Request { impl Request {
/// Creates a new `Request` with the given media ID and server name. /// Creates a new `Request` with the given media ID and server name.
pub fn new(media_id: String, server_name: OwnedServerName) -> Self { pub fn new(media_id: String, server_name: OwnedServerName) -> Self {
Self { media_id, server_name, timeout_ms: crate::media::default_download_timeout() } Self {
media_id,
server_name,
timeout_ms: ruma_common::media::default_download_timeout(),
}
} }
/// Creates a new `Request` with the given URI. /// Creates a new `Request` with the given URI.

View File

@ -48,8 +48,8 @@ pub mod v1 {
#[ruma_api(query)] #[ruma_api(query)]
#[serde( #[serde(
with = "ruma_common::serde::duration::ms", with = "ruma_common::serde::duration::ms",
default = "crate::media::default_download_timeout", default = "ruma_common::media::default_download_timeout",
skip_serializing_if = "crate::media::is_default_download_timeout" skip_serializing_if = "ruma_common::media::is_default_download_timeout"
)] )]
pub timeout_ms: Duration, pub timeout_ms: Duration,
} }
@ -78,7 +78,7 @@ pub mod v1 {
media_id, media_id,
server_name, server_name,
filename, filename,
timeout_ms: crate::media::default_download_timeout(), timeout_ms: ruma_common::media::default_download_timeout(),
} }
} }

View File

@ -13,11 +13,10 @@ pub mod v1 {
use js_int::UInt; use js_int::UInt;
use ruma_common::{ use ruma_common::{
api::{request, response, Metadata}, api::{request, response, Metadata},
media::Method,
metadata, IdParseError, MxcUri, OwnedServerName, metadata, IdParseError, MxcUri, OwnedServerName,
}; };
use crate::media::get_content_thumbnail::v3::Method;
const METADATA: Metadata = metadata! { const METADATA: Metadata = metadata! {
method: GET, method: GET,
rate_limited: true, rate_limited: true,
@ -63,8 +62,8 @@ pub mod v1 {
#[ruma_api(query)] #[ruma_api(query)]
#[serde( #[serde(
with = "ruma_common::serde::duration::ms", with = "ruma_common::serde::duration::ms",
default = "crate::media::default_download_timeout", default = "ruma_common::media::default_download_timeout",
skip_serializing_if = "crate::media::is_default_download_timeout" skip_serializing_if = "ruma_common::media::is_default_download_timeout"
)] )]
pub timeout_ms: Duration, pub timeout_ms: Duration,
@ -105,7 +104,7 @@ pub mod v1 {
method: None, method: None,
width, width,
height, height,
timeout_ms: crate::media::default_download_timeout(), timeout_ms: ruma_common::media::default_download_timeout(),
animated: None, animated: None,
} }
} }

View File

@ -1,7 +1,5 @@
//! Endpoints for the media repository. //! Endpoints for the media repository.
use std::time::Duration;
pub mod create_content; pub mod create_content;
pub mod create_content_async; pub mod create_content_async;
pub mod create_mxc_uri; pub mod create_mxc_uri;
@ -10,14 +8,3 @@ pub mod get_content_as_filename;
pub mod get_content_thumbnail; pub mod get_content_thumbnail;
pub mod get_media_config; pub mod get_media_config;
pub mod get_media_preview; pub mod get_media_preview;
/// The default duration that the client should be willing to wait to start receiving data.
pub(crate) 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.
pub(crate) fn is_default_download_timeout(timeout: &Duration) -> bool {
timeout.as_secs() == 20
}

View File

@ -61,8 +61,8 @@ pub mod v3 {
#[ruma_api(query)] #[ruma_api(query)]
#[serde( #[serde(
with = "ruma_common::serde::duration::ms", with = "ruma_common::serde::duration::ms",
default = "crate::media::default_download_timeout", default = "ruma_common::media::default_download_timeout",
skip_serializing_if = "crate::media::is_default_download_timeout" skip_serializing_if = "ruma_common::media::is_default_download_timeout"
)] )]
pub timeout_ms: Duration, pub timeout_ms: Duration,
@ -108,7 +108,7 @@ pub mod v3 {
media_id, media_id,
server_name, server_name,
allow_remote: true, allow_remote: true,
timeout_ms: crate::media::default_download_timeout(), timeout_ms: ruma_common::media::default_download_timeout(),
allow_redirect: false, allow_redirect: false,
} }
} }

View File

@ -65,8 +65,8 @@ pub mod v3 {
#[ruma_api(query)] #[ruma_api(query)]
#[serde( #[serde(
with = "ruma_common::serde::duration::ms", with = "ruma_common::serde::duration::ms",
default = "crate::media::default_download_timeout", default = "ruma_common::media::default_download_timeout",
skip_serializing_if = "crate::media::is_default_download_timeout" skip_serializing_if = "ruma_common::media::is_default_download_timeout"
)] )]
pub timeout_ms: Duration, pub timeout_ms: Duration,
@ -113,7 +113,7 @@ pub mod v3 {
server_name, server_name,
filename, filename,
allow_remote: true, allow_remote: true,
timeout_ms: crate::media::default_download_timeout(), timeout_ms: ruma_common::media::default_download_timeout(),
allow_redirect: false, allow_redirect: false,
} }
} }

View File

@ -11,14 +11,13 @@ pub mod v3 {
use http::header::CONTENT_TYPE; use http::header::CONTENT_TYPE;
use js_int::UInt; use js_int::UInt;
pub use ruma_common::media::Method;
use ruma_common::{ use ruma_common::{
api::{request, response, Metadata}, api::{request, response, Metadata},
metadata, metadata, IdParseError, MxcUri, OwnedServerName,
serde::StringEnum,
IdParseError, MxcUri, OwnedServerName,
}; };
use crate::{http_headers::CROSS_ORIGIN_RESOURCE_POLICY, PrivOwnedStr}; use crate::http_headers::CROSS_ORIGIN_RESOURCE_POLICY;
const METADATA: Metadata = metadata! { const METADATA: Metadata = metadata! {
method: GET, method: GET,
@ -80,8 +79,8 @@ pub mod v3 {
#[ruma_api(query)] #[ruma_api(query)]
#[serde( #[serde(
with = "ruma_common::serde::duration::ms", with = "ruma_common::serde::duration::ms",
default = "crate::media::default_download_timeout", default = "ruma_common::media::default_download_timeout",
skip_serializing_if = "crate::media::is_default_download_timeout" skip_serializing_if = "ruma_common::media::is_default_download_timeout"
)] )]
pub timeout_ms: Duration, pub timeout_ms: Duration,
@ -140,7 +139,7 @@ pub mod v3 {
width, width,
height, height,
allow_remote: true, allow_remote: true,
timeout_ms: crate::media::default_download_timeout(), timeout_ms: ruma_common::media::default_download_timeout(),
allow_redirect: false, allow_redirect: false,
animated: None, animated: None,
} }
@ -167,20 +166,4 @@ pub mod v3 {
} }
} }
} }
/// The desired resizing method.
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, StringEnum)]
#[ruma_enum(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Method {
/// Crop the original to produce the requested image dimensions.
Crop,
/// Maintain the original aspect ratio of the source image.
Scale,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
} }

View File

@ -26,6 +26,7 @@ pub mod directory;
pub mod encryption; pub mod encryption;
pub mod http_headers; pub mod http_headers;
mod identifiers; mod identifiers;
pub mod media;
mod percent_encode; mod percent_encode;
pub mod power_levels; pub mod power_levels;
pub mod presence; pub mod presence;

View File

@ -0,0 +1,34 @@
//! Common types and functions for the [content repository].
//!
//! [content repository]: https://spec.matrix.org/latest/client-server-api/#content-repository
use std::time::Duration;
use crate::{serde::StringEnum, PrivOwnedStr};
/// The desired resizing method for a thumbnail.
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, StringEnum)]
#[ruma_enum(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Method {
/// Crop the original to produce the requested image dimensions.
Crop,
/// Maintain the original aspect ratio of the source image.
Scale,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
/// The default duration that the client should be willing to wait to start receiving data.
pub 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.
pub fn is_default_download_timeout(timeout: &Duration) -> bool {
timeout.as_secs() == 20
}