diff --git a/ruma-client-api/src/r0/backup.rs b/ruma-client-api/src/r0/backup.rs index 7b5c8044..4427c7c9 100644 --- a/ruma-client-api/src/r0/backup.rs +++ b/ruma-client-api/src/r0/backup.rs @@ -8,10 +8,20 @@ pub mod get_latest_backup; pub mod update_backup; use js_int::UInt; -use ruma_identifiers::{DeviceKeyId, UserId}; +use ruma_identifiers::{DeviceKeyId, RoomId, UserId}; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; +/// A map from room IDs to session IDs to key data. +/// +/// Note: synapse has the `sessions: {}` wrapper, the Matrix spec does not. +#[cfg(not(feature = "unstable-synapse-quirks"))] +pub type Rooms = BTreeMap>; + +/// A map from room IDs to session IDs to key data. +#[cfg(feature = "unstable-synapse-quirks")] +pub type Rooms = BTreeMap; + // TODO: remove /// A wrapper around a mapping of session IDs to key data. #[cfg(feature = "unstable-synapse-quirks")] @@ -30,6 +40,7 @@ pub enum BackupAlgorithm { MegolmBackupV1Curve25519AesSha2 { /// The curve25519 public key used to encrypt the backups, encoded in unpadded base64. public_key: String, + /// Signatures of the auth_data as Signed JSON. signatures: BTreeMap>, }, @@ -40,10 +51,13 @@ pub enum BackupAlgorithm { pub struct KeyData { /// The index of the first message in the session that the key can decrypt. pub first_message_index: UInt, + /// The number of times this key has been forwarded via key-sharing between devices. pub forwarded_count: UInt, + /// Whether the device backing up the key verified the device that the key is from. pub is_verified: bool, + /// Data about the session. pub session_data: SessionData, } @@ -53,8 +67,10 @@ pub struct KeyData { pub struct SessionData { /// Unpadded base64-encoded public half of the ephemeral key. pub ephemeral: String, + /// Ciphertext, encrypted using AES-CBC-256 with PKCS#7 padding, encoded in base64. pub ciphertext: String, + /// First 8 bytes of MAC key, encoded in base64. pub mac: String, } diff --git a/ruma-client-api/src/r0/backup/add_backup_keys.rs b/ruma-client-api/src/r0/backup/add_backup_keys.rs index 8ab6537c..992ed4f1 100644 --- a/ruma-client-api/src/r0/backup/add_backup_keys.rs +++ b/ruma-client-api/src/r0/backup/add_backup_keys.rs @@ -1,10 +1,9 @@ //! [PUT /_matrix/client/r0/room_keys/keys](https://matrix.org/docs/spec/client_server/unstable#put-matrix-client-r0-room-keys-keys) -use std::collections::BTreeMap; - use js_int::UInt; use ruma_api::ruma_api; -use ruma_identifiers::RoomId; + +use super::Rooms; ruma_api! { metadata: { @@ -16,22 +15,19 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The backup version. Must be the current backup. #[ruma_api(query)] - pub version: String, + pub version: &'a str, /// A map from room IDs to session IDs to key data. /// /// Note: synapse has the `sessions: {}` wrapper, the Matrix spec does not. - #[cfg(feature = "unstable-synapse-quirks")] - pub rooms: BTreeMap, - - /// A map from room IDs to session IDs to key data. - #[cfg(not(feature = "unstable-synapse-quirks"))] - pub rooms: BTreeMap>, + pub rooms: Rooms, } + #[non_exhaustive] response: { /// An opaque string representing stored keys in the backup. Clients can compare it with /// the etag value they received in the request of their last key storage request. @@ -43,3 +39,17 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given version. + pub fn new(version: &'a str, rooms: Rooms) -> Self { + Self { version, rooms } + } +} + +impl Response { + /// Creates a new `Response` with the given etag and key count. + pub fn new(etag: String, count: UInt) -> Self { + Self { etag, count } + } +} diff --git a/ruma-client-api/src/r0/backup/create_backup.rs b/ruma-client-api/src/r0/backup/create_backup.rs index 1e91c526..5bec33b2 100644 --- a/ruma-client-api/src/r0/backup/create_backup.rs +++ b/ruma-client-api/src/r0/backup/create_backup.rs @@ -2,6 +2,8 @@ use ruma_api::ruma_api; +use super::BackupAlgorithm; + ruma_api! { metadata: { description: "Creates a new backup.", @@ -12,12 +14,14 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The algorithm used for storing backups. #[serde(flatten)] - pub algorithm: super::BackupAlgorithm, + pub algorithm: BackupAlgorithm, } + #[non_exhaustive] response: { /// The backup version. This is an opaque string. pub version: String, @@ -25,3 +29,17 @@ ruma_api! { error: crate::Error } + +impl Request { + /// Creates a new `Request` with the given backup algorithm. + pub fn new(algorithm: BackupAlgorithm) -> Self { + Self { algorithm } + } +} + +impl Response { + /// Creates a new `Response` with the given version. + pub fn new(version: String) -> Self { + Self { version } + } +} diff --git a/ruma-client-api/src/r0/backup/get_backup.rs b/ruma-client-api/src/r0/backup/get_backup.rs index 8affef51..cc0a74c4 100644 --- a/ruma-client-api/src/r0/backup/get_backup.rs +++ b/ruma-client-api/src/r0/backup/get_backup.rs @@ -3,6 +3,8 @@ use js_int::UInt; use ruma_api::ruma_api; +use super::BackupAlgorithm; + ruma_api! { metadata: { description: "Get information about an existing backup.", @@ -13,16 +15,18 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The backup version. #[ruma_api(path)] - pub version: String, + pub version: &'a str, } + #[non_exhaustive] response: { /// The algorithm used for storing backups. #[serde(flatten)] - pub algorithm: super::BackupAlgorithm, + pub algorithm: BackupAlgorithm, /// The number of keys stored in the backup. pub count: UInt, @@ -37,3 +41,17 @@ ruma_api! { error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given version. + pub fn new(version: &'a str) -> Self { + Self { version } + } +} + +impl Response { + /// Creates a new `Response` with the gien algorithm, key count, etag and version. + pub fn new(algorithm: BackupAlgorithm, count: UInt, etag: String, version: String) -> Self { + Self { algorithm, count, etag, version } + } +} diff --git a/ruma-client-api/src/r0/backup/get_backup_keys.rs b/ruma-client-api/src/r0/backup/get_backup_keys.rs index 0fd70dd3..ae273a87 100644 --- a/ruma-client-api/src/r0/backup/get_backup_keys.rs +++ b/ruma-client-api/src/r0/backup/get_backup_keys.rs @@ -1,9 +1,8 @@ //! [GET /_matrix/client/r0/room_keys/keys](https://matrix.org/docs/spec/client_server/unstable#get-matrix-client-r0-room-keys-keys) -use std::collections::BTreeMap; - use ruma_api::ruma_api; -use ruma_identifiers::RoomId; + +use super::Rooms; ruma_api! { metadata: { @@ -15,23 +14,34 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The backup version. Must be the current backup. #[ruma_api(query)] - pub version: String, + pub version: &'a str, } + #[non_exhaustive] response: { /// A map from room IDs to session IDs to key data. /// /// Note: synapse has the `sessions: {}` wrapper, the Matrix spec does not. - #[cfg(feature = "unstable-synapse-quirks")] - pub rooms: BTreeMap, - - /// A map from room IDs to session IDs to key data. - #[cfg(not(feature = "unstable-synapse-quirks"))] - pub rooms: BTreeMap>, + pub rooms: Rooms, } error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given version. + pub fn new(version: &'a str) -> Self { + Self { version } + } +} + +impl Response { + /// Creates a new `Response` with the given rooms. + pub fn new(rooms: Rooms) -> Self { + Self { rooms } + } +} diff --git a/ruma-client-api/src/r0/backup/get_latest_backup.rs b/ruma-client-api/src/r0/backup/get_latest_backup.rs index 385caee9..d30c3541 100644 --- a/ruma-client-api/src/r0/backup/get_latest_backup.rs +++ b/ruma-client-api/src/r0/backup/get_latest_backup.rs @@ -3,6 +3,8 @@ use js_int::UInt; use ruma_api::ruma_api; +use super::BackupAlgorithm; + ruma_api! { metadata: { description: "Get information about the latest backup.", @@ -13,12 +15,15 @@ ruma_api! { requires_authentication: true, } + #[derive(Default)] + #[non_exhaustive] request: {} + #[non_exhaustive] response: { /// The algorithm used for storing backups. #[serde(flatten)] - pub algorithm: super::BackupAlgorithm, + pub algorithm: BackupAlgorithm, /// The number of keys stored in the backup. pub count: UInt, @@ -33,3 +38,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 algorithm, key count, etag and version. + pub fn new(algorithm: BackupAlgorithm, count: UInt, etag: String, version: String) -> Self { + Self { algorithm, count, etag, version } + } +} diff --git a/ruma-client-api/src/r0/backup/update_backup.rs b/ruma-client-api/src/r0/backup/update_backup.rs index 09cc9079..239c2c42 100644 --- a/ruma-client-api/src/r0/backup/update_backup.rs +++ b/ruma-client-api/src/r0/backup/update_backup.rs @@ -2,6 +2,8 @@ use ruma_api::ruma_api; +use super::BackupAlgorithm; + ruma_api! { metadata: { description: "Update information about an existing backup.", @@ -12,17 +14,34 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The backup version. #[ruma_api(path)] - pub version: String, + pub version: &'a str, /// The algorithm used for storing backups. #[serde(flatten)] - pub algorithm: super::BackupAlgorithm, + pub algorithm: BackupAlgorithm, } + #[derive(Default)] + #[non_exhaustive] response: {} error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given backup version and algorithm. + pub fn new(version: &'a str, algorithm: BackupAlgorithm) -> Self { + Self { version, algorithm } + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self + } +}