events: Move m.relates_to to EncryptedEventContent

This commit is contained in:
132ikl 2021-04-27 02:07:28 -04:00 committed by GitHub
parent 481531a161
commit 21f46520d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,12 +12,11 @@ use crate::{room::message::Relation, MessageEvent};
/// An event that has been encrypted. /// An event that has been encrypted.
pub type EncryptedEvent = MessageEvent<EncryptedEventContent>; pub type EncryptedEvent = MessageEvent<EncryptedEventContent>;
/// The payload for `EncryptedEvent`. /// The encryption scheme for `EncryptedEventContent`
#[derive(Clone, Debug, Deserialize, Serialize, MessageEventContent)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.encrypted")]
#[serde(tag = "algorithm")] #[serde(tag = "algorithm")]
pub enum EncryptedEventContent { pub enum EncryptedEventScheme {
/// An event encrypted with *m.olm.v1.curve25519-aes-sha2*. /// An event encrypted with *m.olm.v1.curve25519-aes-sha2*.
#[serde(rename = "m.olm.v1.curve25519-aes-sha2")] #[serde(rename = "m.olm.v1.curve25519-aes-sha2")]
OlmV1Curve25519AesSha2(OlmV1Curve25519AesSha2Content), OlmV1Curve25519AesSha2(OlmV1Curve25519AesSha2Content),
@ -27,6 +26,21 @@ pub enum EncryptedEventContent {
MegolmV1AesSha2(MegolmV1AesSha2Content), MegolmV1AesSha2(MegolmV1AesSha2Content),
} }
/// The content payload for `EncryptedEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, MessageEventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.encrypted")]
pub struct EncryptedEventContent {
/// Encrypted event content
#[serde(flatten)]
pub scheme: EncryptedEventScheme,
/// Information about related messages for
/// [rich replies](https://matrix.org/docs/spec/client_server/r0.6.1#rich-replies).
#[serde(rename = "m.relates_to", skip_serializing_if = "Option::is_none")]
pub relates_to: Option<Relation>,
}
/// The to-device version of the payload for the `EncryptedEvent`. /// The to-device version of the payload for the `EncryptedEvent`.
pub type EncryptedToDeviceEventContent = EncryptedEventContent; pub type EncryptedToDeviceEventContent = EncryptedEventContent;
@ -87,11 +101,6 @@ pub struct MegolmV1AesSha2Content {
/// The ID of the session used to encrypt the message. /// The ID of the session used to encrypt the message.
pub session_id: String, pub session_id: String,
/// Information about related messages for
/// [rich replies](https://matrix.org/docs/spec/client_server/r0.6.1#rich-replies).
#[serde(rename = "m.relates_to", skip_serializing_if = "Option::is_none")]
pub relates_to: Option<Relation>,
} }
/// Mandatory initial set of fields of `MegolmV1AesSha2Content`. /// Mandatory initial set of fields of `MegolmV1AesSha2Content`.
@ -117,7 +126,7 @@ impl From<MegolmV1AesSha2ContentInit> for MegolmV1AesSha2Content {
/// Creates a new `MegolmV1AesSha2Content` from the given init struct. /// Creates a new `MegolmV1AesSha2Content` from the given init struct.
fn from(init: MegolmV1AesSha2ContentInit) -> Self { fn from(init: MegolmV1AesSha2ContentInit) -> Self {
let MegolmV1AesSha2ContentInit { ciphertext, sender_key, device_id, session_id } = init; let MegolmV1AesSha2ContentInit { ciphertext, sender_key, device_id, session_id } = init;
Self { ciphertext, sender_key, device_id, session_id, relates_to: None } Self { ciphertext, sender_key, device_id, session_id }
} }
} }
@ -127,25 +136,35 @@ mod tests {
use ruma_serde::Raw; use ruma_serde::Raw;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{EncryptedEventContent, MegolmV1AesSha2Content}; use super::{EncryptedEventContent, EncryptedEventScheme, MegolmV1AesSha2Content};
use crate::room::message::{InReplyTo, Relation};
use ruma_identifiers::event_id;
#[test] #[test]
fn serialization() { fn serialization() {
let key_verification_start_content = let key_verification_start_content = EncryptedEventContent {
EncryptedEventContent::MegolmV1AesSha2(MegolmV1AesSha2Content { scheme: EncryptedEventScheme::MegolmV1AesSha2(MegolmV1AesSha2Content {
ciphertext: "ciphertext".into(), ciphertext: "ciphertext".into(),
sender_key: "sender_key".into(), sender_key: "sender_key".into(),
device_id: "device_id".into(), device_id: "device_id".into(),
session_id: "session_id".into(), session_id: "session_id".into(),
relates_to: None, }),
}); relates_to: Some(Relation::Reply {
in_reply_to: InReplyTo { event_id: event_id!("$h29iv0s8:example.com") },
}),
};
let json_data = json!({ let json_data = json!({
"algorithm": "m.megolm.v1.aes-sha2", "algorithm": "m.megolm.v1.aes-sha2",
"ciphertext": "ciphertext", "ciphertext": "ciphertext",
"sender_key": "sender_key", "sender_key": "sender_key",
"device_id": "device_id", "device_id": "device_id",
"session_id": "session_id" "session_id": "session_id",
"m.relates_to": {
"m.in_reply_to": {
"event_id": "$h29iv0s8:example.com"
}
},
}); });
assert_eq!(to_json_value(&key_verification_start_content).unwrap(), json_data); assert_eq!(to_json_value(&key_verification_start_content).unwrap(), json_data);
@ -158,25 +177,36 @@ mod tests {
"ciphertext": "ciphertext", "ciphertext": "ciphertext",
"sender_key": "sender_key", "sender_key": "sender_key",
"device_id": "device_id", "device_id": "device_id",
"session_id": "session_id" "session_id": "session_id",
"m.relates_to": {
"m.in_reply_to": {
"event_id": "$h29iv0s8:example.com"
}
},
}); });
assert_matches!( let content: EncryptedEventContent =
from_json_value::<Raw<EncryptedEventContent>>(json_data) from_json_value::<Raw<EncryptedEventContent>>(json_data)
.unwrap() .unwrap()
.deserialize() .deserialize()
.unwrap(), .unwrap();
EncryptedEventContent::MegolmV1AesSha2(MegolmV1AesSha2Content {
assert_matches!(content.scheme, EncryptedEventScheme::MegolmV1AesSha2(MegolmV1AesSha2Content {
ciphertext, ciphertext,
sender_key, sender_key,
device_id, device_id,
session_id, session_id,
relates_to: None,
}) if ciphertext == "ciphertext" }) if ciphertext == "ciphertext"
&& sender_key == "sender_key" && sender_key == "sender_key"
&& device_id == "device_id" && device_id == "device_id"
&& session_id == "session_id" && session_id == "session_id"
); );
assert_matches!(
content.relates_to,
Option::<Relation>::Some(Relation::Reply { in_reply_to })
if in_reply_to.event_id == event_id!("$h29iv0s8:example.com")
);
} }
#[test] #[test]
@ -196,8 +226,8 @@ mod tests {
.deserialize() .deserialize()
.unwrap(); .unwrap();
match content { match content.scheme {
EncryptedEventContent::OlmV1Curve25519AesSha2(c) => { EncryptedEventScheme::OlmV1Curve25519AesSha2(c) => {
assert_eq!(c.sender_key, "test_key"); assert_eq!(c.sender_key, "test_key");
assert_eq!(c.ciphertext.len(), 1); assert_eq!(c.ciphertext.len(), 1);
assert_eq!(c.ciphertext["test_curve_key"].body, "encrypted_body"); assert_eq!(c.ciphertext["test_curve_key"].body, "encrypted_body");