use std::collections::BTreeMap;
use js_int::UInt;
use ruma_events_macros::EventContent;
use ruma_identifiers::{DeviceId, EventId};
use serde::{Deserialize, Serialize};
use crate::room::message::InReplyTo;
mod relation_serde;
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.encrypted", kind = Message, kind = ToDevice)]
pub struct RoomEncryptedEventContent {
#[serde(flatten)]
pub scheme: EncryptedEventScheme,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub relates_to: Option<Relation>,
}
impl RoomEncryptedEventContent {
pub fn new(scheme: EncryptedEventScheme, relates_to: Option<Relation>) -> Self {
Self { scheme, relates_to }
}
}
impl From<EncryptedEventScheme> for RoomEncryptedEventContent {
fn from(scheme: EncryptedEventScheme) -> Self {
Self { scheme, relates_to: None }
}
}
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.encrypted", kind = ToDevice)]
pub struct ToDeviceRoomEncryptedEventContent {
#[serde(flatten)]
pub scheme: EncryptedEventScheme,
}
impl ToDeviceRoomEncryptedEventContent {
pub fn new(scheme: EncryptedEventScheme) -> Self {
Self { scheme }
}
}
impl From<EncryptedEventScheme> for ToDeviceRoomEncryptedEventContent {
fn from(scheme: EncryptedEventScheme) -> Self {
Self { scheme }
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[serde(tag = "algorithm")]
pub enum EncryptedEventScheme {
#[serde(rename = "m.olm.v1.curve25519-aes-sha2")]
OlmV1Curve25519AesSha2(OlmV1Curve25519AesSha2Content),
#[serde(rename = "m.megolm.v1.aes-sha2")]
MegolmV1AesSha2(MegolmV1AesSha2Content),
}
#[derive(Clone, Debug)]
#[allow(clippy::manual_non_exhaustive)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum Relation {
Reply {
in_reply_to: InReplyTo,
},
#[cfg(feature = "unstable-msc2676")]
Replacement(Replacement),
Reference(Reference),
#[cfg(feature = "unstable-msc2677")]
Annotation(Annotation),
#[doc(hidden)]
_Custom,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg(feature = "unstable-msc2676")]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Replacement {
pub event_id: Box<EventId>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Reference {
pub event_id: Box<EventId>,
}
impl Reference {
pub fn new(event_id: Box<EventId>) -> Self {
Self { event_id }
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg(feature = "unstable-msc2677")]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Annotation {
pub event_id: Box<EventId>,
pub key: String,
}
#[cfg(feature = "unstable-msc2677")]
impl Annotation {
pub fn new(event_id: Box<EventId>, key: String) -> Self {
Self { event_id, key }
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct OlmV1Curve25519AesSha2Content {
pub ciphertext: BTreeMap<String, CiphertextInfo>,
pub sender_key: String,
}
impl OlmV1Curve25519AesSha2Content {
pub fn new(ciphertext: BTreeMap<String, CiphertextInfo>, sender_key: String) -> Self {
Self { ciphertext, sender_key }
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct CiphertextInfo {
pub body: String,
#[serde(rename = "type")]
pub message_type: UInt,
}
impl CiphertextInfo {
pub fn new(body: String, message_type: UInt) -> Self {
Self { body, message_type }
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct MegolmV1AesSha2Content {
pub ciphertext: String,
pub sender_key: String,
pub device_id: Box<DeviceId>,
pub session_id: String,
}
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)]
pub struct MegolmV1AesSha2ContentInit {
pub ciphertext: String,
pub sender_key: String,
pub device_id: Box<DeviceId>,
pub session_id: String,
}
impl From<MegolmV1AesSha2ContentInit> for MegolmV1AesSha2Content {
fn from(init: MegolmV1AesSha2ContentInit) -> Self {
let MegolmV1AesSha2ContentInit { ciphertext, sender_key, device_id, session_id } = init;
Self { ciphertext, sender_key, device_id, session_id }
}
}
#[cfg(test)]
mod tests {
use matches::assert_matches;
use ruma_identifiers::event_id;
use ruma_serde::Raw;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{
EncryptedEventScheme, MegolmV1AesSha2Content, Relation, RoomEncryptedEventContent,
};
use crate::room::message::InReplyTo;
#[test]
fn serialization() {
let key_verification_start_content = RoomEncryptedEventContent {
scheme: EncryptedEventScheme::MegolmV1AesSha2(MegolmV1AesSha2Content {
ciphertext: "ciphertext".into(),
sender_key: "sender_key".into(),
device_id: "device_id".into(),
session_id: "session_id".into(),
}),
relates_to: Some(Relation::Reply {
in_reply_to: InReplyTo { event_id: event_id!("$h29iv0s8:example.com").to_owned() },
}),
};
let json_data = json!({
"algorithm": "m.megolm.v1.aes-sha2",
"ciphertext": "ciphertext",
"sender_key": "sender_key",
"device_id": "device_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);
}
#[test]
fn deserialization() {
let json_data = json!({
"algorithm": "m.megolm.v1.aes-sha2",
"ciphertext": "ciphertext",
"sender_key": "sender_key",
"device_id": "device_id",
"session_id": "session_id",
"m.relates_to": {
"m.in_reply_to": {
"event_id": "$h29iv0s8:example.com"
}
},
});
let content: RoomEncryptedEventContent = from_json_value(json_data).unwrap();
assert_matches!(
content.scheme,
EncryptedEventScheme::MegolmV1AesSha2(MegolmV1AesSha2Content {
ciphertext,
sender_key,
device_id,
session_id,
}) if ciphertext == "ciphertext"
&& sender_key == "sender_key"
&& device_id == "device_id"
&& session_id == "session_id"
);
assert_matches!(
content.relates_to,
Some(Relation::Reply { in_reply_to })
if in_reply_to.event_id == event_id!("$h29iv0s8:example.com")
);
}
#[test]
fn deserialization_olm() {
let json_data = json!({
"sender_key": "test_key",
"ciphertext": {
"test_curve_key": {
"body": "encrypted_body",
"type": 1
}
},
"algorithm": "m.olm.v1.curve25519-aes-sha2"
});
let content: RoomEncryptedEventContent = from_json_value(json_data).unwrap();
match content.scheme {
EncryptedEventScheme::OlmV1Curve25519AesSha2(c) => {
assert_eq!(c.sender_key, "test_key");
assert_eq!(c.ciphertext.len(), 1);
assert_eq!(c.ciphertext["test_curve_key"].body, "encrypted_body");
assert_eq!(c.ciphertext["test_curve_key"].message_type, 1_u16.into());
}
_ => panic!("Wrong content type, expected a OlmV1 content"),
}
}
#[test]
fn deserialization_failure() {
assert!(from_json_value::<Raw<RoomEncryptedEventContent>>(
json!({ "algorithm": "m.megolm.v1.aes-sha2" })
)
.unwrap()
.deserialize()
.is_err());
}
}