Skip to main content

ruma_events/
file.rs

1//! Types for extensible file message events ([MSC3551]).
2//!
3//! [MSC3551]: https://github.com/matrix-org/matrix-spec-proposals/pull/3551
4
5use js_int::UInt;
6use ruma_common::OwnedMxcUri;
7use ruma_macros::EventContent;
8use serde::{Deserialize, Serialize};
9
10use super::{
11    message::TextContentBlock,
12    room::{EncryptedFile, EncryptedFileHashes, EncryptedFileInfo, message::Relation},
13};
14
15/// The payload for an extensible file message.
16///
17/// This is the new primary type introduced in [MSC3551] and should only be sent in rooms with a
18/// version that supports it. See the documentation of the [`message`] module for more information.
19///
20/// [MSC3551]: https://github.com/matrix-org/matrix-spec-proposals/pull/3551
21/// [`message`]: super::message
22#[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
23#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
24#[ruma_event(type = "org.matrix.msc1767.file", kind = MessageLike, without_relation)]
25pub struct FileEventContent {
26    /// The text representation of the message.
27    #[serde(rename = "org.matrix.msc1767.text")]
28    pub text: TextContentBlock,
29
30    /// The file content of the message.
31    #[serde(rename = "org.matrix.msc1767.file")]
32    pub file: FileContentBlock,
33
34    /// The caption of the message, if any.
35    #[serde(rename = "org.matrix.msc1767.caption", skip_serializing_if = "Option::is_none")]
36    pub caption: Option<CaptionContentBlock>,
37
38    /// Whether this message is automated.
39    #[cfg(feature = "unstable-msc3955")]
40    #[serde(
41        default,
42        skip_serializing_if = "ruma_common::serde::is_default",
43        rename = "org.matrix.msc1767.automated"
44    )]
45    pub automated: bool,
46
47    /// Information about related messages.
48    #[serde(
49        flatten,
50        skip_serializing_if = "Option::is_none",
51        deserialize_with = "crate::room::message::relation_serde::deserialize_relation"
52    )]
53    pub relates_to: Option<Relation<FileEventContentWithoutRelation>>,
54}
55
56impl FileEventContent {
57    /// Creates a new non-encrypted `FileEventContent` with the given fallback representation, url
58    /// and file info.
59    pub fn plain(text: TextContentBlock, url: OwnedMxcUri, name: String) -> Self {
60        Self {
61            text,
62            file: FileContentBlock::plain(url, name),
63            caption: None,
64            #[cfg(feature = "unstable-msc3955")]
65            automated: false,
66            relates_to: None,
67        }
68    }
69
70    /// Creates a new non-encrypted `FileEventContent` with the given plain text fallback
71    /// representation, url and name.
72    pub fn plain_with_plain_text(
73        plain_text: impl Into<String>,
74        url: OwnedMxcUri,
75        name: String,
76    ) -> Self {
77        Self {
78            text: TextContentBlock::plain(plain_text),
79            file: FileContentBlock::plain(url, name),
80            caption: None,
81            #[cfg(feature = "unstable-msc3955")]
82            automated: false,
83            relates_to: None,
84        }
85    }
86
87    /// Creates a new encrypted `FileEventContent` with the given fallback representation, url,
88    /// name and encryption info.
89    pub fn encrypted(
90        text: TextContentBlock,
91        url: OwnedMxcUri,
92        name: String,
93        encryption_info: EncryptedContent,
94    ) -> Self {
95        Self {
96            text,
97            file: FileContentBlock::encrypted(url, name, encryption_info),
98            caption: None,
99            #[cfg(feature = "unstable-msc3955")]
100            automated: false,
101            relates_to: None,
102        }
103    }
104
105    /// Creates a new encrypted `FileEventContent` with the given plain text fallback
106    /// representation, url, name and encryption info.
107    pub fn encrypted_with_plain_text(
108        plain_text: impl Into<String>,
109        url: OwnedMxcUri,
110        name: String,
111        encryption_info: EncryptedContent,
112    ) -> Self {
113        Self {
114            text: TextContentBlock::plain(plain_text),
115            file: FileContentBlock::encrypted(url, name, encryption_info),
116            caption: None,
117            #[cfg(feature = "unstable-msc3955")]
118            automated: false,
119            relates_to: None,
120        }
121    }
122}
123
124/// A block for file content.
125#[derive(Clone, Debug, Serialize, Deserialize)]
126#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
127pub struct FileContentBlock {
128    /// The URL to the file.
129    pub url: OwnedMxcUri,
130
131    /// The original filename of the uploaded file.
132    pub name: String,
133
134    /// The mimetype of the file, e.g. "application/msword".
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub mimetype: Option<String>,
137
138    /// The size of the file in bytes.
139    #[serde(skip_serializing_if = "Option::is_none")]
140    pub size: Option<UInt>,
141
142    /// Information on the encrypted file.
143    ///
144    /// Required if the file is encrypted.
145    #[serde(flatten, skip_serializing_if = "Option::is_none")]
146    pub encryption_info: Option<Box<EncryptedContent>>,
147}
148
149impl FileContentBlock {
150    /// Creates a new non-encrypted `FileContentBlock` with the given url and name.
151    pub fn plain(url: OwnedMxcUri, name: String) -> Self {
152        Self { url, name, mimetype: None, size: None, encryption_info: None }
153    }
154
155    /// Creates a new encrypted `FileContentBlock` with the given url, name and encryption info.
156    pub fn encrypted(url: OwnedMxcUri, name: String, encryption_info: EncryptedContent) -> Self {
157        Self {
158            url,
159            name,
160            mimetype: None,
161            size: None,
162            encryption_info: Some(Box::new(encryption_info)),
163        }
164    }
165
166    /// Whether the file is encrypted.
167    pub fn is_encrypted(&self) -> bool {
168        self.encryption_info.is_some()
169    }
170}
171
172/// The encryption info of a file sent to a room with end-to-end encryption enabled.
173#[derive(Clone, Debug, Deserialize, Serialize)]
174#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
175pub struct EncryptedContent {
176    /// Information about the encryption of the file.
177    #[serde(flatten)]
178    pub info: EncryptedFileInfo,
179
180    /// A map from an algorithm name to a hash of the ciphertext.
181    ///
182    /// Clients should support the SHA-256 hash, which uses the key sha256.
183    pub hashes: EncryptedFileHashes,
184}
185
186impl EncryptedContent {
187    /// Construct a new `EncryptedContent` with the given encryption info and hashes.
188    pub fn new(info: EncryptedFileInfo, hashes: EncryptedFileHashes) -> Self {
189        Self { info, hashes }
190    }
191}
192
193impl From<&EncryptedFile> for EncryptedContent {
194    fn from(encrypted: &EncryptedFile) -> Self {
195        let EncryptedFile { info, hashes, .. } = encrypted;
196        Self { info: info.to_owned(), hashes: hashes.to_owned() }
197    }
198}
199
200/// A block for caption content.
201///
202/// A caption is usually a text message that should be displayed next to some media content.
203///
204/// To construct a `CaptionContentBlock` with a custom [`TextContentBlock`], convert it with
205/// `CaptionContentBlock::from()` / `.into()`.
206#[derive(Clone, Debug, Serialize, Deserialize)]
207#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
208pub struct CaptionContentBlock {
209    /// The text message of the caption.
210    #[serde(rename = "org.matrix.msc1767.text")]
211    pub text: TextContentBlock,
212}
213
214impl CaptionContentBlock {
215    /// A convenience constructor to create a plain text caption content block.
216    pub fn plain(body: impl Into<String>) -> Self {
217        Self { text: TextContentBlock::plain(body) }
218    }
219
220    /// A convenience constructor to create an HTML caption content block.
221    pub fn html(body: impl Into<String>, html_body: impl Into<String>) -> Self {
222        Self { text: TextContentBlock::html(body, html_body) }
223    }
224
225    /// A convenience constructor to create a caption content block from Markdown.
226    ///
227    /// The content includes an HTML message if some Markdown formatting was detected, otherwise
228    /// only a plain text message is included.
229    #[cfg(feature = "markdown")]
230    pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
231        Self { text: TextContentBlock::markdown(body) }
232    }
233}
234
235impl From<TextContentBlock> for CaptionContentBlock {
236    fn from(text: TextContentBlock) -> Self {
237        Self { text }
238    }
239}