Remove borrowing from types in ruma_common::encryption

Until a better solution can be found for types that appear both in
request & response types and would otherwise require incoming types to
appear as fields of outgoing types.
This commit is contained in:
Jonas Platte 2020-09-12 02:50:57 +02:00
parent eab5368edd
commit 4a9b1aeb3c
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
5 changed files with 28 additions and 51 deletions

View File

@ -3,7 +3,7 @@
use std::{collections::BTreeMap, time::Duration};
use ruma_api::ruma_api;
use ruma_common::encryption::IncomingDeviceKeys;
use ruma_common::encryption::DeviceKeys;
use ruma_identifiers::{DeviceIdBox, UserId};
use serde_json::Value as JsonValue;
@ -54,7 +54,7 @@ ruma_api! {
/// Information on the queried devices.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub device_keys: BTreeMap<UserId, BTreeMap<DeviceIdBox, IncomingDeviceKeys>>,
pub device_keys: BTreeMap<UserId, BTreeMap<DeviceIdBox, DeviceKeys>>,
/// Information on the master cross-signing keys of the queried users.
#[cfg(feature = "unstable-pre-spec")]

View File

@ -4,7 +4,7 @@ use std::collections::BTreeMap;
use js_int::UInt;
use ruma_api::ruma_api;
use ruma_common::encryption::{DeviceKeys, IncomingDeviceKeys};
use ruma_common::encryption::DeviceKeys;
use ruma_identifiers::{DeviceKeyAlgorithm, DeviceKeyId};
use super::OneTimeKey;
@ -23,7 +23,7 @@ ruma_api! {
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<DeviceKeys<'a>>,
pub device_keys: Option<DeviceKeys>,
/// One-time public keys for "pre-key" messages.
#[serde(skip_serializing_if = "Option::is_none")]
@ -39,7 +39,7 @@ ruma_api! {
error: crate::Error
}
impl Request<'_> {
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Default::default()

View File

@ -4,24 +4,21 @@
use std::collections::BTreeMap;
use ruma_api::Outgoing;
use ruma_identifiers::{DeviceId, DeviceKeyId, EventEncryptionAlgorithm, UserId};
use ruma_serde::CanBeEmpty;
use serde::Serialize;
use ruma_identifiers::{DeviceIdBox, DeviceKeyId, EventEncryptionAlgorithm, UserId};
use serde::{Deserialize, Serialize};
/// Identity keys for a device.
#[derive(Clone, Debug, Outgoing, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[incoming_derive(Clone, Serialize)]
pub struct DeviceKeys<'a> {
pub struct DeviceKeys {
/// The ID of the user the device belongs to. Must match the user ID used when logging in.
pub user_id: &'a UserId,
pub user_id: UserId,
/// The ID of the device these keys belong to. Must match the device ID used when logging in.
pub device_id: &'a DeviceId,
pub device_id: DeviceIdBox,
/// The encryption algorithms supported by this device.
pub algorithms: &'a [EventEncryptionAlgorithm],
pub algorithms: Vec<EventEncryptionAlgorithm>,
/// Public identity keys.
pub keys: BTreeMap<DeviceKeyId, String>,
@ -31,17 +28,17 @@ pub struct DeviceKeys<'a> {
/// Additional data added to the device key information by intermediate servers, and
/// not covered by the signatures.
#[serde(skip_serializing_if = "ruma_serde::is_empty")]
pub unsigned: UnsignedDeviceInfo<'a>,
#[serde(skip_serializing_if = "UnsignedDeviceInfo::is_empty")]
pub unsigned: UnsignedDeviceInfo,
}
impl<'a> DeviceKeys<'a> {
impl DeviceKeys {
/// Creates a new `DeviceKeys` from the given user id, device id, algorithms, keys and
/// signatures.
pub fn new(
user_id: &'a UserId,
device_id: &'a DeviceId,
algorithms: &'a [EventEncryptionAlgorithm],
user_id: UserId,
device_id: DeviceIdBox,
algorithms: Vec<EventEncryptionAlgorithm>,
keys: BTreeMap<DeviceKeyId, String>,
signatures: BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>,
) -> Self {
@ -50,16 +47,15 @@ impl<'a> DeviceKeys<'a> {
}
/// Additional data added to device key information by intermediate servers.
#[derive(Clone, Debug, Default, Outgoing, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[incoming_derive(Clone, Serialize)]
pub struct UnsignedDeviceInfo<'a> {
pub struct UnsignedDeviceInfo {
/// The display name which the user set on the device.
#[serde(skip_serializing_if = "Option::is_none")]
pub device_display_name: Option<&'a str>,
pub device_display_name: Option<String>,
}
impl UnsignedDeviceInfo<'_> {
impl UnsignedDeviceInfo {
/// Creates an empty `UnsignedDeviceInfo`.
pub fn new() -> Self {
Default::default()
@ -70,22 +66,3 @@ 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()
}
}

View File

@ -2,7 +2,7 @@
use js_int::UInt;
use ruma_api::ruma_api;
use ruma_common::encryption::IncomingDeviceKeys;
use ruma_common::encryption::DeviceKeys;
use ruma_identifiers::{DeviceIdBox, UserId};
use serde::{Deserialize, Serialize};
@ -60,7 +60,7 @@ pub struct UserDevice {
pub device_id: DeviceIdBox,
/// Identity keys for the device.
pub keys: IncomingDeviceKeys,
pub keys: DeviceKeys,
/// Optional display name for the device
#[serde(skip_serializing_if = "Option::is_none")]
@ -69,7 +69,7 @@ pub struct UserDevice {
impl UserDevice {
/// Creates a new `UserDevice` with the given device id and keys.
pub fn new(device_id: DeviceIdBox, keys: IncomingDeviceKeys) -> Self {
pub fn new(device_id: DeviceIdBox, keys: DeviceKeys) -> Self {
Self { device_id, keys, device_display_name: None }
}
}

View File

@ -3,7 +3,7 @@
use std::collections::BTreeMap;
use ruma_api::ruma_api;
use ruma_common::encryption::IncomingDeviceKeys;
use ruma_common::encryption::DeviceKeys;
use ruma_identifiers::{DeviceIdBox, UserId};
ruma_api! {
@ -24,7 +24,7 @@ ruma_api! {
response: {
/// Keys from the queried devices.
pub device_keys: BTreeMap<UserId, BTreeMap<DeviceIdBox, IncomingDeviceKeys>>,
pub device_keys: BTreeMap<UserId, BTreeMap<DeviceIdBox, DeviceKeys>>,
}
}
@ -37,7 +37,7 @@ impl Request {
impl Response {
/// Creates a new `Response` with the given device keys.
pub fn new(device_keys: BTreeMap<UserId, BTreeMap<DeviceIdBox, IncomingDeviceKeys>>) -> Self {
pub fn new(device_keys: BTreeMap<UserId, BTreeMap<DeviceIdBox, DeviceKeys>>) -> Self {
Self { device_keys }
}
}