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 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
36 pub shared_history: bool,
37}
38
39impl ToDeviceRoomKeyEventContent {
40 pub fn new(
43 algorithm: EventEncryptionAlgorithm,
44 room_id: OwnedRoomId,
45 session_id: String,
46 session_key: String,
47 ) -> Self {
48 Self { algorithm, room_id, session_id, session_key, shared_history: false }
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use ruma_common::{canonical_json::assert_to_canonical_json_eq, owned_room_id};
55 use serde_json::json;
56
57 use super::ToDeviceRoomKeyEventContent;
58 use crate::EventEncryptionAlgorithm;
59
60 #[test]
61 fn serialization() {
62 let content = ToDeviceRoomKeyEventContent {
63 algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
64 room_id: owned_room_id!("!testroomid:example.org"),
65 session_id: "SessId".into(),
66 session_key: "SessKey".into(),
67 shared_history: true,
68 };
69
70 assert_to_canonical_json_eq!(
71 content,
72 json!({
73 "algorithm": "m.megolm.v1.aes-sha2",
74 "room_id": "!testroomid:example.org",
75 "session_id": "SessId",
76 "session_key": "SessKey",
77 "shared_history": true,
78 })
79 );
80 }
81
82 #[test]
83 fn deserialize() {
84 let content_json = json!({
85 "algorithm": "m.megolm.v1.aes-sha2",
86 "room_id": "!r:example.org",
87 "session_id": "Sess6",
88 "session_key": "SessK",
89 "shared_history": true,
90 });
91
92 let content: ToDeviceRoomKeyEventContent = serde_json::from_value(content_json).unwrap();
93
94 assert_eq!(content.algorithm, EventEncryptionAlgorithm::MegolmV1AesSha2);
95 assert_eq!(content.room_id, "!r:example.org");
96 assert_eq!(content.session_id, "Sess6");
97 assert_eq!(content.session_key, "SessK");
98 assert!(content.shared_history);
99 }
100}