ruma_client_api/directory/
get_public_rooms.rs

1//! `GET /_matrix/client/*/publicRooms`
2//!
3//! Get the list of rooms in this homeserver's public directory.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3publicrooms
9
10    use js_int::UInt;
11    use ruma_common::{
12        OwnedServerName,
13        api::{auth_scheme::NoAuthentication, request, response},
14        directory::PublicRoomsChunk,
15        metadata,
16    };
17
18    metadata! {
19        method: GET,
20        rate_limited: false,
21        authentication: NoAuthentication,
22        history: {
23            1.0 => "/_matrix/client/r0/publicRooms",
24            1.1 => "/_matrix/client/v3/publicRooms",
25        }
26    }
27
28    /// Request type for the `get_public_rooms` endpoint.
29    #[request(error = crate::Error)]
30    #[derive(Default)]
31    pub struct Request {
32        /// Limit for the number of results to return.
33        #[serde(skip_serializing_if = "Option::is_none")]
34        #[ruma_api(query)]
35        pub limit: Option<UInt>,
36
37        /// Pagination token from a previous request.
38        #[serde(skip_serializing_if = "Option::is_none")]
39        #[ruma_api(query)]
40        pub since: Option<String>,
41
42        /// The server to fetch the public room lists from.
43        ///
44        /// `None` means the server this request is sent to.
45        #[serde(skip_serializing_if = "Option::is_none")]
46        #[ruma_api(query)]
47        pub server: Option<OwnedServerName>,
48    }
49
50    /// Response type for the `get_public_rooms` endpoint.
51    #[response(error = crate::Error)]
52    pub struct Response {
53        /// A paginated chunk of public rooms.
54        pub chunk: Vec<PublicRoomsChunk>,
55
56        /// A pagination token for the response.
57        #[serde(skip_serializing_if = "Option::is_none")]
58        pub next_batch: Option<String>,
59
60        /// A pagination token that allows fetching previous results.
61        #[serde(skip_serializing_if = "Option::is_none")]
62        pub prev_batch: Option<String>,
63
64        /// An estimate on the total number of public rooms, if the server has an estimate.
65        #[serde(skip_serializing_if = "Option::is_none")]
66        pub total_room_count_estimate: Option<UInt>,
67    }
68
69    impl Request {
70        /// Creates an empty `Request`.
71        pub fn new() -> Self {
72            Default::default()
73        }
74    }
75
76    impl Response {
77        /// Creates a new `Response` with the given room list chunk.
78        pub fn new(chunk: Vec<PublicRoomsChunk>) -> Self {
79            Self { chunk, next_batch: None, prev_batch: None, total_room_count_estimate: None }
80        }
81    }
82
83    #[cfg(all(test, any(feature = "client", feature = "server")))]
84    mod tests {
85        use js_int::uint;
86
87        #[cfg(feature = "client")]
88        #[test]
89        fn construct_request_from_refs() {
90            use std::borrow::Cow;
91
92            use ruma_common::{
93                api::{
94                    MatrixVersion, OutgoingRequest as _, SupportedVersions,
95                    auth_scheme::SendAccessToken,
96                },
97                server_name,
98            };
99
100            let supported = SupportedVersions {
101                versions: [MatrixVersion::V1_1].into(),
102                features: Default::default(),
103            };
104
105            let req = super::Request {
106                limit: Some(uint!(10)),
107                since: Some("hello".to_owned()),
108                server: Some(server_name!("test.tld").to_owned()),
109            }
110            .try_into_http_request::<Vec<u8>>(
111                "https://homeserver.tld",
112                SendAccessToken::IfRequired("auth_tok"),
113                Cow::Owned(supported),
114            )
115            .unwrap();
116
117            let uri = req.uri();
118            let query = uri.query().unwrap();
119
120            assert_eq!(uri.path(), "/_matrix/client/v3/publicRooms");
121            assert!(query.contains("since=hello"));
122            assert!(query.contains("limit=10"));
123            assert!(query.contains("server=test.tld"));
124        }
125
126        #[cfg(feature = "server")]
127        #[test]
128        fn construct_response_from_refs() {
129            use ruma_common::api::OutgoingResponse as _;
130
131            let res = super::Response {
132                chunk: vec![],
133                next_batch: Some("next_batch_token".into()),
134                prev_batch: Some("prev_batch_token".into()),
135                total_room_count_estimate: Some(uint!(10)),
136            }
137            .try_into_http_response::<Vec<u8>>()
138            .unwrap();
139
140            assert_eq!(
141                String::from_utf8_lossy(res.body()),
142                r#"{"chunk":[],"next_batch":"next_batch_token","prev_batch":"prev_batch_token","total_room_count_estimate":10}"#
143            );
144        }
145    }
146}