Skip to main content

ruma_events/
room_key.rs

1//! Types for the [`m.room_key`] event.
2//!
3//! [`m.room_key`]: https://spec.matrix.org/v1.19/client-server-api/#mroom_key
4
5use ruma_common::{EventEncryptionAlgorithm, OwnedRoomId};
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9pub mod withheld;
10
11/// The content of an `m.room_key` event.
12///
13/// Typically encrypted as an `m.room.encrypted` event, then sent as a to-device event.
14#[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    /// The encryption algorithm the key in this event is to be used with.
19    ///
20    /// Must be `m.megolm.v1.aes-sha2`.
21    pub algorithm: EventEncryptionAlgorithm,
22
23    /// The room where the key is used.
24    pub room_id: OwnedRoomId,
25
26    /// The ID of the session that the key is for.
27    pub session_id: String,
28
29    /// The key to be exchanged.
30    pub session_key: String,
31
32    /// Used to mark key if allowed for shared history.
33    ///
34    /// Defaults to `false`.
35    #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
36    pub shared_history: bool,
37}
38
39impl ToDeviceRoomKeyEventContent {
40    /// Creates a new `ToDeviceRoomKeyEventContent` with the given algorithm, room ID, session ID
41    /// and session key.
42    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}