pub mod v3 {
use js_int::UInt;
use ruma_common::{
api::{request, response, Metadata},
directory::PublicRoomsChunk,
metadata, OwnedServerName,
};
const METADATA: Metadata = metadata! {
method: GET,
rate_limited: false,
authentication: None,
history: {
1.0 => "/_matrix/client/r0/publicRooms",
1.1 => "/_matrix/client/v3/publicRooms",
}
};
#[request(error = crate::Error)]
#[derive(Default)]
pub struct Request {
#[serde(skip_serializing_if = "Option::is_none")]
#[ruma_api(query)]
pub limit: Option<UInt>,
#[serde(skip_serializing_if = "Option::is_none")]
#[ruma_api(query)]
pub since: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[ruma_api(query)]
pub server: Option<OwnedServerName>,
}
#[response(error = crate::Error)]
pub struct Response {
pub chunk: Vec<PublicRoomsChunk>,
#[serde(skip_serializing_if = "Option::is_none")]
pub next_batch: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub prev_batch: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub total_room_count_estimate: Option<UInt>,
}
impl Request {
pub fn new() -> Self {
Default::default()
}
}
impl Response {
pub fn new(chunk: Vec<PublicRoomsChunk>) -> Self {
Self { chunk, next_batch: None, prev_batch: None, total_room_count_estimate: None }
}
}
#[cfg(all(test, any(feature = "client", feature = "server")))]
mod tests {
use js_int::uint;
#[cfg(feature = "client")]
#[test]
fn construct_request_from_refs() {
use ruma_common::{
api::{MatrixVersion, OutgoingRequest as _, SendAccessToken},
server_name,
};
let req = super::Request {
limit: Some(uint!(10)),
since: Some("hello".to_owned()),
server: Some(server_name!("test.tld").to_owned()),
}
.try_into_http_request::<Vec<u8>>(
"https://homeserver.tld",
SendAccessToken::IfRequired("auth_tok"),
&[MatrixVersion::V1_1],
)
.unwrap();
let uri = req.uri();
let query = uri.query().unwrap();
assert_eq!(uri.path(), "/_matrix/client/v3/publicRooms");
assert!(query.contains("since=hello"));
assert!(query.contains("limit=10"));
assert!(query.contains("server=test.tld"));
}
#[cfg(feature = "server")]
#[test]
fn construct_response_from_refs() {
use ruma_common::api::OutgoingResponse as _;
let res = super::Response {
chunk: vec![],
next_batch: Some("next_batch_token".into()),
prev_batch: Some("prev_batch_token".into()),
total_room_count_estimate: Some(uint!(10)),
}
.try_into_http_response::<Vec<u8>>()
.unwrap();
assert_eq!(
String::from_utf8_lossy(res.body()),
r#"{"chunk":[],"next_batch":"next_batch_token","prev_batch":"prev_batch_token","total_room_count_estimate":10}"#
);
}
}
}