Convert m.key.verification.{accept,cancel,key,mac,request} and

m.room_key to the new API.
This commit is contained in:
Jimmy Cuadra 2019-06-20 17:20:03 -07:00
parent 3c70dac634
commit 20d2482108
11 changed files with 209 additions and 177 deletions

View File

@ -37,9 +37,7 @@ mod tests {
content.insert(alice.clone(), room.clone());
let event = DirectEvent {
content,
};
let event = DirectEvent { content };
assert_eq!(
to_string(&event).unwrap(),

View File

@ -30,9 +30,7 @@ mod tests {
#[test]
fn serialization() {
let dummy_event = DummyEvent {
content: Empty,
};
let dummy_event = DummyEvent { content: Empty };
let actual = serde_json::to_string(&dummy_event).unwrap();
let expected = r#"{"content":{},"type":"m.dummy"}"#;

View File

@ -5,7 +5,7 @@ use std::collections::HashMap;
use ruma_identifiers::UserId;
use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer};
use crate::{Empty, Event};
use crate::{Empty, Event, EventType};
/// A list of users to ignore.
#[derive(Clone, Debug, PartialEq)]
@ -37,7 +37,7 @@ impl IgnoredUserListEvent {
impl Serialize for IgnoredUserListEvent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
S: Serializer,
{
let mut state = serializer.serialize_struct("IgnoredUserListEvent", 2)?;
@ -49,9 +49,6 @@ impl Serialize for IgnoredUserListEvent {
}
impl crate::Event for IgnoredUserListEvent {
/// The type of the event.
const EVENT_TYPE: crate::EventType = crate::EventType::IgnoredUserList;
/// The type of this event's `content` field.
type Content = IgnoredUserListEventContent;
@ -59,12 +56,17 @@ impl crate::Event for IgnoredUserListEvent {
fn content(&self) -> &Self::Content {
&self.content
}
/// The type of the event.
fn event_type(&self) -> EventType {
EventType::IgnoredUserList
}
}
impl Serialize for IgnoredUserListEventContent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
S: Serializer,
{
let mut map = HashMap::new();
@ -72,17 +74,15 @@ impl Serialize for IgnoredUserListEventContent {
map.insert(user_id.clone(), Empty);
}
let raw = raw::IgnoredUserListEventContent {
ignored_users: map,
};
let raw = raw::IgnoredUserListEventContent { ignored_users: map };
raw.serialize(serializer)
}
}
mod raw {
use crate::Empty;
use super::*;
use crate::Empty;
/// A list of users to ignore.
#[derive(Clone, Debug, Deserialize)]

View File

@ -7,9 +7,9 @@ use serde::{Deserialize, Serialize};
pub mod accept;
pub mod cancel;
pub mod key;
pub mod mac;
// pub mod mac;
pub mod request;
pub mod start;
// pub mod start;
/// A hash algorithm.
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Deserialize)]

View File

@ -1,22 +1,20 @@
//! Types for the *m.key.verification.accept* event.
use serde::{Deserialize, Serialize};
use ruma_events_macros::ruma_event;
use super::{
HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString,
VerificationMethod,
};
event! {
ruma_event! {
/// Accepts a previously sent *m.key.verification.start* messge.
///
/// Typically sent as a to-device event.
pub struct AcceptEvent(AcceptEventContent) {}
}
/// The payload of an *m.key.verification.accept* event.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct AcceptEventContent {
AcceptEvent {
kind: Event,
event_type: KeyVerificationAccept,
content: {
/// An opaque identifier for the verification process.
///
/// Must be the same as the one used for the *m.key.verification.start* message.
@ -48,4 +46,6 @@ pub struct AcceptEventContent {
/// key (encoded as unpadded base64) and the canonical JSON representation of the
/// *m.key.verification.start* message.
pub commitment: String,
},
}
}

View File

@ -2,21 +2,20 @@
use std::fmt::{Display, Formatter, Result as FmtResult};
use ruma_events_macros::ruma_event;
use serde::{
de::{Error as SerdeError, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
event! {
ruma_event! {
/// Cancels a key verification process/request.
///
/// Typically sent as a to-device event.
pub struct CancelEvent(CancelEventContent) {}
}
/// The payload of an *m.key.verification.cancel* event.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct CancelEventContent {
CancelEvent {
kind: Event,
event_type: KeyVerificationCancel,
content: {
/// The opaque identifier for the verification process/request.
pub transaction_id: String,
@ -27,6 +26,8 @@ pub struct CancelEventContent {
/// The error code for why the process/request was cancelled by the user.
pub code: CancelCode,
},
}
}
/// An error code for why the process/request was cancelled by the user.

View File

@ -1,17 +1,15 @@
//! Types for the *m.key.verification.key* event.
use serde::{Deserialize, Serialize};
use ruma_events_macros::ruma_event;
event! {
ruma_event! {
/// Sends the ephemeral public key for a device to the partner device.
///
/// Typically sent as a to-device event.
pub struct KeyEvent(KeyEventContent) {}
}
/// The payload of an *m.key.verification.key* event.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct KeyEventContent {
KeyEvent {
kind: Event,
event_type: KeyVerificationKey,
content: {
/// An opaque identifier for the verification process.
///
/// Must be the same as the one used for the *m.key.verification.start* message.
@ -19,4 +17,6 @@ pub struct KeyEventContent {
/// The device's ephemeral public key, encoded as unpadded Base64.
pub key: String,
},
}
}

View File

@ -1,18 +1,16 @@
//! Types for the *m.key.verification.mac* event.
use ruma_events_macros::ruma_event;
use ruma_signatures::SignatureSet;
use serde::{Deserialize, Serialize};
event! {
ruma_event! {
/// Sends the MAC of a device's key to the partner device.
///
/// Typically sent as a to-device event.
pub struct MacEvent(MacEventContent) {}
}
/// The payload for an *m.key.verification.mac* event.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MacEventContent {
MacEvent {
kind: Event,
event_type: KeyVerificationMac,
content: {
/// An opaque identifier for the verification process.
///
/// Must be the same as the one used for the *m.key.verification.start* message.
@ -26,4 +24,6 @@ pub struct MacEventContent {
/// The MAC of the comma-separated, sorted, list of key IDs given in the `mac` property, encoded
/// as unpadded Base64.
pub keys: String,
},
}
}

View File

@ -1,21 +1,19 @@
//! Types for the *m.key.verification.request* event.
use js_int::UInt;
use ruma_events_macros::ruma_event;
use ruma_identifiers::DeviceId;
use serde::{Deserialize, Serialize};
use super::VerificationMethod;
event! {
ruma_event! {
/// Requests a key verification with another user's devices.
///
/// Typically sent as a to-device event.
pub struct RequestEvent(RequestEventContent) {}
}
/// The payload of an *m.key.verification.request* event.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct RequestEventContent {
RequestEvent {
kind: Event,
event_type: KeyVerificationRequest,
content: {
/// The device ID which is initiating the request.
pub from_device: DeviceId,
@ -29,7 +27,9 @@ pub struct RequestEventContent {
/// The POSIX timestamp in milliseconds for when the request was made.
///
/// If the request is in the future by more than 5 minutes or more than 10 minutes in the past,
/// the message should be ignored by the receiver.
/// If the request is in the future by more than 5 minutes or more than 10 minutes in
/// the past, the message should be ignored by the receiver.
pub timestamp: UInt,
},
}
}

View File

@ -113,6 +113,10 @@ use serde::{
};
use serde_json::Value;
pub use custom::CustomEvent;
pub use custom_room::CustomRoomEvent;
pub use custom_state::CustomStateEvent;
#[macro_use]
mod macros;
@ -127,12 +131,12 @@ pub mod dummy;
pub mod forwarded_room_key;
pub mod fully_read;
pub mod ignored_user_list;
// pub mod key;
pub mod key;
pub mod presence;
// pub mod push_rules;
pub mod receipt;
pub mod room;
// pub mod room_key;
pub mod room_key;
pub mod room_key_request;
pub mod sticker;
// pub mod stripped;
@ -223,7 +227,7 @@ pub struct Empty;
impl Serialize for Empty {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
S: Serializer,
{
serializer.serialize_map(Some(0))?.end()
}
@ -232,7 +236,7 @@ impl Serialize for Empty {
impl<'de> Deserialize<'de> for Empty {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>
D: Deserializer<'de>,
{
struct EmptyMapVisitor;
@ -245,7 +249,7 @@ impl<'de> Deserialize<'de> for Empty {
fn visit_map<A>(self, _map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>
A: MapAccess<'de>,
{
Ok(Empty)
}
@ -401,9 +405,6 @@ pub trait Event
where
Self: Debug + Serialize,
{
/// The type of the event.
const EVENT_TYPE: EventType;
/// The type of this event's `content` field.
type Content: Debug + Serialize;
@ -411,9 +412,7 @@ where
fn content(&self) -> &Self::Content;
/// The type of the event.
fn event_type(&self) -> EventType {
Self::EVENT_TYPE
}
fn event_type(&self) -> EventType;
}
/// An event within the context of a room.
@ -447,20 +446,56 @@ pub trait StateEvent: RoomEvent {
fn state_key(&self) -> &str;
}
// event! {
// /// A custom basic event not covered by the Matrix specification.
// pub struct CustomEvent(Value) {}
// }
mod custom {
use ruma_events_macros::ruma_event;
use serde_json::Value;
// room_event! {
// /// A custom room event not covered by the Matrix specification.
// pub struct CustomRoomEvent(Value) {}
// }
ruma_event! {
/// A custom basic event not covered by the Matrix specification.
CustomEvent {
kind: Event,
event_type: Custom,
content_type_alias: {
/// The payload for `CustomEvent`.
Value
},
}
}
}
// state_event! {
// /// A custom state event not covered by the Matrix specification.
// pub struct CustomStateEvent(Value) {}
// }
mod custom_room {
use ruma_events_macros::ruma_event;
use serde_json::Value;
ruma_event! {
/// A custom room event not covered by the Matrix specification.
CustomRoomEvent {
kind: RoomEvent,
event_type: Custom,
content_type_alias: {
/// The payload for `CustomRoomEvent`.
Value
},
}
}
}
mod custom_state {
use ruma_events_macros::ruma_event;
use serde_json::Value;
ruma_event! {
/// A custom state event not covered by the Matrix specification.
CustomStateEvent {
kind: StateEvent,
event_type: Custom,
content_type_alias: {
/// The payload for `CustomStateEvent`.
Value
},
}
}
}
impl Display for EventType {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {

View File

@ -1,20 +1,18 @@
//! Types for the *m.room_key* event.
use ruma_events_macros::ruma_event;
use ruma_identifiers::RoomId;
use serde::{Deserialize, Serialize};
use super::Algorithm;
event! {
ruma_event! {
/// This event type is used to exchange keys for end-to-end encryption.
///
/// Typically it is encrypted as an *m.room.encrypted* event, then sent as a to-device event.
pub struct RoomKeyEvent(RoomKeyEventContent) {}
}
/// The payload of an *m.room_key* event.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct RoomKeyEventContent {
RoomKeyEvent {
kind: Event,
event_type: RoomKey,
content: {
/// The encryption algorithm the key in this event is to be used with.
///
/// Must be `m.megolm.v1.aes-sha2`.
@ -29,3 +27,5 @@ pub struct RoomKeyEventContent {
/// The key to be exchanged.
pub session_key: String,
}
}
}