client-api: Update backup endpoints to the new API standards

This commit is contained in:
Jonas Platte 2020-08-29 22:37:06 +02:00
parent fec07a7426
commit cf9b83495c
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
7 changed files with 137 additions and 27 deletions

View File

@ -8,10 +8,20 @@ pub mod get_latest_backup;
pub mod update_backup; pub mod update_backup;
use js_int::UInt; use js_int::UInt;
use ruma_identifiers::{DeviceKeyId, UserId}; use ruma_identifiers::{DeviceKeyId, RoomId, UserId};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::BTreeMap; 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<RoomId, BTreeMap<String, KeyData>>;
/// A map from room IDs to session IDs to key data.
#[cfg(feature = "unstable-synapse-quirks")]
pub type Rooms = BTreeMap<RoomId, Sessions>;
// TODO: remove // TODO: remove
/// A wrapper around a mapping of session IDs to key data. /// A wrapper around a mapping of session IDs to key data.
#[cfg(feature = "unstable-synapse-quirks")] #[cfg(feature = "unstable-synapse-quirks")]
@ -30,6 +40,7 @@ pub enum BackupAlgorithm {
MegolmBackupV1Curve25519AesSha2 { MegolmBackupV1Curve25519AesSha2 {
/// The curve25519 public key used to encrypt the backups, encoded in unpadded base64. /// The curve25519 public key used to encrypt the backups, encoded in unpadded base64.
public_key: String, public_key: String,
/// Signatures of the auth_data as Signed JSON. /// Signatures of the auth_data as Signed JSON.
signatures: BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>, signatures: BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>,
}, },
@ -40,10 +51,13 @@ pub enum BackupAlgorithm {
pub struct KeyData { pub struct KeyData {
/// The index of the first message in the session that the key can decrypt. /// The index of the first message in the session that the key can decrypt.
pub first_message_index: UInt, pub first_message_index: UInt,
/// The number of times this key has been forwarded via key-sharing between devices. /// The number of times this key has been forwarded via key-sharing between devices.
pub forwarded_count: UInt, pub forwarded_count: UInt,
/// Whether the device backing up the key verified the device that the key is from. /// Whether the device backing up the key verified the device that the key is from.
pub is_verified: bool, pub is_verified: bool,
/// Data about the session. /// Data about the session.
pub session_data: SessionData, pub session_data: SessionData,
} }
@ -53,8 +67,10 @@ pub struct KeyData {
pub struct SessionData { pub struct SessionData {
/// Unpadded base64-encoded public half of the ephemeral key. /// Unpadded base64-encoded public half of the ephemeral key.
pub ephemeral: String, pub ephemeral: String,
/// Ciphertext, encrypted using AES-CBC-256 with PKCS#7 padding, encoded in base64. /// Ciphertext, encrypted using AES-CBC-256 with PKCS#7 padding, encoded in base64.
pub ciphertext: String, pub ciphertext: String,
/// First 8 bytes of MAC key, encoded in base64. /// First 8 bytes of MAC key, encoded in base64.
pub mac: String, pub mac: String,
} }

View File

@ -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) //! [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 js_int::UInt;
use ruma_api::ruma_api; use ruma_api::ruma_api;
use ruma_identifiers::RoomId;
use super::Rooms;
ruma_api! { ruma_api! {
metadata: { metadata: {
@ -16,22 +15,19 @@ ruma_api! {
requires_authentication: true, requires_authentication: true,
} }
#[non_exhaustive]
request: { request: {
/// The backup version. Must be the current backup. /// The backup version. Must be the current backup.
#[ruma_api(query)] #[ruma_api(query)]
pub version: String, pub version: &'a str,
/// A map from room IDs to session IDs to key data. /// A map from room IDs to session IDs to key data.
/// ///
/// Note: synapse has the `sessions: {}` wrapper, the Matrix spec does not. /// Note: synapse has the `sessions: {}` wrapper, the Matrix spec does not.
#[cfg(feature = "unstable-synapse-quirks")] pub rooms: Rooms,
pub rooms: BTreeMap<RoomId, super::Sessions>,
/// A map from room IDs to session IDs to key data.
#[cfg(not(feature = "unstable-synapse-quirks"))]
pub rooms: BTreeMap<RoomId, BTreeMap<String, super::KeyData>>,
} }
#[non_exhaustive]
response: { response: {
/// An opaque string representing stored keys in the backup. Clients can compare it with /// 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. /// the etag value they received in the request of their last key storage request.
@ -43,3 +39,17 @@ ruma_api! {
error: crate::Error 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 }
}
}

View File

@ -2,6 +2,8 @@
use ruma_api::ruma_api; use ruma_api::ruma_api;
use super::BackupAlgorithm;
ruma_api! { ruma_api! {
metadata: { metadata: {
description: "Creates a new backup.", description: "Creates a new backup.",
@ -12,12 +14,14 @@ ruma_api! {
requires_authentication: true, requires_authentication: true,
} }
#[non_exhaustive]
request: { request: {
/// The algorithm used for storing backups. /// The algorithm used for storing backups.
#[serde(flatten)] #[serde(flatten)]
pub algorithm: super::BackupAlgorithm, pub algorithm: BackupAlgorithm,
} }
#[non_exhaustive]
response: { response: {
/// The backup version. This is an opaque string. /// The backup version. This is an opaque string.
pub version: String, pub version: String,
@ -25,3 +29,17 @@ ruma_api! {
error: crate::Error 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 }
}
}

View File

@ -3,6 +3,8 @@
use js_int::UInt; use js_int::UInt;
use ruma_api::ruma_api; use ruma_api::ruma_api;
use super::BackupAlgorithm;
ruma_api! { ruma_api! {
metadata: { metadata: {
description: "Get information about an existing backup.", description: "Get information about an existing backup.",
@ -13,16 +15,18 @@ ruma_api! {
requires_authentication: true, requires_authentication: true,
} }
#[non_exhaustive]
request: { request: {
/// The backup version. /// The backup version.
#[ruma_api(path)] #[ruma_api(path)]
pub version: String, pub version: &'a str,
} }
#[non_exhaustive]
response: { response: {
/// The algorithm used for storing backups. /// The algorithm used for storing backups.
#[serde(flatten)] #[serde(flatten)]
pub algorithm: super::BackupAlgorithm, pub algorithm: BackupAlgorithm,
/// The number of keys stored in the backup. /// The number of keys stored in the backup.
pub count: UInt, pub count: UInt,
@ -37,3 +41,17 @@ ruma_api! {
error: crate::Error 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 }
}
}

View File

@ -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) //! [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_api::ruma_api;
use ruma_identifiers::RoomId;
use super::Rooms;
ruma_api! { ruma_api! {
metadata: { metadata: {
@ -15,23 +14,34 @@ ruma_api! {
requires_authentication: true, requires_authentication: true,
} }
#[non_exhaustive]
request: { request: {
/// The backup version. Must be the current backup. /// The backup version. Must be the current backup.
#[ruma_api(query)] #[ruma_api(query)]
pub version: String, pub version: &'a str,
} }
#[non_exhaustive]
response: { response: {
/// A map from room IDs to session IDs to key data. /// A map from room IDs to session IDs to key data.
/// ///
/// Note: synapse has the `sessions: {}` wrapper, the Matrix spec does not. /// Note: synapse has the `sessions: {}` wrapper, the Matrix spec does not.
#[cfg(feature = "unstable-synapse-quirks")] pub rooms: Rooms,
pub rooms: BTreeMap<RoomId, super::Sessions>,
/// A map from room IDs to session IDs to key data.
#[cfg(not(feature = "unstable-synapse-quirks"))]
pub rooms: BTreeMap<RoomId, BTreeMap<String, super::KeyData>>,
} }
error: crate::Error 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 }
}
}

View File

@ -3,6 +3,8 @@
use js_int::UInt; use js_int::UInt;
use ruma_api::ruma_api; use ruma_api::ruma_api;
use super::BackupAlgorithm;
ruma_api! { ruma_api! {
metadata: { metadata: {
description: "Get information about the latest backup.", description: "Get information about the latest backup.",
@ -13,12 +15,15 @@ ruma_api! {
requires_authentication: true, requires_authentication: true,
} }
#[derive(Default)]
#[non_exhaustive]
request: {} request: {}
#[non_exhaustive]
response: { response: {
/// The algorithm used for storing backups. /// The algorithm used for storing backups.
#[serde(flatten)] #[serde(flatten)]
pub algorithm: super::BackupAlgorithm, pub algorithm: BackupAlgorithm,
/// The number of keys stored in the backup. /// The number of keys stored in the backup.
pub count: UInt, pub count: UInt,
@ -33,3 +38,17 @@ ruma_api! {
error: crate::Error 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 }
}
}

View File

@ -2,6 +2,8 @@
use ruma_api::ruma_api; use ruma_api::ruma_api;
use super::BackupAlgorithm;
ruma_api! { ruma_api! {
metadata: { metadata: {
description: "Update information about an existing backup.", description: "Update information about an existing backup.",
@ -12,17 +14,34 @@ ruma_api! {
requires_authentication: true, requires_authentication: true,
} }
#[non_exhaustive]
request: { request: {
/// The backup version. /// The backup version.
#[ruma_api(path)] #[ruma_api(path)]
pub version: String, pub version: &'a str,
/// The algorithm used for storing backups. /// The algorithm used for storing backups.
#[serde(flatten)] #[serde(flatten)]
pub algorithm: super::BackupAlgorithm, pub algorithm: BackupAlgorithm,
} }
#[derive(Default)]
#[non_exhaustive]
response: {} response: {}
error: crate::Error 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
}
}