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::{request, response, Metadata},
13        directory::PublicRoomsChunk,
14        metadata, OwnedServerName,
15    };
16
17    const METADATA: Metadata = metadata! {
18        method: GET,
19        rate_limited: false,
20        authentication: None,
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 ruma_common::{
90                api::{MatrixVersion, OutgoingRequest as _, SendAccessToken, SupportedVersions},
91                server_name,
92            };
93
94            let supported = SupportedVersions {
95                versions: [MatrixVersion::V1_1].into(),
96                features: Default::default(),
97            };
98
99            let req = super::Request {
100                limit: Some(uint!(10)),
101                since: Some("hello".to_owned()),
102                server: Some(server_name!("test.tld").to_owned()),
103            }
104            .try_into_http_request::<Vec<u8>>(
105                "https://homeserver.tld",
106                SendAccessToken::IfRequired("auth_tok"),
107                &supported,
108            )
109            .unwrap();
110
111            let uri = req.uri();
112            let query = uri.query().unwrap();
113
114            assert_eq!(uri.path(), "/_matrix/client/v3/publicRooms");
115            assert!(query.contains("since=hello"));
116            assert!(query.contains("limit=10"));
117            assert!(query.contains("server=test.tld"));
118        }
119
120        #[cfg(feature = "server")]
121        #[test]
122        fn construct_response_from_refs() {
123            use ruma_common::api::OutgoingResponse as _;
124
125            let res = super::Response {
126                chunk: vec![],
127                next_batch: Some("next_batch_token".into()),
128                prev_batch: Some("prev_batch_token".into()),
129                total_room_count_estimate: Some(uint!(10)),
130            }
131            .try_into_http_response::<Vec<u8>>()
132            .unwrap();
133
134            assert_eq!(
135                String::from_utf8_lossy(res.body()),
136                r#"{"chunk":[],"next_batch":"next_batch_token","prev_batch":"prev_batch_token","total_room_count_estimate":10}"#
137            );
138        }
139    }
140}