1use ruma_common::{EventEncryptionAlgorithm, OwnedRoomId};
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9pub mod withheld;
10
11#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
15#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
16#[ruma_event(type = "m.room_key", kind = ToDevice)]
17pub struct ToDeviceRoomKeyEventContent {
18 pub algorithm: EventEncryptionAlgorithm,
22
23 pub room_id: OwnedRoomId,
25
26 pub session_id: String,
28
29 pub session_key: String,
31
32 #[cfg(feature = "unstable-msc3061")]
36 #[serde(
37 default,
38 rename = "org.matrix.msc3061.shared_history",
39 skip_serializing_if = "ruma_common::serde::is_default"
40 )]
41 pub shared_history: bool,
42}
43
44impl ToDeviceRoomKeyEventContent {
45 pub fn new(
48 algorithm: EventEncryptionAlgorithm,
49 room_id: OwnedRoomId,
50 session_id: String,
51 session_key: String,
52 ) -> Self {
53 Self {
54 algorithm,
55 room_id,
56 session_id,
57 session_key,
58 #[cfg(feature = "unstable-msc3061")]
59 shared_history: false,
60 }
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use ruma_common::owned_room_id;
67 use serde_json::{json, to_value as to_json_value};
68
69 use super::ToDeviceRoomKeyEventContent;
70 use crate::EventEncryptionAlgorithm;
71
72 #[test]
73 fn serialization() {
74 let content = ToDeviceRoomKeyEventContent {
75 algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
76 room_id: owned_room_id!("!testroomid:example.org"),
77 session_id: "SessId".into(),
78 session_key: "SessKey".into(),
79 #[cfg(feature = "unstable-msc3061")]
80 shared_history: true,
81 };
82
83 #[cfg(not(feature = "unstable-msc3061"))]
84 assert_eq!(
85 to_json_value(content).unwrap(),
86 json!({
87 "algorithm": "m.megolm.v1.aes-sha2",
88 "room_id": "!testroomid:example.org",
89 "session_id": "SessId",
90 "session_key": "SessKey",
91 })
92 );
93
94 #[cfg(feature = "unstable-msc3061")]
95 assert_eq!(
96 to_json_value(content).unwrap(),
97 json!({
98 "algorithm": "m.megolm.v1.aes-sha2",
99 "room_id": "!testroomid:example.org",
100 "session_id": "SessId",
101 "session_key": "SessKey",
102 "org.matrix.msc3061.shared_history": true,
103 })
104 );
105 }
106}