From f84287038e0131c77c6740d7e9218332c1c7626e Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 23 Aug 2020 18:00:58 +0200 Subject: [PATCH] federation-api: Make remaining request / response types non-exhaustive --- .../src/event/get_missing_events/v1.rs | 4 +++- .../src/membership/create_join_event.rs | 10 ++++++++ .../src/membership/create_join_event/v1.rs | 17 ++++++++++++++ .../src/membership/create_join_event/v2.rs | 21 +++++++++++++++-- .../src/openid/get_openid_userinfo/v1.rs | 16 +++++++++++++ .../src/query/get_profile_information/v1.rs | 21 +++++++++++++++-- .../src/query/get_room_information/v1.rs | 16 +++++++++++++ .../send_transaction_message/v1.rs | 23 +++++++++++++++++++ 8 files changed, 123 insertions(+), 5 deletions(-) diff --git a/ruma-federation-api/src/event/get_missing_events/v1.rs b/ruma-federation-api/src/event/get_missing_events/v1.rs index 8cb29be1..ac3c5487 100644 --- a/ruma-federation-api/src/event/get_missing_events/v1.rs +++ b/ruma-federation-api/src/event/get_missing_events/v1.rs @@ -36,8 +36,10 @@ ruma_api! { latest_events: &'a [EventId], } + #[derive(Default)] + #[non_exhaustive] response: { - /// The missing events. + /// The missing PDUs. events: Vec } } diff --git a/ruma-federation-api/src/membership/create_join_event.rs b/ruma-federation-api/src/membership/create_join_event.rs index 4e8f8e76..9d4d3dc6 100644 --- a/ruma-federation-api/src/membership/create_join_event.rs +++ b/ruma-federation-api/src/membership/create_join_event.rs @@ -9,12 +9,22 @@ use serde::{Deserialize, Serialize}; /// Full state of the room. #[derive(Clone, Debug, Deserialize, Serialize)] +#[non_exhaustive] pub struct RoomState { /// The resident server's DNS name. pub origin: String, + /// The full set of authorization events that make up the state of the room, /// and their authorization events, recursively. pub auth_chain: Vec>, + /// The room state. pub state: Vec>, } + +impl RoomState { + /// Creates an empty `RoomState` with the given `origin`. + pub fn new(origin: String) -> Self { + Self { origin, auth_chain: Vec::new(), state: Vec::new() } + } +} diff --git a/ruma-federation-api/src/membership/create_join_event/v1.rs b/ruma-federation-api/src/membership/create_join_event/v1.rs index 64e96667..2aa2b5ac 100644 --- a/ruma-federation-api/src/membership/create_join_event/v1.rs +++ b/ruma-federation-api/src/membership/create_join_event/v1.rs @@ -17,6 +17,7 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The room ID that is about to be joined. #[ruma_api(path)] @@ -31,6 +32,7 @@ ruma_api! { pub pdu_stub: Raw, } + #[non_exhaustive] response: { /// Full state and auth chain of the room prior to the join event. #[ruma_api(body)] @@ -38,3 +40,18 @@ ruma_api! { pub room_state: RoomState, } } + +// FIXME: Construct from Pdu, same for similar endpoints +impl<'a> Request<'a> { + /// Creates a `Request` from the given room ID, event ID and `PduStub`. + pub fn new(room_id: &'a RoomId, event_id: &'a EventId, pdu_stub: Raw) -> Self { + Self { room_id, event_id, pdu_stub } + } +} + +impl Response { + /// Creates a new `Response` with the given room state. + pub fn new(room_state: RoomState) -> Self { + Self { room_state } + } +} diff --git a/ruma-federation-api/src/membership/create_join_event/v2.rs b/ruma-federation-api/src/membership/create_join_event/v2.rs index aecbe8d7..1722c7b0 100644 --- a/ruma-federation-api/src/membership/create_join_event/v2.rs +++ b/ruma-federation-api/src/membership/create_join_event/v2.rs @@ -17,23 +17,40 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The room ID that is about to be joined. #[ruma_api(path)] - pub room_id: RoomId, + pub room_id: &'a RoomId, /// The user ID the join event will be for. #[ruma_api(path)] - pub event_id: EventId, + pub event_id: &'a EventId, /// PDU type without event and room IDs. #[ruma_api(body)] pub pdu_stub: Raw, } + #[non_exhaustive] response: { /// Full state of the room. #[ruma_api(body)] pub room_state: RoomState, } } + +// FIXME: Construct from Pdu, same for similar endpoints +impl<'a> Request<'a> { + /// Creates a `Request` from the given room ID, event ID and `PduStub`. + pub fn new(room_id: &'a RoomId, event_id: &'a EventId, pdu_stub: Raw) -> Self { + Self { room_id, event_id, pdu_stub } + } +} + +impl Response { + /// Creates a new `Response` with the given room state. + pub fn new(room_state: RoomState) -> Self { + Self { room_state } + } +} diff --git a/ruma-federation-api/src/openid/get_openid_userinfo/v1.rs b/ruma-federation-api/src/openid/get_openid_userinfo/v1.rs index 98ea1bac..c3c54213 100644 --- a/ruma-federation-api/src/openid/get_openid_userinfo/v1.rs +++ b/ruma-federation-api/src/openid/get_openid_userinfo/v1.rs @@ -13,14 +13,30 @@ ruma_api! { requires_authentication: false, } + #[non_exhaustive] request: { /// The OpenID access token to get information about the owner for. #[ruma_api(query)] pub access_token: &'a str, } + #[non_exhaustive] response: { /// The Matrix User ID who generated the token. pub sub: UserId, } } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given access token. + pub fn new(access_token: &'a str) -> Self { + Self { access_token } + } +} + +impl Response { + /// Creates a new `Response` with the given user id. + pub fn new(sub: UserId) -> Self { + Self { sub } + } +} diff --git a/ruma-federation-api/src/query/get_profile_information/v1.rs b/ruma-federation-api/src/query/get_profile_information/v1.rs index 37cf0803..17b64578 100644 --- a/ruma-federation-api/src/query/get_profile_information/v1.rs +++ b/ruma-federation-api/src/query/get_profile_information/v1.rs @@ -14,10 +14,11 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// User ID to query. #[ruma_api(query)] - pub user_id: UserId, + pub user_id: &'a UserId, /// Profile field to query. #[serde(skip_serializing_if = "Option::is_none")] @@ -25,6 +26,8 @@ ruma_api! { pub field: Option, } + #[derive(Default)] + #[non_exhaustive] response: { /// Display name of the user. #[serde(skip_serializing_if = "Option::is_none")] @@ -36,8 +39,22 @@ ruma_api! { } } +impl<'a> Request<'a> { + /// Creates a new `Request` with the given user id. + pub fn new(user_id: &'a UserId) -> Self { + Self { user_id, field: None } + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Default::default() + } +} + /// Profile fields to specify in query. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)] pub enum ProfileField { /// Display name of the user. #[serde(rename = "displayname")] diff --git a/ruma-federation-api/src/query/get_room_information/v1.rs b/ruma-federation-api/src/query/get_room_information/v1.rs index 90f4efa8..a1886ad9 100644 --- a/ruma-federation-api/src/query/get_room_information/v1.rs +++ b/ruma-federation-api/src/query/get_room_information/v1.rs @@ -13,12 +13,14 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// Room alias to query. #[ruma_api(query)] pub room_alias: &'a RoomAliasId, } + #[non_exhaustive] response: { /// Room ID mapped to queried alias. pub room_id: RoomId, @@ -27,3 +29,17 @@ ruma_api! { pub servers: Vec, } } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given room alias ID. + pub fn new(room_alias: &'a RoomAliasId) -> Self { + Self { room_alias } + } +} + +impl Response { + /// Creates a new `Response` with the given room IDs and servers. + pub fn new(room_id: RoomId, servers: Vec) -> Self { + Self { room_id, servers } + } +} diff --git a/ruma-federation-api/src/transactions/send_transaction_message/v1.rs b/ruma-federation-api/src/transactions/send_transaction_message/v1.rs index 38541352..53661627 100644 --- a/ruma-federation-api/src/transactions/send_transaction_message/v1.rs +++ b/ruma-federation-api/src/transactions/send_transaction_message/v1.rs @@ -18,6 +18,7 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// A transaction ID unique between sending and receiving homeservers. #[ruma_api(path)] @@ -43,6 +44,8 @@ ruma_api! { pub edus: &'a [Edu], } + #[derive(Default)] + #[non_exhaustive] response: { /// Map of event IDs and response for each PDU given in the request. #[serde(with = "crate::serde::pdu_process_response")] @@ -50,6 +53,26 @@ ruma_api! { } } +impl<'a> Request<'a> { + /// Creates a new `Request` with the given transaction ID, origin, timestamp. + /// + /// The PDU and EDU lists will start off empty. + pub fn new( + transaction_id: &'a str, + origin: &'a ServerName, + origin_server_ts: SystemTime, + ) -> Self { + Self { transaction_id, origin, origin_server_ts, pdus: &[], edus: &[] } + } +} + +impl Response { + /// Creates a new `Response` with the given PDUs. + pub fn new(pdus: BTreeMap>) -> Self { + Self { pdus } + } +} + /// Type for passing ephemeral data to homeservers. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Edu {