client-api: Update media endpoints to the new API standards

This commit is contained in:
Jonas Platte 2020-09-03 17:31:50 +02:00
parent 7ccdaa34d6
commit 53162321c9
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
6 changed files with 132 additions and 18 deletions

View File

@ -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<String>,
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<u8>,
}
#[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<u8>) -> 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 }
}
}

View File

@ -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<bool>,
#[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<u8>, content_type: String, content_disposition: String) -> Self {
Self { file, content_type, content_disposition }
}
}

View File

@ -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<bool>,
#[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<u8>, content_type: String, content_disposition: String) -> Self {
Self { file, content_type, content_disposition }
}
}

View File

@ -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<bool>,
/// 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<u8>) -> Self {
Self { content_type, file }
}
}

View File

@ -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 }
}
}

View File

@ -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<RawJsonValue>) -> Self {
Self { data: Some(data) }
}
/// Creates a new `Response` with the given OpenGraph data (in any kind of serializable object).
pub fn from_serialize<T: Serialize>(data: &T) -> serde_json::Result<Self> {
Ok(Self { data: Some(to_raw_json_value(data)?) })
}
}
#[cfg(test)]
mod tests {
use serde_json::{