diff --git a/ruma-client-api/src/r0/media/create_content.rs b/ruma-client-api/src/r0/media/create_content.rs index 25bc4044..261f04ef 100644 --- a/ruma-client-api/src/r0/media/create_content.rs +++ b/ruma-client-api/src/r0/media/create_content.rs @@ -12,22 +12,24 @@ ruma_api! { requires_authentication: true, } + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] request: { /// The name of the file being uploaded. #[ruma_api(query)] #[serde(skip_serializing_if = "Option::is_none")] - pub filename: Option, + pub filename: Option<&'a str>, /// The content type of the file being uploaded. // TODO: This should be optional. #[ruma_api(header = CONTENT_TYPE)] - pub content_type: String, + pub content_type: &'a str, /// The file contents to upload. #[ruma_api(raw_body)] pub file: Vec, } + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] response: { /// The MXC URI for the uploaded content. pub content_uri: String, @@ -35,3 +37,17 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given content type and file contents. + pub fn new(content_type: &'a str, file: Vec) -> Self { + Self { filename: None, content_type, file } + } +} + +impl Response { + /// Creates a new `Response` with the given MXC URI. + pub fn new(content_uri: String) -> Self { + Self { content_uri } + } +} diff --git a/ruma-client-api/src/r0/media/get_content.rs b/ruma-client-api/src/r0/media/get_content.rs index e85d96b1..0ec64a6d 100644 --- a/ruma-client-api/src/r0/media/get_content.rs +++ b/ruma-client-api/src/r0/media/get_content.rs @@ -13,6 +13,7 @@ ruma_api! { requires_authentication: false, } + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] request: { /// The media ID from the mxc:// URI (the path component). #[ruma_api(path)] @@ -23,11 +24,14 @@ ruma_api! { pub server_name: &'a ServerName, /// Whether to fetch media deemed remote. + /// /// Used to prevent routing loops. Defaults to `true`. #[ruma_api(query)] - pub allow_remote: Option, + #[serde(default = "ruma_serde::default_true", skip_serializing_if = "ruma_serde::is_true")] + pub allow_remote: bool, } + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] response: { /// The content that was previously uploaded. #[ruma_api(raw_body)] @@ -44,3 +48,17 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given media ID and server name. + pub fn new(media_id: &'a str, server_name: &'a ServerName) -> Self { + Self { media_id, server_name, allow_remote: true } + } +} + +impl Response { + /// Creates a new `Response` with the given file contents, content type and filename. + pub fn new(file: Vec, content_type: String, content_disposition: String) -> Self { + Self { file, content_type, content_disposition } + } +} diff --git a/ruma-client-api/src/r0/media/get_content_as_filename.rs b/ruma-client-api/src/r0/media/get_content_as_filename.rs index 1981c3f3..27a34ec8 100644 --- a/ruma-client-api/src/r0/media/get_content_as_filename.rs +++ b/ruma-client-api/src/r0/media/get_content_as_filename.rs @@ -13,6 +13,7 @@ ruma_api! { requires_authentication: false, } + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] request: { /// The media ID from the mxc:// URI (the path component). #[ruma_api(path)] @@ -27,11 +28,14 @@ ruma_api! { pub filename: &'a str, /// Whether to fetch media deemed remote. + /// /// Used to prevent routing loops. Defaults to `true`. #[ruma_api(query)] - pub allow_remote: Option, + #[serde(default = "ruma_serde::default_true", skip_serializing_if = "ruma_serde::is_true")] + pub allow_remote: bool, } + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] response: { /// The content that was previously uploaded. #[ruma_api(raw_body)] @@ -48,3 +52,17 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given media ID, server name and filename. + pub fn new(media_id: &'a str, server_name: &'a ServerName, filename: &'a str) -> Self { + Self { media_id, server_name, filename, allow_remote: true } + } +} + +impl Response { + /// Creates a new `Response` with the given file, content type and filename. + pub fn new(file: Vec, content_type: String, content_disposition: String) -> Self { + Self { file, content_type, content_disposition } + } +} diff --git a/ruma-client-api/src/r0/media/get_content_thumbnail.rs b/ruma-client-api/src/r0/media/get_content_thumbnail.rs index 331d7bd9..5fa4ef5e 100644 --- a/ruma-client-api/src/r0/media/get_content_thumbnail.rs +++ b/ruma-client-api/src/r0/media/get_content_thumbnail.rs @@ -26,14 +26,8 @@ ruma_api! { requires_authentication: false, } + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] request: { - /// Whether to fetch media deemed remote. - /// - /// Used to prevent routing loops. Defaults to `true`. - #[ruma_api(query)] - #[serde(skip_serializing_if = "Option::is_none")] - pub allow_remote: Option, - /// The media ID from the mxc:// URI (the path component). #[ruma_api(path)] pub media_id: &'a str, @@ -42,11 +36,6 @@ ruma_api! { #[ruma_api(path)] pub server_name: &'a ServerName, - /// The *desired* height of the thumbnail. The actual thumbnail may not match the size - /// specified. - #[ruma_api(query)] - pub height: UInt, - /// The desired resizing method. #[ruma_api(query)] #[serde(skip_serializing_if = "Option::is_none")] @@ -56,8 +45,21 @@ ruma_api! { /// specified. #[ruma_api(query)] pub width: UInt, + + /// The *desired* height of the thumbnail. The actual thumbnail may not match the size + /// specified. + #[ruma_api(query)] + pub height: UInt, + + /// Whether to fetch media deemed remote. + /// + /// Used to prevent routing loops. Defaults to `true`. + #[ruma_api(query)] + #[serde(default = "ruma_serde::default_true", skip_serializing_if = "ruma_serde::is_true")] + pub allow_remote: bool, } + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] response: { /// The content type of the thumbnail. #[ruma_api(header = CONTENT_TYPE)] @@ -70,3 +72,18 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given media ID, server name, desired thumbnail width and + /// desired thumbnail height. + pub fn new(media_id: &'a str, server_name: &'a ServerName, width: UInt, height: UInt) -> Self { + Self { media_id, server_name, method: None, width, height, allow_remote: true } + } +} + +impl Response { + /// Creates a new `Response` with the given content type and thumbnail. + pub fn new(content_type: String, file: Vec) -> Self { + Self { content_type, file } + } +} diff --git a/ruma-client-api/src/r0/media/get_media_config.rs b/ruma-client-api/src/r0/media/get_media_config.rs index 963a0d2e..7079be45 100644 --- a/ruma-client-api/src/r0/media/get_media_config.rs +++ b/ruma-client-api/src/r0/media/get_media_config.rs @@ -13,8 +13,11 @@ ruma_api! { requires_authentication: true, } + #[derive(Default)] + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] request: {} + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] response: { /// Maximum size of upload in bytes. #[serde(rename = "m.upload.size")] @@ -23,3 +26,17 @@ ruma_api! { error: crate::Error } + +impl Request { + /// Creates an empty `Request`. + pub fn new() -> Self { + Self + } +} + +impl Response { + /// Creates a new `Response` with the given maximum upload size. + pub fn new(upload_size: UInt) -> Self { + Self { upload_size } + } +} diff --git a/ruma-client-api/src/r0/media/get_media_preview.rs b/ruma-client-api/src/r0/media/get_media_preview.rs index ba500513..2b15ca25 100644 --- a/ruma-client-api/src/r0/media/get_media_preview.rs +++ b/ruma-client-api/src/r0/media/get_media_preview.rs @@ -3,7 +3,8 @@ use std::time::SystemTime; use ruma_api::ruma_api; -use serde_json::value::RawValue as RawJsonValue; +use serde::Serialize; +use serde_json::value::{to_raw_value as to_raw_json_value, RawValue as RawJsonValue}; ruma_api! { metadata: { @@ -15,10 +16,11 @@ ruma_api! { requires_authentication: true, } + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] request: { /// URL to get a preview of. #[ruma_api(query)] - pub url: String, + pub url: &'a str, /// Preferred point in time (in milliseconds) to return a preview for. #[ruma_api(query)] @@ -26,6 +28,8 @@ ruma_api! { pub ts: SystemTime, } + #[derive(Default)] + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] response: { /// OpenGraph-like data for the URL. /// @@ -38,6 +42,30 @@ ruma_api! { error: crate::Error } +impl<'a> Request<'a> { + /// Creates a new `Request` with the given url and timestamp. + pub fn new(url: &'a str, ts: SystemTime) -> Self { + Self { url, ts } + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self { data: None } + } + + /// Creates a new `Response` with the given OpenGraph data (in a `serde_json::value::RawValue`). + pub fn from_raw_value(data: Box) -> Self { + Self { data: Some(data) } + } + + /// Creates a new `Response` with the given OpenGraph data (in any kind of serializable object). + pub fn from_serialize(data: &T) -> serde_json::Result { + Ok(Self { data: Some(to_raw_json_value(data)?) }) + } +} + #[cfg(test)] mod tests { use serde_json::{