Skip to main content

ruma_client_api/authenticated_media/
get_media_config.rs

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