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