From 135bea856249dfa8166106a7cd47a78ea6f4f325 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 20 Aug 2020 02:13:57 +0200 Subject: [PATCH] federation-api: More small improvements --- .../discovery/get_remote_server_keys/v2.rs | 28 +++++++++++++---- .../get_remote_server_keys_batch/v2.rs | 19 ++++++++++++ .../create_join_event_template/v1.rs | 31 +++++++++++++++++-- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/ruma-federation-api/src/discovery/get_remote_server_keys/v2.rs b/ruma-federation-api/src/discovery/get_remote_server_keys/v2.rs index 3d11336b..a4cbe0ef 100644 --- a/ruma-federation-api/src/discovery/get_remote_server_keys/v2.rs +++ b/ruma-federation-api/src/discovery/get_remote_server_keys/v2.rs @@ -11,29 +11,45 @@ ruma_api! { description: "Query for another server's keys.", method: GET, name: "get_remote_server_keys", + // Note: The spec has an additional, deprecated path parameter on this. We may want to + // support an additional parameter at the end, even if it is ignored. path: "/_matrix/key/v2/query/:server_name", rate_limited: false, requires_authentication: false, } + #[non_exhaustive] request: { /// The server's DNS name to query #[ruma_api(path)] pub server_name: &'a ServerName, - /// A millisecond POSIX timestamp in milliseconds indicating when the - /// returned certificates will need to be valid until to be useful to - /// the requesting server. + /// A millisecond POSIX timestamp in milliseconds indicating when the returned certificates + /// will need to be valid until to be useful to the requesting server. /// - /// If not supplied, the current time as determined by the notary server - /// is used. + /// If not supplied, the current time as determined by the receiving server is used. #[ruma_api(query)] - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] + #[serde(default = "SystemTime::now", with = "ruma_serde::time::ms_since_unix_epoch")] pub minimum_valid_until_ts: SystemTime, } + #[non_exhaustive] response: { /// The queried server's keys, signed by the notary server. pub server_keys: Vec, } } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given server name and `minimum_valid_until` timestamp. + pub fn new(server_name: &'a ServerName, minimum_valid_until_ts: SystemTime) -> Self { + Self { server_name, minimum_valid_until_ts } + } +} + +impl Response { + /// Creates a new `Response` with the given keys. + pub fn new(server_keys: Vec) -> Self { + Self { server_keys } + } +} diff --git a/ruma-federation-api/src/discovery/get_remote_server_keys_batch/v2.rs b/ruma-federation-api/src/discovery/get_remote_server_keys_batch/v2.rs index 4da95082..983b63e4 100644 --- a/ruma-federation-api/src/discovery/get_remote_server_keys_batch/v2.rs +++ b/ruma-federation-api/src/discovery/get_remote_server_keys_batch/v2.rs @@ -17,6 +17,7 @@ ruma_api! { requires_authentication: false, } + #[non_exhaustive] request: { /// The query criteria. The outer string key on the object is the server /// name (eg: matrix.org). The inner string key is the Key ID to query @@ -41,12 +42,30 @@ ruma_api! { pub minimum_valid_until_ts: SystemTime, } + #[non_exhaustive] response: { /// The queried server's keys, signed by the notary server. pub server_keys: Vec, } } +impl Request { + /// Creates a new `Request` with the given query criteria and `minimum_valid_until` timestamp. + pub fn new( + server_keys: BTreeMap>, + minimum_valid_until_ts: SystemTime, + ) -> Self { + Self { server_keys, minimum_valid_until_ts } + } +} + +impl Response { + /// Creates a new `Response` with the given keys. + pub fn new(server_keys: Vec) -> Self { + Self { server_keys } + } +} + /// The query criteria. #[derive(Clone, Debug, Default, Deserialize, Serialize)] #[non_exhaustive] diff --git a/ruma-federation-api/src/membership/create_join_event_template/v1.rs b/ruma-federation-api/src/membership/create_join_event_template/v1.rs index 639e5668..4fb2353a 100644 --- a/ruma-federation-api/src/membership/create_join_event_template/v1.rs +++ b/ruma-federation-api/src/membership/create_join_event_template/v1.rs @@ -15,6 +15,7 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The room ID that is about to be joined. #[ruma_api(path)] @@ -24,17 +25,43 @@ ruma_api! { #[ruma_api(path)] pub user_id: &'a UserId, - /// The room versions the sending server has support for. Defaults to 1. + /// The room versions the sending server has support for. + /// + /// Defaults to `&[RoomVersionId::Version1]`. #[ruma_api(query)] - #[serde(skip_serializing_if = "<[_]>::is_empty")] + #[serde(default = "default_ver", skip_serializing_if = "is_default_ver")] pub ver: &'a [RoomVersionId], } + #[non_exhaustive] response: { /// The version of the room where the server is trying to join. + #[serde(skip_serializing_if = "Option::is_none")] pub room_version: Option, /// An unsigned template event. pub event: Raw, } } + +fn default_ver() -> Vec { + vec![RoomVersionId::Version1] +} + +fn is_default_ver(ver: &&[RoomVersionId]) -> bool { + **ver == [RoomVersionId::Version1] +} + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given room id and user id. + pub fn new(room_id: &'a RoomId, user_id: &'a UserId) -> Self { + Self { room_id, user_id, ver: &[RoomVersionId::Version1] } + } +} + +impl Response { + /// Creates a new `Response` with the given template event. + pub fn new(event: Raw) -> Self { + Self { room_version: None, event } + } +}