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(
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    /// Creates a new `ToDeviceRoomKeyEventContent` with the given algorithm, room ID, session ID
45    /// and session key.
46    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}