federation-api: Improve claim_keys endpoint

This commit is contained in:
Timo Kösters 2021-05-27 23:26:17 +02:00 committed by Jonas Platte
parent 2538b23c26
commit c198cf6e9f
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
6 changed files with 88 additions and 77 deletions

View File

@ -1,76 +1,5 @@
//! Endpoints for client devices to exchange information not persisted in room DAG.
use std::{
convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult},
};
use ruma_identifiers::DeviceIdBox;
use serde::{
de::{self, Unexpected},
Deserialize, Deserializer, Serialize, Serializer,
};
pub use ruma_common::to_device::DeviceIdOrAllDevices;
pub mod send_event_to_device;
/// Represents one or all of a user's devices.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum DeviceIdOrAllDevices {
/// Represents a device Id for one of a user's devices.
DeviceId(DeviceIdBox),
/// Represents all devices for a user.
AllDevices,
}
impl Display for DeviceIdOrAllDevices {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
DeviceIdOrAllDevices::DeviceId(device_id) => write!(f, "{}", device_id.to_string()),
DeviceIdOrAllDevices::AllDevices => write!(f, "*"),
}
}
}
impl From<DeviceIdBox> for DeviceIdOrAllDevices {
fn from(d: DeviceIdBox) -> Self {
DeviceIdOrAllDevices::DeviceId(d)
}
}
impl TryFrom<&str> for DeviceIdOrAllDevices {
type Error = &'static str;
fn try_from(device_id_or_all_devices: &str) -> Result<Self, Self::Error> {
if device_id_or_all_devices.is_empty() {
Err("Device identifier cannot be empty")
} else if "*" == device_id_or_all_devices {
Ok(DeviceIdOrAllDevices::AllDevices)
} else {
Ok(DeviceIdOrAllDevices::DeviceId(device_id_or_all_devices.into()))
}
}
}
impl Serialize for DeviceIdOrAllDevices {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::DeviceId(device_id) => device_id.serialize(serializer),
Self::AllDevices => serializer.serialize_str("*"),
}
}
}
impl<'de> Deserialize<'de> for DeviceIdOrAllDevices {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = ruma_serde::deserialize_cow_str(deserializer)?;
DeviceIdOrAllDevices::try_from(s.as_ref()).map_err(|_| {
de::Error::invalid_value(Unexpected::Str(&s), &"a valid device identifier or '*'")
})
}
}

View File

@ -14,5 +14,6 @@ pub mod push;
pub mod receipt;
pub mod thirdparty;
mod time;
pub mod to_device;
pub use time::{MilliSecondsSinceUnixEpoch, SecondsSinceUnixEpoch};

View File

@ -0,0 +1,76 @@
//! Common types for the Send-To-Device Messaging
//!
//! [send-to-device]: https://matrix.org/docs/spec/client_server/r0.6.1#id70
use std::{
convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult},
};
use ruma_identifiers::DeviceIdBox;
use serde::{
de::{self, Unexpected},
Deserialize, Deserializer, Serialize, Serializer,
};
/// Represents one or all of a user's devices.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum DeviceIdOrAllDevices {
/// Represents a device Id for one of a user's devices.
DeviceId(DeviceIdBox),
/// Represents all devices for a user.
AllDevices,
}
impl Display for DeviceIdOrAllDevices {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
DeviceIdOrAllDevices::DeviceId(device_id) => write!(f, "{}", device_id.to_string()),
DeviceIdOrAllDevices::AllDevices => write!(f, "*"),
}
}
}
impl From<DeviceIdBox> for DeviceIdOrAllDevices {
fn from(d: DeviceIdBox) -> Self {
DeviceIdOrAllDevices::DeviceId(d)
}
}
impl TryFrom<&str> for DeviceIdOrAllDevices {
type Error = &'static str;
fn try_from(device_id_or_all_devices: &str) -> Result<Self, Self::Error> {
if device_id_or_all_devices.is_empty() {
Err("Device identifier cannot be empty")
} else if "*" == device_id_or_all_devices {
Ok(DeviceIdOrAllDevices::AllDevices)
} else {
Ok(DeviceIdOrAllDevices::DeviceId(device_id_or_all_devices.into()))
}
}
}
impl Serialize for DeviceIdOrAllDevices {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::DeviceId(device_id) => device_id.serialize(serializer),
Self::AllDevices => serializer.serialize_str("*"),
}
}
}
impl<'de> Deserialize<'de> for DeviceIdOrAllDevices {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = ruma_serde::deserialize_cow_str(deserializer)?;
DeviceIdOrAllDevices::try_from(s.as_ref()).map_err(|_| {
de::Error::invalid_value(Unexpected::Str(&s), &"a valid device identifier or '*'")
})
}
}

View File

@ -1,5 +1,9 @@
# [unreleased]
Breaking Changes:
* Change types of keys::claim_keys::v1 response to match the client-server endpoint
Improvements:
* Add master_keys and self_signing keys to keys::get_keys::v1 response

View File

@ -4,6 +4,7 @@
use std::collections::BTreeMap;
use ruma_api::ruma_api;
use ruma_common::encryption::OneTimeKey;
use ruma_identifiers::{DeviceIdBox, DeviceKeyAlgorithm, DeviceKeyId, UserId};
use serde::{Deserialize, Serialize};
@ -46,7 +47,7 @@ impl Response {
pub type OneTimeKeyClaims = BTreeMap<UserId, BTreeMap<DeviceIdBox, DeviceKeyAlgorithm>>;
/// One time keys for use in pre-key messages
pub type OneTimeKeys = BTreeMap<UserId, BTreeMap<DeviceIdBox, BTreeMap<DeviceKeyId, KeyObject>>>;
pub type OneTimeKeys = BTreeMap<UserId, BTreeMap<DeviceIdBox, BTreeMap<DeviceKeyId, OneTimeKey>>>;
/// A key and its signature
#[derive(Debug, Clone, Serialize, Deserialize)]

View File

@ -3,10 +3,10 @@
use std::collections::BTreeMap;
use js_int::UInt;
use ruma_common::{encryption::DeviceKeys, presence::PresenceState};
use ruma_events::{
from_raw_json_value, receipt::Receipt, room_key::RoomKeyToDeviceEventContent, EventType,
use ruma_common::{
encryption::DeviceKeys, presence::PresenceState, to_device::DeviceIdOrAllDevices,
};
use ruma_events::{from_raw_json_value, receipt::Receipt, EventType};
use ruma_identifiers::{DeviceIdBox, EventId, RoomId, UserId};
use serde::{de, Deserialize, Serialize};
use serde_json::{value::RawValue as RawJsonValue, Value as JsonValue};
@ -258,7 +258,7 @@ pub struct DirectDeviceContent {
/// The contents of the messages to be sent. These are arranged in a map
/// of user IDs to a map of device IDs to message bodies. The device ID may
/// also be *, meaning all known devices for the user.
pub messages: BTreeMap<UserId, BTreeMap<DeviceIdBox, RoomKeyToDeviceEventContent>>,
pub messages: BTreeMap<UserId, BTreeMap<DeviceIdOrAllDevices, Box<RawJsonValue>>>,
}
impl DirectDeviceContent {