Skip to main content

ruma_client_api/media/
get_media_config.rs

1//! `GET /_matrix/media/*/config`
2//!
3//! Gets the config for the media repository.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/v1.19/client-server-api/#get_matrixmediav3config
9
10    use js_int::UInt;
11    use ruma_common::{
12        api::{auth_scheme::AccessToken, request, response},
13        metadata,
14    };
15
16    metadata! {
17        method: GET,
18        rate_limited: true,
19        authentication: AccessToken,
20        history: {
21            1.0 => "/_matrix/media/r0/config",
22            1.1 => "/_matrix/media/v3/config",
23            1.11 => deprecated,
24        }
25    }
26
27    /// Request type for the `get_media_config` endpoint.
28    #[request]
29    #[derive(Default)]
30    #[deprecated = "\
31        Since Matrix 1.11, clients should use `authenticated_media::get_media_config::v1::Request` \
32        instead if the homeserver supports it.\
33    "]
34    pub struct Request {}
35
36    /// Response type for the `get_media_config` endpoint.
37    #[response]
38    #[derive(Default)]
39    pub struct Response {
40        /// Maximum size of upload in bytes.
41        ///
42        /// If this is `None`, the server does not advertise an upload size limit, but this
43        /// doesn't mean that there is no limit.
44        #[serde(rename = "m.upload.size", skip_serializing_if = "Option::is_none")]
45        pub upload_size: Option<UInt>,
46    }
47
48    #[allow(deprecated)]
49    impl Request {
50        /// Creates an empty `Request`.
51        pub fn new() -> Self {
52            Self {}
53        }
54    }
55
56    impl Response {
57        /// Creates a new `Response` with the given maximum upload size.
58        ///
59        /// If `upload_size` is `None`, `m.upload.size` is omitted from the serialized response.
60        pub fn new(upload_size: Option<UInt>) -> Self {
61            Self { upload_size }
62        }
63    }
64}
65
66#[cfg(all(test, feature = "client"))]
67mod client_tests {
68    use js_int::uint;
69    use ruma_common::api::IncomingResponseExt as _;
70    use serde_json::json;
71
72    use super::v3::Response;
73
74    fn deserialize(body: serde_json::Value) -> Response {
75        let body = body.to_string();
76        Response::try_from_http_response(http::Response::new(body.as_bytes())).unwrap()
77    }
78
79    #[test]
80    fn deserialize_response_with_upload_size() {
81        let response = deserialize(json!({ "m.upload.size": 50_000_000 }));
82        assert_eq!(response.upload_size, Some(uint!(50_000_000)));
83    }
84
85    #[test]
86    fn deserialize_response_without_upload_size() {
87        assert_eq!(deserialize(json!({})).upload_size, None);
88        assert_eq!(deserialize(json!({ "m.upload.size": null })).upload_size, None);
89    }
90}
91
92#[cfg(all(test, feature = "server"))]
93mod server_tests {
94    use js_int::uint;
95    use ruma_common::api::OutgoingResponseExt as _;
96    use serde_json::{Value as JsonValue, json};
97
98    use super::v3::Response;
99
100    fn serialize(response: Response) -> JsonValue {
101        let response: http::Response<Vec<u8>> = response.try_into_http_response().unwrap();
102        serde_json::from_slice(response.body()).unwrap()
103    }
104
105    #[test]
106    fn serialize_response_with_upload_size() {
107        assert_eq!(
108            serialize(Response::new(Some(uint!(50_000_000)))),
109            json!({ "m.upload.size": 50_000_000 })
110        );
111    }
112
113    #[test]
114    fn serialize_response_without_upload_size() {
115        assert_eq!(serialize(Response::new(None)), json!({}));
116    }
117}