ruma_events/
room_key.rs

1//! Types for the [`m.room_key`] event.
2//!
3//! [`m.room_key`]: https://spec.matrix.org/latest/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    #[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    /// Creates a new `ToDeviceRoomKeyEventContent` with the given algorithm, room ID, session ID
46    /// and session key.
47    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}