ruma_client_api/directory/
get_public_rooms.rs1pub mod v3 {
6 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(error = crate::Error)]
29 #[derive(Default)]
30 pub struct Request {
31 #[serde(skip_serializing_if = "Option::is_none")]
33 #[ruma_api(query)]
34 pub limit: Option<UInt>,
35
36 #[serde(skip_serializing_if = "Option::is_none")]
38 #[ruma_api(query)]
39 pub since: Option<String>,
40
41 #[serde(skip_serializing_if = "Option::is_none")]
45 #[ruma_api(query)]
46 pub server: Option<OwnedServerName>,
47 }
48
49 #[response(error = crate::Error)]
51 pub struct Response {
52 pub chunk: Vec<PublicRoomsChunk>,
54
55 #[serde(skip_serializing_if = "Option::is_none")]
57 pub next_batch: Option<String>,
58
59 #[serde(skip_serializing_if = "Option::is_none")]
61 pub prev_batch: Option<String>,
62
63 #[serde(skip_serializing_if = "Option::is_none")]
65 pub total_room_count_estimate: Option<UInt>,
66 }
67
68 impl Request {
69 pub fn new() -> Self {
71 Default::default()
72 }
73 }
74
75 impl Response {
76 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}