From 0596e4604511f3140c93548d76c8cc211c02cadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sun, 26 Feb 2023 12:04:54 +0100 Subject: [PATCH] events: Add method to construct RoomEncryptionEventContent with the recommended defaults --- crates/ruma-common/CHANGELOG.md | 1 + crates/ruma-common/src/events/room/encryption.rs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index 1f0f3b4c..86b14d1f 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -12,6 +12,7 @@ Improvements: - To remove a user-defined push rule - Add `AsRef<[u8]>` implementations for identifier types - Add `InitialStateEvent::{new, to_raw, to_raw_any}` +- Add a convenience method to construct `RoomEncryptionEventContent` with the recommended defaults. # 0.11.3 diff --git a/crates/ruma-common/src/events/room/encryption.rs b/crates/ruma-common/src/events/room/encryption.rs index aadbf398..e9635800 100644 --- a/crates/ruma-common/src/events/room/encryption.rs +++ b/crates/ruma-common/src/events/room/encryption.rs @@ -2,7 +2,7 @@ //! //! [`m.room.encryption`]: https://spec.matrix.org/latest/client-server-api/#mroomencryption -use js_int::UInt; +use js_int::{uint, UInt}; use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; @@ -38,4 +38,17 @@ impl RoomEncryptionEventContent { pub fn new(algorithm: EventEncryptionAlgorithm) -> Self { Self { algorithm, rotation_period_ms: None, rotation_period_msgs: None } } + + /// Creates a new `RoomEncryptionEventContent` with the mandatory algorithm and the recommended + /// defaults. + /// + /// Note that changing the values of the fields is not a breaking change and you shouldn't rely + /// on those specific values. + pub fn with_recommended_defaults() -> Self { + Self { + algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2, + rotation_period_ms: Some(uint!(604_800_000)), + rotation_period_msgs: Some(uint!(100)), + } + } }