From 21eb1e8e411cb4776fd82252e7774878fdfdfdc8 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 24 Aug 2020 21:39:50 +0200 Subject: [PATCH] federation-api: Fix various issues --- .../src/discovery/get_server_version/v1.rs | 25 ++++++++++- ruma-federation-api/src/keys/claim_keys/v1.rs | 18 +++++--- ruma-federation-api/src/keys/get_keys/v1.rs | 4 +- .../src/membership/create_invite/v1.rs | 45 ++++++++++++------- .../src/membership/create_invite/v2.rs | 14 +++--- 5 files changed, 76 insertions(+), 30 deletions(-) diff --git a/ruma-federation-api/src/discovery/get_server_version/v1.rs b/ruma-federation-api/src/discovery/get_server_version/v1.rs index 913eb933..272ffd2e 100644 --- a/ruma-federation-api/src/discovery/get_server_version/v1.rs +++ b/ruma-federation-api/src/discovery/get_server_version/v1.rs @@ -26,13 +26,36 @@ ruma_api! { } } -#[derive(Clone, Debug, Serialize, Deserialize)] +impl Request { + /// Creates an empty `Request`. + pub fn new() -> Self { + Self + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Default::default() + } +} + /// Arbitrary values that identify this implementation. +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +#[non_exhaustive] pub struct Server { /// Arbitrary name that identifies this implementation. #[serde(skip_serializing_if = "Option::is_none")] pub name: Option, + /// Version of this implementation. The version format depends on the implementation. #[serde(skip_serializing_if = "Option::is_none")] pub version: Option, } + +impl Server { + /// Creates an empty `Server`. + pub fn new() -> Self { + Default::default() + } +} diff --git a/ruma-federation-api/src/keys/claim_keys/v1.rs b/ruma-federation-api/src/keys/claim_keys/v1.rs index 28553384..946e2e5f 100644 --- a/ruma-federation-api/src/keys/claim_keys/v1.rs +++ b/ruma-federation-api/src/keys/claim_keys/v1.rs @@ -20,13 +20,13 @@ ruma_api! { #[non_exhaustive] request: { /// The keys to be claimed. - one_time_keys: OneTimeKeyClaims, + pub one_time_keys: OneTimeKeyClaims, } #[non_exhaustive] response: { /// One-time keys for the queried devices - one_time_keys: OneTimeKeys, + pub one_time_keys: OneTimeKeys, } } @@ -51,11 +51,19 @@ pub type OneTimeKeyClaims = BTreeMap>>; /// A key and its signature -#[non_exhaustive] #[derive(Debug, Clone, Serialize, Deserialize)] +#[non_exhaustive] pub struct KeyObject { /// The key, encoded using unpadded base64. - key: String, + pub key: String, + /// Signature of the key object. - signatures: BTreeMap>, + pub signatures: BTreeMap>, +} + +impl KeyObject { + /// Creates a new `KeyObject` with the given key and signatures. + pub fn new(key: String, signatures: BTreeMap>) -> Self { + Self { key, signatures } + } } diff --git a/ruma-federation-api/src/keys/get_keys/v1.rs b/ruma-federation-api/src/keys/get_keys/v1.rs index 388cd544..df346b95 100644 --- a/ruma-federation-api/src/keys/get_keys/v1.rs +++ b/ruma-federation-api/src/keys/get_keys/v1.rs @@ -20,13 +20,13 @@ ruma_api! { request: { /// The keys to be downloaded. Gives all keys for a given user if the list of device ids is /// empty. - device_keys: BTreeMap>, + pub device_keys: BTreeMap>, } #[non_exhaustive] response: { /// Keys from the queried devices. - device_keys: BTreeMap>, + pub device_keys: BTreeMap>, } } diff --git a/ruma-federation-api/src/membership/create_invite/v1.rs b/ruma-federation-api/src/membership/create_invite/v1.rs index 63c9b567..c771b7e2 100644 --- a/ruma-federation-api/src/membership/create_invite/v1.rs +++ b/ruma-federation-api/src/membership/create_invite/v1.rs @@ -22,17 +22,17 @@ ruma_api! { request: { /// The room ID that the user is being invited to. #[ruma_api(path)] - pub room_id: RoomId, + pub room_id: &'a RoomId, /// The event ID for the invite event, generated by the inviting server. #[ruma_api(path)] - pub event_id: EventId, + pub event_id: &'a EventId, /// The matrix ID of the user who sent the original `m.room.third_party_invite`. - pub sender: UserId, + pub sender: &'a UserId, /// The name of the inviting homeserver. - pub origin: Box, + pub origin: &'a ServerName, /// A timestamp added by the inviting homeserver. pub origin_server_ts: UInt, @@ -42,12 +42,13 @@ ruma_api! { pub kind: EventType, /// The user ID of the invited member. - pub state_key: UserId, + pub state_key: &'a UserId, /// The content of the event. pub content: MemberEventContent, /// Information included alongside the event that is not signed. + #[serde(default, skip_serializing_if = "UnsignedEventContent::is_empty")] pub unsigned: UnsignedEventContent, } @@ -56,38 +57,52 @@ ruma_api! { /// The response invite event #[ruma_api(body)] #[serde(with = "crate::serde::invite_response")] - event: InviteEvent, + pub event: InviteEvent, } } /// Information included alongside an event that is not signed. -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +#[non_exhaustive] pub struct UnsignedEventContent { /// An optional list of simplified events to help the receiver of the invite identify the room. /// The recommended events to include are the join rules, canonical alias, avatar, and name of /// the room. + #[serde(skip_serializing_if = "<[_]>::is_empty")] pub invite_room_state: Vec, } +impl UnsignedEventContent { + /// Creates an empty `UnsignedEventContent`. + pub fn new() -> Self { + Default::default() + } + + /// Checks whether all of the fields are empty. + pub fn is_empty(&self) -> bool { + self.invite_room_state.is_empty() + } +} + /// Initial set of fields of `Request`. -pub struct RequestInit { +pub struct RequestInit<'a> { /// The room ID that the user is being invited to. - pub room_id: RoomId, + pub room_id: &'a RoomId, /// The event ID for the invite event, generated by the inviting server. - pub event_id: EventId, + pub event_id: &'a EventId, /// The matrix ID of the user who sent the original `m.room.third_party_invite`. - pub sender: UserId, + pub sender: &'a UserId, /// The name of the inviting homeserver. - pub origin: Box, + pub origin: &'a ServerName, /// A timestamp added by the inviting homeserver. pub origin_server_ts: UInt, /// The user ID of the invited member. - pub state_key: UserId, + pub state_key: &'a UserId, /// The content of the event. pub content: MemberEventContent, @@ -96,9 +111,9 @@ pub struct RequestInit { pub unsigned: UnsignedEventContent, } -impl From for Request { +impl<'a> From> for Request<'a> { /// Creates a new `Request` with the given parameters. - fn from(init: RequestInit) -> Self { + fn from(init: RequestInit<'a>) -> Self { Self { room_id: init.room_id, event_id: init.event_id, diff --git a/ruma-federation-api/src/membership/create_invite/v2.rs b/ruma-federation-api/src/membership/create_invite/v2.rs index 87ec05aa..0c552afb 100644 --- a/ruma-federation-api/src/membership/create_invite/v2.rs +++ b/ruma-federation-api/src/membership/create_invite/v2.rs @@ -19,31 +19,31 @@ ruma_api! { request: { /// The room ID that the user is being invited to. #[ruma_api(path)] - room_id: RoomId, + pub room_id: RoomId, /// The event ID for the invite event, generated by the inviting server. #[ruma_api(path)] - event_id: EventId, + pub event_id: EventId, /// The version of the room where the user is being invited to. - room_version: RoomVersionId, + pub room_version: RoomVersionId, /// An invite event. - event: InviteEvent, + pub event: InviteEvent, /// An optional list of simplified events to help the receiver of the invite identify the room. - invite_room_state: StrippedState, + pub invite_room_state: StrippedState, } #[non_exhaustive] response: { /// An invite event. - event: InviteEvent, + pub event: InviteEvent, } } impl Request { - /// Creates a new `Request` with the given parameters + /// Creates a new `Request` with the given parameters. pub fn new( room_id: RoomId, event_id: EventId,