ruma_client_api/directory/
get_public_rooms.rs1pub mod v3 {
6 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(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 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}