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