Repository: ruma/ruma
Pull request #185 (2020-08-07 18:20:00)

Add room::aliases endpoint and changelog entry
This checks off the new endpoint box in #11


Patch:
diff --git a/ruma-client-api/CHANGELOG.md b/ruma-client-api/CHANGELOG.md
index 6f3c5967..19ae6606 100644
--- a/ruma-client-api/CHANGELOG.md
+++ b/ruma-client-api/CHANGELOG.md
@@ -6,6 +6,10 @@ Breaking changes:
   `Option<UInt>` to `UInt`
 * Update strum dependency to 0.19
 
+Improvements:
+
+* Add `r0::room::aliases` (introduced in r0.6.1)
+
 # 0.10.0
 
 Bug fixes:
diff --git a/ruma-client-api/src/r0/account/add_3pid.rs b/ruma-client-api/src/r0/account/add_3pid.rs
index 413acc4e..aa279964 100644
--- a/ruma-client-api/src/r0/account/add_3pid.rs
+++ b/ruma-client-api/src/r0/account/add_3pid.rs
@@ -20,10 +20,10 @@ ruma_api! {
         pub auth: Option<AuthData>,
 
         /// Client-generated secret string used to protect this session.
-        pub client_secret: String,
+        pub client_secret: &'a str,
 
         /// The session identifier given by the identity server.
-        pub sid: String,
+        pub sid: &'a str,
     }
 
     response: {}
diff --git a/ruma-client-api/src/r0/alias/create_alias.rs b/ruma-client-api/src/r0/alias/create_alias.rs
index dba3e74b..9863c2dc 100644
--- a/ruma-client-api/src/r0/alias/create_alias.rs
+++ b/ruma-client-api/src/r0/alias/create_alias.rs
@@ -16,10 +16,10 @@ ruma_api! {
     request: {
         /// The room alias to set.
         #[ruma_api(path)]
-        pub room_alias: RoomAliasId,
+        pub room_alias: &'a RoomAliasId,
 
         /// The room ID to set.
-        pub room_id: RoomId,
+        pub room_id: &'a RoomId,
     }
 
     response: {}
diff --git a/ruma-client-api/src/r0/alias/delete_alias.rs b/ruma-client-api/src/r0/alias/delete_alias.rs
index f5c2aa26..2a275713 100644
--- a/ruma-client-api/src/r0/alias/delete_alias.rs
+++ b/ruma-client-api/src/r0/alias/delete_alias.rs
@@ -16,7 +16,7 @@ ruma_api! {
     request: {
         /// The room alias to remove.
         #[ruma_api(path)]
-        pub room_alias: RoomAliasId,
+        pub room_alias: &'a RoomAliasId,
     }
 
     response: {}
diff --git a/ruma-client-api/src/r0/alias/get_alias.rs b/ruma-client-api/src/r0/alias/get_alias.rs
index f3184dc1..f40ac7df 100644
--- a/ruma-client-api/src/r0/alias/get_alias.rs
+++ b/ruma-client-api/src/r0/alias/get_alias.rs
@@ -16,15 +16,15 @@ ruma_api! {
     request: {
         /// The room alias.
         #[ruma_api(path)]
-        pub room_alias: RoomAliasId,
+        pub room_alias: &'a RoomAliasId,
     }
 
     response: {
         /// The room ID for this room alias.
-        pub room_id: RoomId,
+        pub room_id: &'a RoomId,
 
         /// A list of servers that are aware of this room ID.
-        pub servers: Vec<String>,
+        pub servers: &'a [String],
     }
 
     error: crate::Error
diff --git a/ruma-client-api/src/r0/directory.rs b/ruma-client-api/src/r0/directory.rs
index e6ae3669..c778eeaf 100644
--- a/ruma-client-api/src/r0/directory.rs
+++ b/ruma-client-api/src/r0/directory.rs
@@ -6,33 +6,34 @@ pub mod get_room_visibility;
 pub mod set_room_visibility;
 
 use js_int::UInt;
+use ruma_api::Outgoing;
 use ruma_identifiers::{RoomAliasId, RoomId};
-use serde::{Deserialize, Serialize};
+use serde::Serialize;
 
 /// A chunk of a room list response, describing one room
-#[derive(Clone, Debug, Deserialize, Serialize)]
-pub struct PublicRoomsChunk {
+#[derive(Clone, Debug, Outgoing, Serialize)]
+pub struct PublicRoomsChunk<'a> {
     /// Aliases of the room.
     #[serde(default, skip_serializing_if = "Vec::is_empty")]
-    pub aliases: Vec<RoomAliasId>,
+    pub aliases: Vec<&'a RoomAliasId>,
 
     /// The canonical alias of the room, if any.
     #[serde(skip_serializing_if = "Option::is_none")]
-    pub canonical_alias: Option<RoomAliasId>,
+    pub canonical_alias: Option<&'a RoomAliasId>,
 
     /// The name of the room, if any.
     #[serde(skip_serializing_if = "Option::is_none")]
-    pub name: Option<String>,
+    pub name: Option<&'a str>,
 
     /// The number of members joined to the room.
     pub num_joined_members: UInt,
 
     /// The ID of the room.
-    pub room_id: RoomId,
+    pub room_id: &'a RoomId,
 
     /// The topic of the room, if any.
     #[serde(skip_serializing_if = "Option::is_none")]
-    pub topic: Option<String>,
+    pub topic: Option<&'a str>,
 
     /// Whether the room may be viewed by guest users without joining.
     pub world_readable: bool,
@@ -44,5 +45,5 @@ pub struct PublicRoomsChunk {
 
     /// The URL for the room's avatar, if one is set.
     #[serde(skip_serializing_if = "Option::is_none")]
-    pub avatar_url: Option<String>,
+    pub avatar_url: Option<&'a str>,
 }
diff --git a/ruma-client-api/src/r0/directory/get_public_rooms.rs b/ruma-client-api/src/r0/directory/get_public_rooms.rs
index 1d1b3241..242190d7 100644
--- a/ruma-client-api/src/r0/directory/get_public_rooms.rs
+++ b/ruma-client-api/src/r0/directory/get_public_rooms.rs
@@ -3,7 +3,7 @@
 use js_int::UInt;
 use ruma_api::ruma_api;
 
-use super::PublicRoomsChunk;
+use super::{IncomingPublicRoomsChunk, PublicRoomsChunk};
 
 ruma_api! {
     metadata: {
@@ -24,25 +24,25 @@ ruma_api! {
         /// Pagination token from a previous request.
         #[serde(skip_serializing_if = "Option::is_none")]
         #[ruma_api(query)]
-        pub since: Option<String>,
+        pub since: Option<&'a str>,
 
         /// The server to fetch the public room lists from.
         ///
         /// `None` means the server this request is sent to.
         #[serde(skip_serializing_if = "Option::is_none")]
         #[ruma_api(query)]
-        pub server: Option<String>,
+        pub server: Option<&'a str>,
     }
 
     response: {
         /// A paginated chunk of public rooms.
-        pub chunk: Vec<PublicRoomsChunk>,
+        pub chunk: Vec<PublicRoomsChunk<'a>>,
 
         /// A pagination token for the response.
-        pub next_batch: Option<String>,
+        pub next_batch: Option<&'a str>,
 
         /// A pagination token that allows fetching previous results.
-        pub prev_batch: Option<String>,
+        pub prev_batch: Option<&'a str>,
 
         /// An estimate on the total number of public rooms, if the server has an estimate.
         pub total_room_count_estimate: Option<UInt>,
@@ -50,3 +50,38 @@ ruma_api! {
 
     error: crate::Error
 }
+
+#[test]
+fn construct_request_from_refs() {
+    let req: http::Request<Vec<u8>> =
+        Request { limit: Some(js_int::uint!(10)), since: Some("hello"), server: Some("address") }
+            .try_into_http_request("https://homeserver.tld", Some("auth_tok"))
+            .unwrap();
+
+    let uri = req.uri();
+    let query = uri.query().unwrap();
+
+    assert_eq!(uri.path(), "/_matrix/client/r0/publicRooms");
+    assert!(query.contains("since=hello"));
+    assert!(query.contains("limit=10"));
+    assert!(query.contains("server=address"));
+}
+
+#[test]
+fn construct_response_from_refs() {
+    use std::convert::TryInto;
+
+    let res: http::Response<Vec<u8>> = Response {
+        chunk: vec![],
+        next_batch: Some("next_batch_token"),
+        prev_batch: Some("prev_batch_token"),
+        total_room_count_estimate: Some(js_int::uint!(10)),
+    }
+    .try_into()
+    .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}"#
+    );
+}
diff --git a/ruma-client-api/src/r0/directory/get_public_rooms_filtered.rs b/ruma-client-api/src/r0/directory/get_public_rooms_filtered.rs
index 9df51661..72c17e23 100644
--- a/ruma-client-api/src/r0/directory/get_public_rooms_filtered.rs
+++ b/ruma-client-api/src/r0/directory/get_public_rooms_filtered.rs
@@ -12,7 +12,7 @@ use serde::{
 
 use serde_json::Value as JsonValue;
 
-use super::PublicRoomsChunk;
+use super::{IncomingPublicRoomsChunk, PublicRoomsChunk};
 
 ruma_api! {
     metadata: {
@@ -30,7 +30,7 @@ ruma_api! {
         /// `None` means the server this request is sent to.
         #[serde(skip_serializing_if = "Option::is_none")]
         #[ruma_api(query)]
-        pub server: Option<String>,
+        pub server: Option<&'a str>,
 
         /// Limit for the number of results to return.
         #[serde(skip_serializing_if = "Option::is_none")]
@@ -38,7 +38,7 @@ ruma_api! {
 
         /// Pagination token from a previous request.
         #[serde(skip_serializing_if = "Option::is_none")]
-        pub since: Option<String>,
+        pub since: Option<&'a str>,
 
         /// Filter to apply to the results.
         #[serde(skip_serializing_if = "Option::is_none")]
@@ -51,13 +51,13 @@ ruma_api! {
 
     response: {
         /// A paginated chunk of public rooms.
-        pub chunk: Vec<PublicRoomsChunk>,
+        pub chunk: Vec<PublicRoomsChunk<'a>>,
 
         /// A pagination token for the response.
-        pub next_batch: Option<String>,
+        pub next_batch: Option<&'a str>,
 
         /// A pagination token that allows fetching previous results.
-        pub prev_batch: Option<String>,
+        pub prev_batch: Option<&'a str>,
 
         /// An estimate on the total number of public rooms, if the server has an estimate.
         pub total_room_count_estimate: Option<UInt>,
diff --git a/ruma-client-api/src/r0/room/aliases.rs b/ruma-client-api/src/r0/room/aliases.rs
new file mode 100644
index 00000000..a96edc89
--- /dev/null
+++ b/ruma-client-api/src/r0/room/aliases.rs
@@ -0,0 +1,27 @@
+//! [PUT /_matrix/client/r0/directory/room/{roomAlias}](https://matrix.org/docs/spec/client_server/r0.6.0#put-matrix-client-r0-directory-room-roomalias)
+
+use ruma_api::ruma_api;
+use ruma_identifiers::{RoomAliasId, RoomId};
+
+ruma_api! {
+    metadata: {
+        description: "Get a list of local aliases on a given room.",
+        method: PUT,
+        name: "create_alias",
+        path: "/_matrix/client/r0/directory/room/:room_id",
+        rate_limited: false,
+        requires_authentication: true,
+    }
+
+    request: {
+        /// The room ID to get aliases of.
+        #[ruma_api(path)]
+        pub room_id: &'a RoomId,
+    }
+
+    response: {
+        aliases: &'a [RoomAliasId],
+    }
+
+    error: crate::Error
+}


Test patch:
diff --git a/ruma-api/tests/ruma_api_lifetime.rs b/ruma-api/tests/ruma_api_lifetime.rs
index caba75a3..9cec8f82 100644
--- a/ruma-api/tests/ruma_api_lifetime.rs
+++ b/ruma-api/tests/ruma_api_lifetime.rs
@@ -1,5 +1,3 @@
-use ruma_identifiers::{RoomAliasId, RoomId};
-
 #[allow(unused)]
 #[derive(Copy, Clone, Debug, ruma_api::Outgoing, serde::Serialize)]
 pub struct OtherThing<'t> {
@@ -8,9 +6,10 @@ pub struct OtherThing<'t> {
 }
 
 mod empty_response {
-    use super::*;
+    use ruma_api::ruma_api;
+    use ruma_identifiers::{RoomAliasId, RoomId};
 
-    ruma_api::ruma_api! {
+    ruma_api! {
         metadata: {
             description: "Add an alias to a room.",
             method: PUT,
@@ -34,9 +33,10 @@ mod empty_response {
 }
 
 mod nested_types {
-    use super::*;
+    use ruma_api::ruma_api;
+    use ruma_identifiers::RoomAliasId;
 
-    ruma_api::ruma_api! {
+    ruma_api! {
         metadata: {
             description: "Add an alias to a room.",
             method: PUT,
@@ -59,9 +59,11 @@ mod nested_types {
 }
 
 mod full_request_response {
-    use super::*;
+    use ruma_api::ruma_api;
+
+    use super::{IncomingOtherThing, OtherThing};
 
-    ruma_api::ruma_api! {
+    ruma_api! {
         metadata: {
             description: "Does something.",
             method: POST,
@@ -91,9 +93,11 @@ mod full_request_response {
 }
 
 mod full_request_response_with_query_map {
-    use super::*;
+    use ruma_api::ruma_api;
 
-    ruma_api::ruma_api! {
+    use super::{IncomingOtherThing, OtherThing};
+
+    ruma_api! {
         metadata: {
             description: "Does something.",
             method: GET,
@@ -123,7 +127,9 @@ mod full_request_response_with_query_map {
 }
 
 mod query_fields {
-    ruma_api::ruma_api! {
+    use ruma_api::ruma_api;
+
+    ruma_api! {
         metadata: {
             description: "Get the list of rooms in this homeserver's public directory.",
             method: GET,