events: Remove BasicToDevice enum

This commit is contained in:
Kévin Commaille 2021-05-10 17:26:40 +02:00 committed by Jonas Platte
parent 8d8ca81e04
commit 7faa59be6b
12 changed files with 19 additions and 98 deletions

View File

@ -688,10 +688,6 @@ fn marker_traits(kind: &EventKind, ruma_events: &TokenStream) -> TokenStream {
#[automatically_derived] #[automatically_derived]
impl #ruma_events::BasicEventContent for #ident {} impl #ruma_events::BasicEventContent for #ident {}
}, },
EventKind::BasicToDevice => quote! {
#[automatically_derived]
impl #ruma_events::BasicEventContent for #ident {}
},
_ => TokenStream::new(), _ => TokenStream::new(),
} }
} }

View File

@ -68,7 +68,6 @@ pub enum EventKind {
Ephemeral, Ephemeral,
Message, Message,
State, State,
BasicToDevice,
ToDevice, ToDevice,
Redaction, Redaction,
Presence, Presence,
@ -82,7 +81,6 @@ impl fmt::Display for EventKind {
EventKind::Ephemeral => write!(f, "EphemeralRoomEvent"), EventKind::Ephemeral => write!(f, "EphemeralRoomEvent"),
EventKind::Message => write!(f, "MessageEvent"), EventKind::Message => write!(f, "MessageEvent"),
EventKind::State => write!(f, "StateEvent"), EventKind::State => write!(f, "StateEvent"),
EventKind::BasicToDevice => write!(f, "BasicToDeviceEvent"),
EventKind::ToDevice => write!(f, "ToDeviceEvent"), EventKind::ToDevice => write!(f, "ToDeviceEvent"),
EventKind::Redaction => write!(f, "RedactionEvent"), EventKind::Redaction => write!(f, "RedactionEvent"),
EventKind::Presence => write!(f, "PresenceEvent"), EventKind::Presence => write!(f, "PresenceEvent"),
@ -140,14 +138,13 @@ impl Parse for EventKind {
"EphemeralRoom" => EventKind::Ephemeral, "EphemeralRoom" => EventKind::Ephemeral,
"Message" => EventKind::Message, "Message" => EventKind::Message,
"State" => EventKind::State, "State" => EventKind::State,
"BasicToDevice" => EventKind::BasicToDevice,
"ToDevice" => EventKind::ToDevice, "ToDevice" => EventKind::ToDevice,
id => { id => {
return Err(syn::Error::new( return Err(syn::Error::new(
input.span(), input.span(),
format!( format!(
"valid event kinds are GlobalAccountData, RoomAccountData, EphemeralRoom, \ "valid event kinds are GlobalAccountData, RoomAccountData, EphemeralRoom, \
Message, State, BasicToDevice, ToDevice found `{}`", Message, State, ToDevice found `{}`",
id id
), ),
)); ));
@ -179,7 +176,6 @@ pub fn to_kind_variation(ident: &Ident) -> Option<(EventKind, EventKindVariation
"RedactedStrippedStateEvent" => { "RedactedStrippedStateEvent" => {
Some((EventKind::State, EventKindVariation::RedactedStripped)) Some((EventKind::State, EventKindVariation::RedactedStripped))
} }
"BasicToDeviceEvent" => Some((EventKind::BasicToDevice, EventKindVariation::Full)),
"ToDeviceEvent" => Some((EventKind::ToDevice, EventKindVariation::Full)), "ToDeviceEvent" => Some((EventKind::ToDevice, EventKindVariation::Full)),
"PresenceEvent" => Some((EventKind::Presence, EventKindVariation::Full)), "PresenceEvent" => Some((EventKind::Presence, EventKindVariation::Full)),
"RedactionEvent" => Some((EventKind::Redaction, EventKindVariation::Full)), "RedactionEvent" => Some((EventKind::Redaction, EventKindVariation::Full)),

View File

@ -42,7 +42,8 @@ Breaking changes:
``` ```
* Add `tag::TagName` type and use it for `tag::Tags` * Add `tag::TagName` type and use it for `tag::Tags`
* Move `FullyRead` from `EphemeralRoom` enum to `RoomAccountData` enum * Move `FullyRead` from `EphemeralRoom` enum to `RoomAccountData` enum
* Split `Basic` enum into `GlobalAccountData`, `RoomAccountData` and `BasicToDevice` enums * Split `Basic` enum into `GlobalAccountData` and `RoomAccountData` enums
* Remove `DummyEvent`, `DummyEventContent`, `RoomKeyEvent`, `RoomKeyEventContent`
Improvements: Improvements:

View File

@ -3,54 +3,7 @@
use ruma_events_macros::BasicEventContent; use ruma_events_macros::BasicEventContent;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::BasicToDeviceEvent;
/// This event type is used to indicate new Olm sessions for end-to-end encryption.
///
/// Typically it is encrypted as an *m.room.encrypted* event, then sent as a to-device event.
///
/// The event does not have any content associated with it. The sending client is expected to
/// send a key share request shortly after this message, causing the receiving client to process
/// this *m.dummy* event as the most recent event and using the keyshare request to set up the
/// session. The keyshare request and *m.dummy* combination should result in the original
/// sending client receiving keys over the newly established session.
pub type DummyEvent = BasicToDeviceEvent<DummyEventContent>;
/// The payload for `DummyEvent`. /// The payload for `DummyEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] #[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)]
#[ruma_event(type = "m.dummy")] #[ruma_event(type = "m.dummy")]
pub struct DummyEventContent {} pub struct DummyToDeviceEventContent {}
/// The to-device version of the payload for the `DummyEvent`.
pub type DummyToDeviceEventContent = DummyEventContent;
#[cfg(test)]
mod tests {
use ruma_serde::Raw;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{DummyEvent, DummyEventContent};
#[test]
fn serialization() {
let dummy_event = DummyEvent { content: DummyEventContent {} };
let actual = to_json_value(dummy_event).unwrap();
let expected = json!({
"content": {},
"type": "m.dummy"
});
assert_eq!(actual, expected);
}
#[test]
fn deserialization() {
let json = json!({
"content": {},
"type": "m.dummy"
});
assert!(from_json_value::<Raw<DummyEvent>>(json).unwrap().deserialize().is_ok());
}
}

View File

@ -98,15 +98,6 @@ event_enum! {
] ]
} }
event_enum! {
/// Any basic to-device event.
kind: BasicToDevice,
events: [
"m.dummy",
"m.room_key",
]
}
event_enum! { event_enum! {
/// Any to-device event. /// Any to-device event.
kind: ToDevice, kind: ToDevice,

View File

@ -317,14 +317,6 @@ pub struct RedactedStrippedStateEvent<C: RedactedStateEventContent> {
pub state_key: String, pub state_key: String,
} }
/// A basic event sent using send-to-device event messaging one that consists only of it's type
/// and the `content` object.
#[derive(Clone, Debug, Event)]
pub struct BasicToDeviceEvent<C: BasicEventContent> {
/// Data specific to the event type.
pub content: C,
}
/// An event sent using send-to-device messaging. /// An event sent using send-to-device messaging.
#[derive(Clone, Debug, Event)] #[derive(Clone, Debug, Event)]
pub struct ToDeviceEvent<C: EventContent> { pub struct ToDeviceEvent<C: EventContent> {

View File

@ -200,8 +200,8 @@ pub use self::{
}, },
error::{FromStrError, InvalidInput}, error::{FromStrError, InvalidInput},
event_kinds::{ event_kinds::{
BasicToDeviceEvent, EphemeralRoomEvent, GlobalAccountDataEvent, InitialStateEvent, EphemeralRoomEvent, GlobalAccountDataEvent, InitialStateEvent, MessageEvent,
MessageEvent, RedactedMessageEvent, RedactedStateEvent, RedactedStrippedStateEvent, RedactedMessageEvent, RedactedStateEvent, RedactedStrippedStateEvent,
RedactedSyncMessageEvent, RedactedSyncStateEvent, RoomAccountDataEvent, StateEvent, RedactedSyncMessageEvent, RedactedSyncStateEvent, RoomAccountDataEvent, StateEvent,
StrippedStateEvent, SyncEphemeralRoomEvent, SyncMessageEvent, SyncStateEvent, StrippedStateEvent, SyncEphemeralRoomEvent, SyncMessageEvent, SyncStateEvent,
ToDeviceEvent, ToDeviceEvent,

View File

@ -4,15 +4,10 @@ use ruma_events_macros::BasicEventContent;
use ruma_identifiers::{EventEncryptionAlgorithm, RoomId}; use ruma_identifiers::{EventEncryptionAlgorithm, RoomId};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::BasicToDeviceEvent;
/// Typically encrypted as an *m.room.encrypted* event, then sent as a to-device event.
pub type RoomKeyEvent = BasicToDeviceEvent<RoomKeyEventContent>;
/// The payload for `RoomKeyEvent`. /// The payload for `RoomKeyEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] #[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)]
#[ruma_event(type = "m.room_key")] #[ruma_event(type = "m.room_key")]
pub struct RoomKeyEventContent { pub struct RoomKeyToDeviceEventContent {
/// The encryption algorithm the key in this event is to be used with. /// The encryption algorithm the key in this event is to be used with.
/// ///
/// Must be `m.megolm.v1.aes-sha2`. /// Must be `m.megolm.v1.aes-sha2`.
@ -28,26 +23,24 @@ pub struct RoomKeyEventContent {
pub session_key: String, pub session_key: String,
} }
/// The to-device version of the payload for the `RoomKeyEvent`.
pub type RoomKeyToDeviceEventContent = RoomKeyEventContent;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ruma_identifiers::{room_id, EventEncryptionAlgorithm}; use ruma_identifiers::{room_id, user_id, EventEncryptionAlgorithm};
use serde_json::{json, to_value as to_json_value}; use serde_json::{json, to_value as to_json_value};
use super::RoomKeyEventContent; use super::RoomKeyToDeviceEventContent;
use crate::BasicToDeviceEvent; use crate::ToDeviceEvent;
#[test] #[test]
fn serialization() { fn serialization() {
let ev = BasicToDeviceEvent { let ev = ToDeviceEvent {
content: RoomKeyEventContent { content: RoomKeyToDeviceEventContent {
algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2, algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
room_id: room_id!("!testroomid:example.org"), room_id: room_id!("!testroomid:example.org"),
session_id: "SessId".into(), session_id: "SessId".into(),
session_key: "SessKey".into(), session_key: "SessKey".into(),
}, },
sender: user_id!("@user:example.org"),
}; };
assert_eq!( assert_eq!(
@ -60,6 +53,7 @@ mod tests {
"session_id": "SessId", "session_id": "SessId",
"session_key": "SessKey", "session_key": "SessKey",
}, },
"sender": "@user:example.org",
}) })
); );
} }

View File

@ -1,4 +1,4 @@
use ruma_events::{room_key::RoomKeyEventContent, AnyToDeviceEventContent, ToDeviceEvent}; use ruma_events::{room_key::RoomKeyToDeviceEventContent, AnyToDeviceEventContent, ToDeviceEvent};
use ruma_identifiers::{room_id, user_id, EventEncryptionAlgorithm}; use ruma_identifiers::{room_id, user_id, EventEncryptionAlgorithm};
use serde_json::{json, to_value as to_json_value}; use serde_json::{json, to_value as to_json_value};
@ -6,7 +6,7 @@ use serde_json::{json, to_value as to_json_value};
fn serialization() { fn serialization() {
let ev = ToDeviceEvent { let ev = ToDeviceEvent {
sender: user_id!("@example:example.org"), sender: user_id!("@example:example.org"),
content: AnyToDeviceEventContent::RoomKey(RoomKeyEventContent { content: AnyToDeviceEventContent::RoomKey(RoomKeyToDeviceEventContent {
algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2, algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
room_id: room_id!("!testroomid:example.org"), room_id: room_id!("!testroomid:example.org"),
session_id: "SessId".into(), session_id: "SessId".into(),

View File

@ -6,10 +6,8 @@ event_enum! {
events: [ events: [
"m.direct", "m.direct",
#[cfg(test)] #[cfg(test)]
"m.dummy",
"m.ignored_user_list", "m.ignored_user_list",
"m.push_rules", "m.push_rules",
"m.room_key",
#[cfg(any())] #[cfg(any())]
"m.ruma_test", "m.ruma_test",
] ]

View File

@ -1,4 +1,4 @@
error: valid event kinds are GlobalAccountData, RoomAccountData, EphemeralRoom, Message, State, BasicToDevice, ToDevice found `NotReal` error: valid event kinds are GlobalAccountData, RoomAccountData, EphemeralRoom, Message, State, ToDevice found `NotReal`
--> $DIR/09-enum-invalid-kind.rs:4:18 --> $DIR/09-enum-invalid-kind.rs:4:18
| |
4 | kind: NotReal, 4 | kind: NotReal,

View File

@ -5,7 +5,7 @@ use std::collections::BTreeMap;
use js_int::UInt; use js_int::UInt;
use ruma_common::{encryption::DeviceKeys, presence::PresenceState}; use ruma_common::{encryption::DeviceKeys, presence::PresenceState};
use ruma_events::{ use ruma_events::{
from_raw_json_value, receipt::Receipt, room_key::RoomKeyEventContent, EventType, from_raw_json_value, receipt::Receipt, room_key::RoomKeyToDeviceEventContent, EventType,
}; };
use ruma_identifiers::{DeviceIdBox, EventId, RoomId, UserId}; use ruma_identifiers::{DeviceIdBox, EventId, RoomId, UserId};
use serde::{de, Deserialize, Serialize}; use serde::{de, Deserialize, Serialize};
@ -258,7 +258,7 @@ pub struct DirectDeviceContent {
/// The contents of the messages to be sent. These are arranged in a map /// 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 /// 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. /// also be *, meaning all known devices for the user.
pub messages: BTreeMap<UserId, BTreeMap<DeviceIdBox, RoomKeyEventContent>>, pub messages: BTreeMap<UserId, BTreeMap<DeviceIdBox, RoomKeyToDeviceEventContent>>,
} }
impl DirectDeviceContent { impl DirectDeviceContent {