From fec07a7426e898938a91e612ce3c1c0a64e935d5 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 28 Aug 2020 23:37:22 +0200 Subject: [PATCH] Update lots of types to the new API standards --- ruma-client-api/src/r0/account.rs | 17 ++++-- ruma-client-api/src/r0/account/add_3pid.rs | 17 ++++++ ruma-client-api/src/r0/account/bind_3pid.rs | 30 ++++++++-- .../src/r0/account/change_password.rs | 20 ++++++- ruma-client-api/src/r0/account/deactivate.rs | 17 ++++++ ruma-client-api/src/r0/account/delete_3pid.rs | 14 ++++- .../r0/account/get_username_availability.rs | 20 ++++++- ...request_3pid_management_token_via_email.rs | 26 +++++++-- ...equest_3pid_management_token_via_msisdn.rs | 41 +++++++++++-- .../src/r0/account/request_openid_token.rs | 25 +++++++- ...request_password_change_token_via_email.rs | 27 +++++++-- ...equest_password_change_token_via_msisdn.rs | 30 ++++++++-- .../request_registration_token_via_email.rs | 27 +++++++-- .../request_registration_token_via_msisdn.rs | 40 ++++++++++--- ruma-client-api/src/r0/account/unbind_3pid.rs | 20 ++++++- ruma-client-api/src/r0/account/whoami.rs | 17 ++++++ .../src/r0/appservice/set_room_visibility.rs | 21 ++++++- .../src/r0/capabilities/get_capabilities.rs | 23 ++++++++ ruma-client-api/src/r0/keys/claim_keys.rs | 16 +++++ .../src/r0/keys/get_key_changes.rs | 16 +++++ ruma-client-api/src/r0/keys/get_keys.rs | 22 ++++++- ruma-client-api/src/r0/keys/upload_keys.rs | 21 ++++++- .../src/r0/keys/upload_signatures.rs | 17 ++++++ .../src/r0/keys/upload_signing_keys.rs | 18 ++++++ ruma-client-api/src/r0/push.rs | 1 + ruma-client-api/src/r0/session/login.rs | 16 +++++ .../src/r0/to_device/send_event_to_device.rs | 21 +++++++ ruma-client/src/lib.rs | 14 ++--- ruma-common/src/encryption.rs | 58 +++++++++++++------ .../src/device/get_devices/v1.rs | 6 +- ruma-federation-api/src/keys/get_keys/v1.rs | 6 +- 31 files changed, 577 insertions(+), 87 deletions(-) diff --git a/ruma-client-api/src/r0/account.rs b/ruma-client-api/src/r0/account.rs index f2c24fdd..98cbc925 100644 --- a/ruma-client-api/src/r0/account.rs +++ b/ruma-client-api/src/r0/account.rs @@ -18,17 +18,26 @@ pub mod unbind_3pid; pub mod whoami; +use ruma_api::Outgoing; use serde::{Deserialize, Serialize}; /// Additional authentication information for requestToken endpoints. -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct IdentityServerInfo { +#[derive(Clone, Debug, Outgoing, Serialize)] +#[non_exhaustive] +pub struct IdentityServerInfo<'a> { /// The ID server to send the onward request to as a hostname with an /// appended colon and port number if the port is not the default. - pub id_server: String, + pub id_server: &'a str, /// Access token previously registered with identity server. - pub id_access_token: String, + pub id_access_token: &'a str, +} + +impl<'a> IdentityServerInfo<'a> { + /// Creates a new `IdentityServerInfo` with the given server name and access token. + pub fn new(id_server: &'a str, id_access_token: &'a str) -> Self { + Self { id_server, id_access_token } + } } /// Possible values for deleting or unbinding 3PIDs diff --git a/ruma-client-api/src/r0/account/add_3pid.rs b/ruma-client-api/src/r0/account/add_3pid.rs index 27af4307..a276ead8 100644 --- a/ruma-client-api/src/r0/account/add_3pid.rs +++ b/ruma-client-api/src/r0/account/add_3pid.rs @@ -14,6 +14,7 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// Additional information for the User-Interactive Authentication API. #[serde(skip_serializing_if = "Option::is_none")] @@ -26,7 +27,23 @@ ruma_api! { pub sid: &'a str, } + #[derive(Default)] + #[non_exhaustive] response: {} error: UiaaResponse } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given client secret and session identifier. + pub fn new(client_secret: &'a str, sid: &'a str) -> Self { + Self { auth: None, client_secret, sid } + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self + } +} diff --git a/ruma-client-api/src/r0/account/bind_3pid.rs b/ruma-client-api/src/r0/account/bind_3pid.rs index fff6820e..ef99bb4a 100644 --- a/ruma-client-api/src/r0/account/bind_3pid.rs +++ b/ruma-client-api/src/r0/account/bind_3pid.rs @@ -2,7 +2,7 @@ use ruma_api::ruma_api; -use super::IdentityServerInfo; +use super::{IdentityServerInfo, IncomingIdentityServerInfo}; ruma_api! { metadata: { @@ -14,20 +14,42 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// Client-generated secret string used to protect this session. - pub client_secret: String, + pub client_secret: &'a str, /// The ID server to send the onward request to as a hostname with an /// appended colon and port number if the port is not the default. #[serde(flatten)] - pub identity_server_info: IdentityServerInfo, + pub identity_server_info: IdentityServerInfo<'a>, /// The session identifier given by the identity server. - pub sid: String, + pub sid: &'a str, } + #[derive(Default)] + #[non_exhaustive] response: {} error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given client secret, identity server information and + /// session identifier. + pub fn new( + client_secret: &'a str, + identity_server_info: IdentityServerInfo<'a>, + sid: &'a str, + ) -> Self { + Self { client_secret, identity_server_info, sid } + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self + } +} diff --git a/ruma-client-api/src/r0/account/change_password.rs b/ruma-client-api/src/r0/account/change_password.rs index 882ad4b6..854c7813 100644 --- a/ruma-client-api/src/r0/account/change_password.rs +++ b/ruma-client-api/src/r0/account/change_password.rs @@ -14,9 +14,10 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The new password for the account. - pub new_password: String, + pub new_password: &'a str, /// True to revoke the user's other access tokens, and their associated devices if the /// request succeeds. @@ -29,10 +30,27 @@ ruma_api! { pub logout_devices: bool, /// Additional authentication information for the user-interactive authentication API. + #[serde(skip_serializing_if = "Option::is_none")] pub auth: Option>, } + #[derive(Default)] + #[non_exhaustive] response: {} error: UiaaResponse } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given password. + pub fn new(new_password: &'a str) -> Self { + Self { new_password, logout_devices: true, auth: None } + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self + } +} diff --git a/ruma-client-api/src/r0/account/deactivate.rs b/ruma-client-api/src/r0/account/deactivate.rs index 182c6137..1e4ceffe 100644 --- a/ruma-client-api/src/r0/account/deactivate.rs +++ b/ruma-client-api/src/r0/account/deactivate.rs @@ -16,6 +16,8 @@ ruma_api! { requires_authentication: true, } + #[derive(Default)] + #[non_exhaustive] request: { /// Additional authentication information for the user-interactive authentication API. #[serde(skip_serializing_if = "Option::is_none")] @@ -27,6 +29,7 @@ ruma_api! { pub id_server: Option<&'a str>, } + #[non_exhaustive] response: { /// Result of unbind operation. pub id_server_unbind_result: ThirdPartyIdRemovalStatus, @@ -34,3 +37,17 @@ ruma_api! { error: UiaaResponse } + +impl Request<'_> { + /// Creates an empty `Request`. + pub fn new() -> Self { + Default::default() + } +} + +impl Response { + /// Creates a new `Response` with the given unbind result. + pub fn new(id_server_unbind_result: ThirdPartyIdRemovalStatus) -> Self { + Self { id_server_unbind_result } + } +} diff --git a/ruma-client-api/src/r0/account/delete_3pid.rs b/ruma-client-api/src/r0/account/delete_3pid.rs index 3ea33efc..6916fc70 100644 --- a/ruma-client-api/src/r0/account/delete_3pid.rs +++ b/ruma-client-api/src/r0/account/delete_3pid.rs @@ -15,23 +15,31 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// Identity server to delete from. #[serde(skip_serializing_if = "Option::is_none")] - pub id_server: Option, + pub id_server: Option<&'a str>, /// Medium of the 3PID to be removed. pub medium: Medium, /// Third-party address being removed. - pub address: String, + pub address: &'a str, } + #[non_exhaustive] response: { /// Result of unbind operation. pub id_server_unbind_result: ThirdPartyIdRemovalStatus, } error: crate::Error - +} + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given medium and address. + pub fn new(medium: Medium, address: &'a str) -> Self { + Self { id_server: None, medium, address } + } } diff --git a/ruma-client-api/src/r0/account/get_username_availability.rs b/ruma-client-api/src/r0/account/get_username_availability.rs index 48ee79a9..a180be1a 100644 --- a/ruma-client-api/src/r0/account/get_username_availability.rs +++ b/ruma-client-api/src/r0/account/get_username_availability.rs @@ -12,17 +12,33 @@ ruma_api! { requires_authentication: false, } + #[non_exhaustive] request: { /// The username to check the availability of. #[ruma_api(query)] - pub username: String, + pub username: &'a str, } + #[non_exhaustive] response: { /// A flag to indicate that the username is available. /// This should always be true when the server replies with 200 OK. - pub available: bool + pub available: bool, } error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given username. + pub fn new(username: &'a str) -> Self { + Self { username } + } +} + +impl Response { + /// Creates a new `Response` with the given availability flag. + pub fn new(available: bool) -> Self { + Self { available } + } +} diff --git a/ruma-client-api/src/r0/account/request_3pid_management_token_via_email.rs b/ruma-client-api/src/r0/account/request_3pid_management_token_via_email.rs index c5205c5e..c9e85809 100644 --- a/ruma-client-api/src/r0/account/request_3pid_management_token_via_email.rs +++ b/ruma-client-api/src/r0/account/request_3pid_management_token_via_email.rs @@ -3,7 +3,7 @@ use js_int::UInt; use ruma_api::ruma_api; -use super::IdentityServerInfo; +use super::{IdentityServerInfo, IncomingIdentityServerInfo}; ruma_api! { metadata: { @@ -15,26 +15,28 @@ ruma_api! { requires_authentication: false, } + #[non_exhaustive] request: { /// Client-generated secret string used to protect this session. - pub client_secret: String, + pub client_secret: &'a str, /// The email address. - pub email: String, + pub email: &'a str, /// Used to distinguish protocol level retries from requests to re-send the email. pub send_attempt: UInt, /// Return URL for identity server to redirect the client back to. #[serde(skip_serializing_if = "Option::is_none")] - pub next_link: Option, + pub next_link: Option<&'a str>, /// Optional identity server hostname and access token. Deprecated since r0.6.0. #[serde(flatten)] #[serde(skip_serializing_if = "Option::is_none")] - pub identity_server_info: Option, + pub identity_server_info: Option>, } + #[non_exhaustive] response: { /// The session identifier given by the identity server. pub sid: String, @@ -46,3 +48,17 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the client secret, email and send-attempt counter. + pub fn new(client_secret: &'a str, email: &'a str, send_attempt: UInt) -> Self { + Self { client_secret, email, send_attempt, next_link: None, identity_server_info: None } + } +} + +impl Response { + /// Creates a new `Response` with the given session identifier. + pub fn new(sid: String) -> Self { + Self { sid, submit_url: None } + } +} diff --git a/ruma-client-api/src/r0/account/request_3pid_management_token_via_msisdn.rs b/ruma-client-api/src/r0/account/request_3pid_management_token_via_msisdn.rs index d420af33..5ec50ad6 100644 --- a/ruma-client-api/src/r0/account/request_3pid_management_token_via_msisdn.rs +++ b/ruma-client-api/src/r0/account/request_3pid_management_token_via_msisdn.rs @@ -3,7 +3,7 @@ use js_int::UInt; use ruma_api::ruma_api; -use super::IdentityServerInfo; +use super::{IdentityServerInfo, IncomingIdentityServerInfo}; ruma_api! { metadata: { @@ -15,29 +15,31 @@ ruma_api! { requires_authentication: false, } + #[non_exhaustive] request: { /// Client-generated secret string used to protect this session. - pub client_secret: String, + pub client_secret: &'a str, /// Two-letter ISO 3166 country code for the phone number. - pub country: String, + pub country: &'a str, /// Phone number to validate. - pub phone_number: String, + pub phone_number: &'a str, /// Used to distinguish protocol level retries from requests to re-send the SMS. pub send_attempt: UInt, /// Return URL for identity server to redirect the client back to. #[serde(skip_serializing_if = "Option::is_none")] - pub next_link: Option, + pub next_link: Option<&'a str>, /// Optional identity server hostname and access token. Deprecated since r0.6.0. #[serde(flatten)] #[serde(skip_serializing_if = "Option::is_none")] - pub identity_server_info: Option, + pub identity_server_info: Option>, } + #[non_exhaustive] response: { /// The session identifier given by the identity server. pub sid: String, @@ -49,3 +51,30 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given client secret, country code, phone number and + /// send-attempt counter. + pub fn new( + client_secret: &'a str, + country: &'a str, + phone_number: &'a str, + send_attempt: UInt, + ) -> Self { + Self { + client_secret, + country, + phone_number, + send_attempt, + next_link: None, + identity_server_info: None, + } + } +} + +impl Response { + /// Creates a new `Response` with the given session identifier. + pub fn new(sid: String) -> Self { + Self { sid, submit_url: None } + } +} diff --git a/ruma-client-api/src/r0/account/request_openid_token.rs b/ruma-client-api/src/r0/account/request_openid_token.rs index b9b71ee4..0aa38628 100644 --- a/ruma-client-api/src/r0/account/request_openid_token.rs +++ b/ruma-client-api/src/r0/account/request_openid_token.rs @@ -16,12 +16,14 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// User ID of authenticated user. #[ruma_api(path)] - pub user_id: UserId, + pub user_id: &'a UserId, } + #[non_exhaustive] response: { /// Access token for verifying user's identity. pub access_token: String, @@ -40,8 +42,29 @@ ruma_api! { error: crate::Error } +impl<'a> Request<'a> { + /// Creates a new `Request` with the given user ID. + pub fn new(user_id: &'a UserId) -> Self { + Self { user_id } + } +} + +impl Response { + /// Creates a new `Response` with the given access token, token type, server name and expiration + /// duration. + pub fn new( + access_token: String, + token_type: TokenType, + matrix_server_name: ServerNameBox, + expires_in: Duration, + ) -> Self { + Self { access_token, token_type, matrix_server_name, expires_in } + } +} + /// Access token types. #[derive(Clone, Copy, Debug, Deserialize, Serialize)] +#[non_exhaustive] pub enum TokenType { /// Bearer token type Bearer, diff --git a/ruma-client-api/src/r0/account/request_password_change_token_via_email.rs b/ruma-client-api/src/r0/account/request_password_change_token_via_email.rs index e79d9e7f..016cdc90 100644 --- a/ruma-client-api/src/r0/account/request_password_change_token_via_email.rs +++ b/ruma-client-api/src/r0/account/request_password_change_token_via_email.rs @@ -3,7 +3,7 @@ use js_int::UInt; use ruma_api::ruma_api; -use super::IdentityServerInfo; +use super::{IdentityServerInfo, IncomingIdentityServerInfo}; ruma_api! { metadata: { @@ -15,26 +15,28 @@ ruma_api! { requires_authentication: false, } + #[non_exhaustive] request: { /// Client-generated secret string used to protect this session. - pub client_secret: String, + pub client_secret: &'a str, /// The email address. - pub email: String, + pub email: &'a str, /// Used to distinguish protocol level retries from requests to re-send the email. pub send_attempt: UInt, /// Return URL for identity server to redirect the client back to. #[serde(skip_serializing_if = "Option::is_none")] - pub next_link: Option, + pub next_link: Option<&'a str>, /// Optional identity server hostname and access token. Deprecated since r0.6.0. #[serde(flatten)] #[serde(skip_serializing_if = "Option::is_none")] - pub identity_server_info: Option, + pub identity_server_info: Option>, } + #[non_exhaustive] response: { /// The session identifier given by the identity server. pub sid: String, @@ -46,3 +48,18 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given client secret, email address and send-attempt + /// counter. + pub fn new(client_secret: &'a str, email: &'a str, send_attempt: UInt) -> Self { + Self { client_secret, email, send_attempt, next_link: None, identity_server_info: None } + } +} + +impl Response { + /// Creates a new `Response` with the given session identifier. + pub fn new(sid: String) -> Self { + Self { sid, submit_url: None } + } +} diff --git a/ruma-client-api/src/r0/account/request_password_change_token_via_msisdn.rs b/ruma-client-api/src/r0/account/request_password_change_token_via_msisdn.rs index e8e9d5a6..344ecd67 100644 --- a/ruma-client-api/src/r0/account/request_password_change_token_via_msisdn.rs +++ b/ruma-client-api/src/r0/account/request_password_change_token_via_msisdn.rs @@ -13,24 +13,26 @@ ruma_api! { requires_authentication: false, } + #[non_exhaustive] request: { /// Client-generated secret string used to protect this session. - pub client_secret: String, + pub client_secret: &'a str, /// Two-letter ISO 3166 country code for the phone number. - pub country: String, + pub country: &'a str, /// Phone number to validate. - pub phone_number: String, + pub phone_number: &'a str, /// Used to distinguish protocol level retries from requests to re-send the SMS. pub send_attempt: UInt, /// Return URL for identity server to redirect the client back to. #[serde(skip_serializing_if = "Option::is_none")] - pub next_link: Option, + pub next_link: Option<&'a str>, } + #[non_exhaustive] response: { /// The session identifier given by the identity server. pub sid: String, @@ -42,3 +44,23 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given client secret, country code, phone number and + /// send-attempt counter. + pub fn new( + client_secret: &'a str, + country: &'a str, + phone_number: &'a str, + send_attempt: UInt, + ) -> Self { + Self { client_secret, country, phone_number, send_attempt, next_link: None } + } +} + +impl Response { + /// Creates a new `Response` with the given session identifier. + pub fn new(sid: String) -> Self { + Self { sid, submit_url: None } + } +} diff --git a/ruma-client-api/src/r0/account/request_registration_token_via_email.rs b/ruma-client-api/src/r0/account/request_registration_token_via_email.rs index ca28acb9..12e1ce01 100644 --- a/ruma-client-api/src/r0/account/request_registration_token_via_email.rs +++ b/ruma-client-api/src/r0/account/request_registration_token_via_email.rs @@ -3,7 +3,7 @@ use js_int::UInt; use ruma_api::ruma_api; -use super::IdentityServerInfo; +use super::{IdentityServerInfo, IncomingIdentityServerInfo}; ruma_api! { metadata: { @@ -15,26 +15,28 @@ ruma_api! { requires_authentication: false, } + #[non_exhaustive] request: { /// Client-generated secret string used to protect this session. - pub client_secret: String, + pub client_secret: &'a str, /// The email address. - pub email: String, + pub email: &'a str, /// Used to distinguish protocol level retries from requests to re-send the email. pub send_attempt: UInt, /// Return URL for identity server to redirect the client back to. #[serde(skip_serializing_if = "Option::is_none")] - pub next_link: Option, + pub next_link: Option<&'a str>, /// Optional identity server hostname and access token. Deprecated since r0.6.0. #[serde(flatten)] #[serde(skip_serializing_if = "Option::is_none")] - pub identity_server_info: Option, + pub identity_server_info: Option>, } + #[non_exhaustive] response: { /// The session identifier given by the identity server. pub sid: String, @@ -46,3 +48,18 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given client secret, email address and send-attempt + /// counter. + pub fn new(client_secret: &'a str, email: &'a str, send_attempt: UInt) -> Self { + Self { client_secret, email, send_attempt, next_link: None, identity_server_info: None } + } +} + +impl Response { + /// Creates a new `Response` with the given session identifier. + pub fn new(sid: String) -> Self { + Self { sid, submit_url: None } + } +} diff --git a/ruma-client-api/src/r0/account/request_registration_token_via_msisdn.rs b/ruma-client-api/src/r0/account/request_registration_token_via_msisdn.rs index a9b0d902..c783bba2 100644 --- a/ruma-client-api/src/r0/account/request_registration_token_via_msisdn.rs +++ b/ruma-client-api/src/r0/account/request_registration_token_via_msisdn.rs @@ -3,7 +3,7 @@ use js_int::UInt; use ruma_api::ruma_api; -use super::IdentityServerInfo; +use super::{IdentityServerInfo, IncomingIdentityServerInfo}; ruma_api! { metadata: { @@ -15,29 +15,31 @@ ruma_api! { requires_authentication: false, } + #[non_exhaustive] request: { /// Client-generated secret string used to protect this session. - pub client_secret: String, + pub client_secret: &'a str, /// Two-letter ISO 3166 country code for the phone number. - pub country: String, + pub country: &'a str, /// Phone number to validate. - pub phone_number: String, - - /// Return URL for identity server to redirect the client back to. - #[serde(skip_serializing_if = "Option::is_none")] - pub next_link: Option, + pub phone_number: &'a str, /// Used to distinguish protocol level retries from requests to re-send the SMS. pub send_attempt: UInt, + /// Return URL for identity server to redirect the client back to. + #[serde(skip_serializing_if = "Option::is_none")] + pub next_link: Option<&'a str>, + /// Optional identity server hostname and access token. Deprecated since r0.6.0. #[serde(flatten)] #[serde(skip_serializing_if = "Option::is_none")] - pub identity_server_info: Option, + pub identity_server_info: Option>, } + #[non_exhaustive] response: { /// The session identifier given by the identity server. pub sid: String, @@ -49,3 +51,23 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given client secret, country code, phone number and + /// send-attempt counter. + pub fn new( + client_secret: &'a str, + country: &'a str, + phone_number: &'a str, + send_attempt: UInt, + ) -> Self { + Self { + client_secret, + country, + phone_number, + send_attempt, + next_link: None, + identity_server_info: None, + } + } +} diff --git a/ruma-client-api/src/r0/account/unbind_3pid.rs b/ruma-client-api/src/r0/account/unbind_3pid.rs index b3c73b18..a4b15cc3 100644 --- a/ruma-client-api/src/r0/account/unbind_3pid.rs +++ b/ruma-client-api/src/r0/account/unbind_3pid.rs @@ -15,18 +15,20 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// Identity server to unbind from. #[serde(skip_serializing_if = "Option::is_none")] - pub id_server: Option, + pub id_server: Option<&'a str>, /// Medium of the 3PID to be removed. pub medium: Medium, /// Third-party address being removed. - pub address: String, + pub address: &'a str, } + #[non_exhaustive] response: { /// Result of unbind operation. pub id_server_unbind_result: ThirdPartyIdRemovalStatus, @@ -34,3 +36,17 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given medium and third-party address. + pub fn new(medium: Medium, address: &'a str) -> Self { + Self { id_server: None, medium, address } + } +} + +impl Response { + /// Creates a new `Response` with the given unbind result. + pub fn new(id_server_unbind_result: ThirdPartyIdRemovalStatus) -> Self { + Self { id_server_unbind_result } + } +} diff --git a/ruma-client-api/src/r0/account/whoami.rs b/ruma-client-api/src/r0/account/whoami.rs index f2c44d7b..fac644cc 100644 --- a/ruma-client-api/src/r0/account/whoami.rs +++ b/ruma-client-api/src/r0/account/whoami.rs @@ -13,8 +13,11 @@ ruma_api! { requires_authentication: true, } + #[derive(Default)] + #[non_exhaustive] request: {} + #[non_exhaustive] response: { /// The id of the user that owns the access token. pub user_id: UserId, @@ -22,3 +25,17 @@ ruma_api! { error: crate::Error } + +impl Request { + /// Creates an empty `Request`. + pub fn new() -> Self { + Self + } +} + +impl Response { + /// Creates a new `Response` with the given user ID. + pub fn new(user_id: UserId) -> Self { + Self { user_id } + } +} diff --git a/ruma-client-api/src/r0/appservice/set_room_visibility.rs b/ruma-client-api/src/r0/appservice/set_room_visibility.rs index 1d107067..3a76ea32 100644 --- a/ruma-client-api/src/r0/appservice/set_room_visibility.rs +++ b/ruma-client-api/src/r0/appservice/set_room_visibility.rs @@ -15,20 +15,37 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The protocol (network) ID to update the room list for. #[ruma_api(path)] - pub network_id: String, + pub network_id: &'a str, /// The room ID to add to the directory. #[ruma_api(path)] - pub room_id: RoomId, + pub room_id: &'a RoomId, /// Whether the room should be visible (public) in the directory or not (private). pub visibility: Visibility, } + #[derive(Default)] + #[non_exhaustive] response: {} error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given network ID, room ID and visibility. + pub fn new(network_id: &'a str, room_id: &'a RoomId, visibility: Visibility) -> Self { + Self { network_id, room_id, visibility } + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self + } +} diff --git a/ruma-client-api/src/r0/capabilities/get_capabilities.rs b/ruma-client-api/src/r0/capabilities/get_capabilities.rs index 0c7bb7d7..087c9d86 100644 --- a/ruma-client-api/src/r0/capabilities/get_capabilities.rs +++ b/ruma-client-api/src/r0/capabilities/get_capabilities.rs @@ -16,8 +16,11 @@ ruma_api! { requires_authentication: true } + #[derive(Default)] + #[non_exhaustive] request: {} + #[non_exhaustive] response: { /// The capabilities the server supports pub capabilities: Capabilities, @@ -26,6 +29,26 @@ ruma_api! { error: crate::Error } +impl Request { + /// Creates an empty `Request`. + pub fn new() -> Self { + Self + } +} + +impl Response { + /// Creates a new `Response` with the given capabilities. + pub fn new(capabilities: Capabilities) -> Self { + Self { capabilities } + } +} + +impl From for Response { + fn from(capabilities: Capabilities) -> Self { + Self::new(capabilities) + } +} + /// Contains information about all the capabilities that the server supports. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Capabilities { diff --git a/ruma-client-api/src/r0/keys/claim_keys.rs b/ruma-client-api/src/r0/keys/claim_keys.rs index 8b1f4d84..dc393727 100644 --- a/ruma-client-api/src/r0/keys/claim_keys.rs +++ b/ruma-client-api/src/r0/keys/claim_keys.rs @@ -20,6 +20,7 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The time (in milliseconds) to wait when downloading keys from remote servers. /// 10 seconds is the recommended default. @@ -34,6 +35,7 @@ ruma_api! { pub one_time_keys: BTreeMap>, } + #[non_exhaustive] response: { /// If any remote homeservers could not be reached, they are recorded here. /// The names of the properties are the names of the unreachable servers. @@ -46,5 +48,19 @@ ruma_api! { error: crate::Error } +impl Request { + /// Creates a new `Request` with the given key claims and the recommended 10 second timeout. + pub fn new(one_time_keys: BTreeMap>) -> Self { + Self { timeout: Some(Duration::from_secs(10)), one_time_keys } + } +} + +impl Response { + /// Creates a new `Response` with the given keys and no failures. + pub fn new(one_time_keys: BTreeMap) -> Self { + Self { failures: BTreeMap::new(), one_time_keys } + } +} + /// The one-time keys for a given device. pub type OneTimeKeys = BTreeMap>; diff --git a/ruma-client-api/src/r0/keys/get_key_changes.rs b/ruma-client-api/src/r0/keys/get_key_changes.rs index 350cefcd..596b8add 100644 --- a/ruma-client-api/src/r0/keys/get_key_changes.rs +++ b/ruma-client-api/src/r0/keys/get_key_changes.rs @@ -13,6 +13,7 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The desired start point of the list. /// @@ -28,6 +29,7 @@ ruma_api! { pub to: &'a str, } + #[non_exhaustive] response: { /// The Matrix User IDs of all users who updated their device identity keys. pub changed: Vec, @@ -39,3 +41,17 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given start and end points. + pub fn new(from: &'a str, to: &'a str) -> Self { + Self { from, to } + } +} + +impl Response { + /// Creates a new `Response` with the given changed and left user ID lists. + pub fn new(changed: Vec, left: Vec) -> Self { + Self { changed, left } + } +} diff --git a/ruma-client-api/src/r0/keys/get_keys.rs b/ruma-client-api/src/r0/keys/get_keys.rs index 5a942d85..23fd4d6b 100644 --- a/ruma-client-api/src/r0/keys/get_keys.rs +++ b/ruma-client-api/src/r0/keys/get_keys.rs @@ -3,7 +3,7 @@ use std::{collections::BTreeMap, time::Duration}; use ruma_api::ruma_api; -use ruma_common::encryption::DeviceKeys; +use ruma_common::encryption::IncomingDeviceKeys; use ruma_identifiers::{DeviceIdBox, UserId}; use serde_json::Value as JsonValue; @@ -20,6 +20,8 @@ ruma_api! { requires_authentication: true, } + #[derive(Default)] + #[non_exhaustive] request: { /// The time (in milliseconds) to wait when downloading keys from remote /// servers. 10 seconds is the recommended default. @@ -43,6 +45,8 @@ ruma_api! { pub token: Option<&'a str>, } + #[derive(Default)] + #[non_exhaustive] response: { /// If any remote homeservers could not be reached, they are recorded /// here. The names of the properties are the names of the unreachable @@ -52,7 +56,7 @@ ruma_api! { /// Information on the queried devices. #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] - pub device_keys: BTreeMap>, + pub device_keys: BTreeMap>, /// Information on the master cross-signing keys of the queried users. #[cfg(feature = "unstable-pre-spec")] @@ -72,3 +76,17 @@ ruma_api! { error: crate::Error } + +impl Request<'_> { + /// Creates an empty `Request`. + pub fn new() -> Self { + Default::default() + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Default::default() + } +} diff --git a/ruma-client-api/src/r0/keys/upload_keys.rs b/ruma-client-api/src/r0/keys/upload_keys.rs index 61457177..257ac1ef 100644 --- a/ruma-client-api/src/r0/keys/upload_keys.rs +++ b/ruma-client-api/src/r0/keys/upload_keys.rs @@ -4,7 +4,7 @@ use std::collections::BTreeMap; use js_int::UInt; use ruma_api::ruma_api; -use ruma_common::encryption::DeviceKeys; +use ruma_common::encryption::{DeviceKeys, IncomingDeviceKeys}; use ruma_identifiers::{DeviceKeyAlgorithm, DeviceKeyId}; use super::OneTimeKey; @@ -19,16 +19,19 @@ ruma_api! { requires_authentication: true, } + #[derive(Default)] + #[non_exhaustive] request: { /// Identity keys for the device. May be absent if no new identity keys are required. #[serde(skip_serializing_if = "Option::is_none")] - pub device_keys: Option, + pub device_keys: Option>, /// One-time public keys for "pre-key" messages. #[serde(skip_serializing_if = "Option::is_none")] pub one_time_keys: Option>, } + #[non_exhaustive] response: { /// For each key algorithm, the number of unclaimed one-time keys of that /// type currently held on the server for this device. @@ -37,3 +40,17 @@ ruma_api! { error: crate::Error } + +impl Request<'_> { + /// Creates an empty `Request`. + pub fn new() -> Self { + Default::default() + } +} + +impl Response { + /// Creates a new `Response` with the given one time key counts. + pub fn new(one_time_key_counts: BTreeMap) -> Self { + Self { one_time_key_counts } + } +} diff --git a/ruma-client-api/src/r0/keys/upload_signatures.rs b/ruma-client-api/src/r0/keys/upload_signatures.rs index acd8c9c8..4a2d4c75 100644 --- a/ruma-client-api/src/r0/keys/upload_signatures.rs +++ b/ruma-client-api/src/r0/keys/upload_signatures.rs @@ -15,13 +15,30 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// Signed keys. #[ruma_api(body)] pub signed_keys: BTreeMap>, } + #[derive(Default)] + #[non_exhaustive] response: {} error: crate::Error } + +impl Request { + /// Creates a new `Request` with the given signed keys. + pub fn new(signed_keys: BTreeMap>) -> Self { + Self { signed_keys } + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self + } +} diff --git a/ruma-client-api/src/r0/keys/upload_signing_keys.rs b/ruma-client-api/src/r0/keys/upload_signing_keys.rs index c250c3e0..599dc91b 100644 --- a/ruma-client-api/src/r0/keys/upload_signing_keys.rs +++ b/ruma-client-api/src/r0/keys/upload_signing_keys.rs @@ -15,6 +15,8 @@ ruma_api! { requires_authentication: true, } + #[derive(Default)] + #[non_exhaustive] request: { /// Additional authentication information for the user-interactive authentication API. #[serde(skip_serializing_if = "Option::is_none")] @@ -35,7 +37,23 @@ ruma_api! { pub user_signing_key: Option, } + #[derive(Default)] + #[non_exhaustive] response: {} error: crate::Error } + +impl Request<'_> { + /// Creates an empty `Request`. + pub fn new() -> Self { + Default::default() + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self + } +} diff --git a/ruma-client-api/src/r0/push.rs b/ruma-client-api/src/r0/push.rs index c4cc6971..fe535ceb 100644 --- a/ruma-client-api/src/r0/push.rs +++ b/ruma-client-api/src/r0/push.rs @@ -22,6 +22,7 @@ pub mod set_pushrule_enabled; #[derive( Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Display, EnumString, )] +#[non_exhaustive] #[serde(rename_all = "snake_case")] #[strum(serialize_all = "snake_case")] pub enum RuleKind { diff --git a/ruma-client-api/src/r0/session/login.rs b/ruma-client-api/src/r0/session/login.rs index 1aaf2182..25310aff 100644 --- a/ruma-client-api/src/r0/session/login.rs +++ b/ruma-client-api/src/r0/session/login.rs @@ -15,6 +15,7 @@ ruma_api! { requires_authentication: false, } + #[non_exhaustive] request: { /// Identification information for the user. #[serde(flatten)] @@ -34,6 +35,7 @@ ruma_api! { pub initial_device_display_name: Option<&'a str>, } + #[non_exhaustive] response: { /// The fully-qualified Matrix ID that has been registered. pub user_id: UserId, @@ -63,6 +65,20 @@ ruma_api! { error: crate::Error } +impl<'a> Request<'a> { + /// Creates a new `Request` with the given user and login info. + pub fn new(user: UserInfo<'a>, login_info: LoginInfo<'a>) -> Self { + Self { user, login_info, device_id: None, initial_device_display_name: None } + } +} + +impl Response { + /// Creates a new `Response` with the given user ID, access token and device ID. + pub fn new(user_id: UserId, access_token: String, device_id: DeviceIdBox) -> Self { + Self { user_id, access_token, home_server: None, device_id, well_known: None } + } +} + /// Identification information for the user. #[derive(Clone, Copy, Debug, PartialEq, Eq, Outgoing, Serialize)] #[serde(from = "user_serde::IncomingUserInfo", into = "user_serde::UserInfo")] diff --git a/ruma-client-api/src/r0/to_device/send_event_to_device.rs b/ruma-client-api/src/r0/to_device/send_event_to_device.rs index 5e991ba7..ea6a61a3 100644 --- a/ruma-client-api/src/r0/to_device/send_event_to_device.rs +++ b/ruma-client-api/src/r0/to_device/send_event_to_device.rs @@ -19,6 +19,7 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// Type of event being sent to each device. #[ruma_api(path)] @@ -37,7 +38,27 @@ ruma_api! { pub messages: BTreeMap>> } + #[derive(Default)] + #[non_exhaustive] response: {} error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given event type, transaction ID and messages. + pub fn new( + event_type: EventType, + txn_id: &'a str, + messages: BTreeMap>>, + ) -> Self { + Self { event_type, txn_id, messages } + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self + } +} diff --git a/ruma-client/src/lib.rs b/ruma-client/src/lib.rs index f086e3e7..59c7a0c3 100644 --- a/ruma-client/src/lib.rs +++ b/ruma-client/src/lib.rs @@ -214,15 +214,15 @@ where device_id: Option<&DeviceId>, initial_device_display_name: Option<&str>, ) -> Result> { - use ruma_client_api::r0::session::login; + use ruma_client_api::r0::session::login::{LoginInfo, Request as LoginRequest, UserInfo}; let response = self - .request(login::Request { - user: login::UserInfo::MatrixId(user), - login_info: login::LoginInfo::Password { password }, - device_id, - initial_device_display_name, - }) + .request(assign!( + LoginRequest::new(UserInfo::MatrixId(user), LoginInfo::Password { password }), { + device_id, + initial_device_display_name, + } + )) .await?; let session = Session { diff --git a/ruma-common/src/encryption.rs b/ruma-common/src/encryption.rs index e5550fb2..7b13cca8 100644 --- a/ruma-common/src/encryption.rs +++ b/ruma-common/src/encryption.rs @@ -4,21 +4,24 @@ use std::collections::BTreeMap; -use ruma_identifiers::{DeviceIdBox, DeviceKeyId, EventEncryptionAlgorithm, UserId}; -use serde::{Deserialize, Serialize}; +use ruma_api::Outgoing; +use ruma_identifiers::{DeviceId, DeviceKeyId, EventEncryptionAlgorithm, UserId}; +use ruma_serde::CanBeEmpty; +use serde::Serialize; /// Identity keys for a device. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Outgoing, Serialize)] #[non_exhaustive] -pub struct DeviceKeys { +#[incoming_derive(Clone, Serialize)] +pub struct DeviceKeys<'a> { /// The ID of the user the device belongs to. Must match the user ID used when logging in. - pub user_id: UserId, + pub user_id: &'a UserId, /// The ID of the device these keys belong to. Must match the device ID used when logging in. - pub device_id: DeviceIdBox, + pub device_id: &'a DeviceId, /// The encryption algorithms supported by this device. - pub algorithms: Vec, + pub algorithms: &'a [EventEncryptionAlgorithm], /// Public identity keys. pub keys: BTreeMap, @@ -28,17 +31,17 @@ pub struct DeviceKeys { /// Additional data added to the device key information by intermediate servers, and /// not covered by the signatures. - #[serde(skip_serializing_if = "UnsignedDeviceInfo::is_empty")] - pub unsigned: UnsignedDeviceInfo, + #[serde(skip_serializing_if = "ruma_serde::is_empty")] + pub unsigned: UnsignedDeviceInfo<'a>, } -impl DeviceKeys { +impl<'a> DeviceKeys<'a> { /// Creates a new `DeviceKeys` from the given user id, device id, algorithms, keys and /// signatures. pub fn new( - user_id: UserId, - device_id: DeviceIdBox, - algorithms: Vec, + user_id: &'a UserId, + device_id: &'a DeviceId, + algorithms: &'a [EventEncryptionAlgorithm], keys: BTreeMap, signatures: BTreeMap>, ) -> Self { @@ -47,14 +50,16 @@ impl DeviceKeys { } /// Additional data added to device key information by intermediate servers. -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub struct UnsignedDeviceInfo { +#[derive(Clone, Debug, Default, Outgoing, Serialize)] +#[non_exhaustive] +#[incoming_derive(Clone, Serialize)] +pub struct UnsignedDeviceInfo<'a> { /// The display name which the user set on the device. #[serde(skip_serializing_if = "Option::is_none")] - pub device_display_name: Option, + pub device_display_name: Option<&'a str>, } -impl UnsignedDeviceInfo { +impl UnsignedDeviceInfo<'_> { /// Creates an empty `UnsignedDeviceInfo`. pub fn new() -> Self { Default::default() @@ -65,3 +70,22 @@ impl UnsignedDeviceInfo { self.device_display_name.is_none() } } + +impl IncomingUnsignedDeviceInfo { + /// Checks whether all fields are empty / `None`. + pub fn is_empty(&self) -> bool { + self.device_display_name.is_none() + } +} + +impl CanBeEmpty for UnsignedDeviceInfo<'_> { + fn is_empty(&self) -> bool { + self.is_empty() + } +} + +impl CanBeEmpty for IncomingUnsignedDeviceInfo { + fn is_empty(&self) -> bool { + self.is_empty() + } +} diff --git a/ruma-federation-api/src/device/get_devices/v1.rs b/ruma-federation-api/src/device/get_devices/v1.rs index 7f62a5da..8dd9c03f 100644 --- a/ruma-federation-api/src/device/get_devices/v1.rs +++ b/ruma-federation-api/src/device/get_devices/v1.rs @@ -2,7 +2,7 @@ use js_int::UInt; use ruma_api::ruma_api; -use ruma_common::encryption::DeviceKeys; +use ruma_common::encryption::IncomingDeviceKeys; use ruma_identifiers::{DeviceIdBox, UserId}; use serde::{Deserialize, Serialize}; @@ -62,7 +62,7 @@ pub struct UserDevice { pub device_id: DeviceIdBox, /// Identity keys for the device. - pub keys: DeviceKeys, + pub keys: IncomingDeviceKeys, /// Optional display name for the device #[serde(skip_serializing_if = "Option::is_none")] @@ -71,7 +71,7 @@ pub struct UserDevice { impl UserDevice { /// Creates a new `UserDevice` with the given device id and keys. - pub fn new(device_id: DeviceIdBox, keys: DeviceKeys) -> Self { + pub fn new(device_id: DeviceIdBox, keys: IncomingDeviceKeys) -> Self { Self { device_id, keys, device_display_name: None } } } diff --git a/ruma-federation-api/src/keys/get_keys/v1.rs b/ruma-federation-api/src/keys/get_keys/v1.rs index df346b95..b136592a 100644 --- a/ruma-federation-api/src/keys/get_keys/v1.rs +++ b/ruma-federation-api/src/keys/get_keys/v1.rs @@ -3,7 +3,7 @@ use std::collections::BTreeMap; use ruma_api::ruma_api; -use ruma_common::encryption::DeviceKeys; +use ruma_common::encryption::IncomingDeviceKeys; use ruma_identifiers::{DeviceIdBox, UserId}; ruma_api! { @@ -26,7 +26,7 @@ ruma_api! { #[non_exhaustive] response: { /// Keys from the queried devices. - pub device_keys: BTreeMap>, + pub device_keys: BTreeMap>, } } @@ -39,7 +39,7 @@ impl Request { impl Response { /// Creates a new `Response` with the given device keys. - pub fn new(device_keys: BTreeMap>) -> Self { + pub fn new(device_keys: BTreeMap>) -> Self { Self { device_keys } } }