ruma_events/room/
encryption.rs

1//! Types for the [`m.room.encryption`] event.
2//!
3//! [`m.room.encryption`]: https://spec.matrix.org/latest/client-server-api/#mroomencryption
4
5use js_int::{uint, UInt};
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9use crate::{EmptyStateKey, EventEncryptionAlgorithm};
10
11/// The content of an `m.room.encryption` event.
12///
13/// Defines how messages sent in this room should be encrypted.
14#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
15#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
16#[ruma_event(type = "m.room.encryption", kind = State, state_key_type = EmptyStateKey)]
17pub struct RoomEncryptionEventContent {
18    /// The encryption algorithm to be used to encrypt messages sent in this room.
19    ///
20    /// Must be `m.megolm.v1.aes-sha2`.
21    pub algorithm: EventEncryptionAlgorithm,
22
23    /// Whether state events should be encrypted alongside message-like events.
24    #[cfg(feature = "unstable-msc3414")]
25    #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
26    #[serde(rename = "io.element.msc3414.encrypt_state_events")]
27    pub encrypt_state_events: bool,
28
29    /// How long the session should be used before changing it.
30    ///
31    /// `uint!(604800000)` (a week) is the recommended default.
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub rotation_period_ms: Option<UInt>,
34
35    /// How many messages should be sent before changing the session.
36    ///
37    /// `uint!(100)` is the recommended default.
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub rotation_period_msgs: Option<UInt>,
40}
41
42impl RoomEncryptionEventContent {
43    /// Creates a new `RoomEncryptionEventContent` with the given algorithm.
44    pub fn new(algorithm: EventEncryptionAlgorithm) -> Self {
45        Self {
46            algorithm,
47            #[cfg(feature = "unstable-msc3414")]
48            encrypt_state_events: false,
49            rotation_period_ms: None,
50            rotation_period_msgs: None,
51        }
52    }
53
54    /// Creates a new `RoomEncryptionEventContent` with the mandatory algorithm and the recommended
55    /// defaults.
56    ///
57    /// Note that changing the values of the fields is not a breaking change and you shouldn't rely
58    /// on those specific values.
59    pub fn with_recommended_defaults() -> Self {
60        // Defaults defined at <https://spec.matrix.org/latest/client-server-api/#mroomencryption>
61        Self {
62            algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
63            #[cfg(feature = "unstable-msc3414")]
64            encrypt_state_events: false,
65            rotation_period_ms: Some(uint!(604_800_000)),
66            rotation_period_msgs: Some(uint!(100)),
67        }
68    }
69
70    /// Enable encrypted state as specified in [MSC3414][msc].
71    ///
72    /// [msc]: https://github.com/matrix-org/matrix-spec-proposals/pull/3414
73    #[cfg(feature = "unstable-msc3414")]
74    pub fn with_encrypted_state(mut self) -> Self {
75        self.encrypt_state_events = true;
76        self
77    }
78}