ruma_events/
encrypted.rs

1//! Types for extensible encrypted events ([MSC3956]).
2//!
3//! [MSC3956]: https://github.com/matrix-org/matrix-spec-proposals/pull/3956
4
5use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8use super::room::encrypted::{EncryptedEventScheme, Relation};
9
10/// The payload for an extensible encrypted message.
11///
12/// This is the new primary type introduced in [MSC3956] and should only be sent in rooms with a
13/// version that supports it. See the documentation of the [`message`] module for more information.
14///
15/// [MSC3956]: https://github.com/matrix-org/matrix-spec-proposals/pull/3956
16/// [`message`]: super::message
17#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
18#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
19#[ruma_event(type = "org.matrix.msc1767.encrypted", kind = MessageLike)]
20pub struct EncryptedEventContent {
21    /// The encrypted content.
22    #[serde(rename = "org.matrix.msc1767.encrypted")]
23    pub encrypted: EncryptedContentBlock,
24
25    /// Information about related events.
26    #[serde(rename = "m.relates_to", skip_serializing_if = "Option::is_none")]
27    pub relates_to: Option<Relation>,
28}
29
30impl EncryptedEventContent {
31    /// Creates a new `EncryptedEventContent` with the given scheme and relation.
32    pub fn new(scheme: EncryptedEventScheme, relates_to: Option<Relation>) -> Self {
33        Self { encrypted: scheme.into(), relates_to }
34    }
35}
36
37impl From<EncryptedEventScheme> for EncryptedEventContent {
38    fn from(scheme: EncryptedEventScheme) -> Self {
39        Self { encrypted: scheme.into(), relates_to: None }
40    }
41}
42
43/// A block for encrypted content.
44#[derive(Clone, Debug, Deserialize, Serialize)]
45#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
46pub struct EncryptedContentBlock {
47    /// Algorithm-specific fields.
48    #[serde(flatten)]
49    pub scheme: EncryptedEventScheme,
50}
51
52impl From<EncryptedEventScheme> for EncryptedContentBlock {
53    fn from(scheme: EncryptedEventScheme) -> Self {
54        Self { scheme }
55    }
56}