matrix_sdk/
attachment.rs

1// Copyright 2022 Kévin Commaille
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Types and traits for attachments.
16
17use std::time::Duration;
18
19use ruma::{
20    assign,
21    events::{
22        room::{
23            message::{AudioInfo, FileInfo, FormattedBody, VideoInfo},
24            ImageInfo, ThumbnailInfo,
25        },
26        Mentions,
27    },
28    OwnedTransactionId, UInt,
29};
30
31use crate::room::reply::Reply;
32
33/// Base metadata about an image.
34#[derive(Debug, Clone, Default)]
35pub struct BaseImageInfo {
36    /// The height of the image in pixels.
37    pub height: Option<UInt>,
38    /// The width of the image in pixels.
39    pub width: Option<UInt>,
40    /// The file size of the image in bytes.
41    pub size: Option<UInt>,
42    /// The [BlurHash](https://blurha.sh/) for this image.
43    pub blurhash: Option<String>,
44    /// Whether this image is animated.
45    pub is_animated: Option<bool>,
46}
47
48/// Base metadata about a video.
49#[derive(Debug, Clone, Default)]
50pub struct BaseVideoInfo {
51    /// The duration of the video.
52    pub duration: Option<Duration>,
53    /// The height of the video in pixels.
54    pub height: Option<UInt>,
55    /// The width of the video in pixels.
56    pub width: Option<UInt>,
57    /// The file size of the video in bytes.
58    pub size: Option<UInt>,
59    /// The [BlurHash](https://blurha.sh/) for this video.
60    pub blurhash: Option<String>,
61}
62
63/// Base metadata about an audio clip.
64#[derive(Debug, Clone, Default)]
65pub struct BaseAudioInfo {
66    /// The duration of the audio clip.
67    pub duration: Option<Duration>,
68    /// The file size of the audio clip in bytes.
69    pub size: Option<UInt>,
70}
71
72/// Base metadata about a file.
73#[derive(Debug, Clone, Default)]
74pub struct BaseFileInfo {
75    /// The size of the file in bytes.
76    pub size: Option<UInt>,
77}
78
79/// Types of metadata for an attachment.
80#[derive(Debug)]
81pub enum AttachmentInfo {
82    /// The metadata of an image.
83    Image(BaseImageInfo),
84    /// The metadata of a video.
85    Video(BaseVideoInfo),
86    /// The metadata of an audio clip.
87    Audio(BaseAudioInfo),
88    /// The metadata of a file.
89    File(BaseFileInfo),
90    /// The metadata of a voice message
91    Voice {
92        /// The audio info
93        audio_info: BaseAudioInfo,
94        /// The waveform of the voice message
95        waveform: Option<Vec<u16>>,
96    },
97}
98
99impl From<AttachmentInfo> for ImageInfo {
100    fn from(info: AttachmentInfo) -> Self {
101        match info {
102            AttachmentInfo::Image(info) => assign!(ImageInfo::new(), {
103                height: info.height,
104                width: info.width,
105                size: info.size,
106                blurhash: info.blurhash,
107                is_animated: info.is_animated,
108            }),
109            _ => ImageInfo::new(),
110        }
111    }
112}
113
114impl From<AttachmentInfo> for VideoInfo {
115    fn from(info: AttachmentInfo) -> Self {
116        match info {
117            AttachmentInfo::Video(info) => assign!(VideoInfo::new(), {
118                duration: info.duration,
119                height: info.height,
120                width: info.width,
121                size: info.size,
122                blurhash: info.blurhash,
123            }),
124            _ => VideoInfo::new(),
125        }
126    }
127}
128
129impl From<AttachmentInfo> for AudioInfo {
130    fn from(info: AttachmentInfo) -> Self {
131        match info {
132            AttachmentInfo::Audio(info) => assign!(AudioInfo::new(), {
133                duration: info.duration,
134                size: info.size,
135            }),
136            AttachmentInfo::Voice { audio_info, .. } => assign!(AudioInfo::new(), {
137                duration: audio_info.duration,
138                size: audio_info.size,
139            }),
140            _ => AudioInfo::new(),
141        }
142    }
143}
144
145impl From<AttachmentInfo> for FileInfo {
146    fn from(info: AttachmentInfo) -> Self {
147        match info {
148            AttachmentInfo::File(info) => assign!(FileInfo::new(), {
149                size: info.size,
150            }),
151            _ => FileInfo::new(),
152        }
153    }
154}
155
156/// A thumbnail to upload and send for an attachment.
157#[derive(Debug)]
158pub struct Thumbnail {
159    /// The raw bytes of the thumbnail.
160    pub data: Vec<u8>,
161    /// The type of the thumbnail, this will be used as the content-type header.
162    pub content_type: mime::Mime,
163    /// The height of the thumbnail in pixels.
164    pub height: UInt,
165    /// The width of the thumbnail in pixels.
166    pub width: UInt,
167    /// The file size of the thumbnail in bytes.
168    pub size: UInt,
169}
170
171impl Thumbnail {
172    /// Convert this `Thumbnail` into a `(data, content_type, info)` tuple.
173    pub fn into_parts(self) -> (Vec<u8>, mime::Mime, Box<ThumbnailInfo>) {
174        let thumbnail_info = assign!(ThumbnailInfo::new(), {
175            height: Some(self.height),
176            width: Some(self.width),
177            size: Some(self.size),
178            mimetype: Some(self.content_type.to_string())
179        });
180        (self.data, self.content_type, Box::new(thumbnail_info))
181    }
182}
183
184/// Configuration for sending an attachment.
185#[derive(Debug, Default)]
186pub struct AttachmentConfig {
187    pub(crate) txn_id: Option<OwnedTransactionId>,
188    pub(crate) info: Option<AttachmentInfo>,
189    pub(crate) thumbnail: Option<Thumbnail>,
190    pub(crate) caption: Option<String>,
191    pub(crate) formatted_caption: Option<FormattedBody>,
192    pub(crate) mentions: Option<Mentions>,
193    pub(crate) reply: Option<Reply>,
194}
195
196impl AttachmentConfig {
197    /// Create a new empty `AttachmentConfig`.
198    pub fn new() -> Self {
199        Self::default()
200    }
201
202    /// Set the thumbnail to send.
203    ///
204    /// # Arguments
205    ///
206    /// * `thumbnail` - The thumbnail of the media. If the `content_type` does
207    ///   not support it (e.g. audio clips), it is ignored.
208    #[must_use]
209    pub fn thumbnail(mut self, thumbnail: Option<Thumbnail>) -> Self {
210        self.thumbnail = thumbnail;
211        self
212    }
213
214    /// Set the transaction ID to send.
215    ///
216    /// # Arguments
217    ///
218    /// * `txn_id` - A unique ID that can be attached to a `MessageEvent` held
219    ///   in its unsigned field as `transaction_id`. If not given, one is
220    ///   created for the message.
221    #[must_use]
222    pub fn txn_id(mut self, txn_id: OwnedTransactionId) -> Self {
223        self.txn_id = Some(txn_id);
224        self
225    }
226
227    /// Set the media metadata to send.
228    ///
229    /// # Arguments
230    ///
231    /// * `info` - The metadata of the media. If the `AttachmentInfo` type
232    ///   doesn't match the `content_type`, it is ignored.
233    #[must_use]
234    pub fn info(mut self, info: AttachmentInfo) -> Self {
235        self.info = Some(info);
236        self
237    }
238
239    /// Set the optional caption.
240    ///
241    /// # Arguments
242    ///
243    /// * `caption` - The optional caption.
244    pub fn caption(mut self, caption: Option<String>) -> Self {
245        self.caption = caption;
246        self
247    }
248
249    /// Set the optional formatted caption.
250    ///
251    /// # Arguments
252    ///
253    /// * `formatted_caption` - The optional formatted caption.
254    pub fn formatted_caption(mut self, formatted_caption: Option<FormattedBody>) -> Self {
255        self.formatted_caption = formatted_caption;
256        self
257    }
258
259    /// Set the mentions of the message.
260    ///
261    /// # Arguments
262    ///
263    /// * `mentions` - The mentions of the message.
264    pub fn mentions(mut self, mentions: Option<Mentions>) -> Self {
265        self.mentions = mentions;
266        self
267    }
268
269    /// Set the reply information of the message.
270    ///
271    /// # Arguments
272    ///
273    /// * `reply` - The reply information of the message.
274    pub fn reply(mut self, reply: Option<Reply>) -> Self {
275        self.reply = reply;
276        self
277    }
278}
279
280/// Configuration for sending a gallery.
281#[cfg(feature = "unstable-msc4274")]
282#[derive(Debug, Default)]
283pub struct GalleryConfig {
284    pub(crate) txn_id: Option<OwnedTransactionId>,
285    pub(crate) items: Vec<GalleryItemInfo>,
286    pub(crate) caption: Option<String>,
287    pub(crate) formatted_caption: Option<FormattedBody>,
288    pub(crate) mentions: Option<Mentions>,
289    pub(crate) reply: Option<Reply>,
290}
291
292#[cfg(feature = "unstable-msc4274")]
293impl GalleryConfig {
294    /// Create a new empty `GalleryConfig`.
295    pub fn new() -> Self {
296        Self::default()
297    }
298
299    /// Set the transaction ID to send.
300    ///
301    /// # Arguments
302    ///
303    /// * `txn_id` - A unique ID that can be attached to a `MessageEvent` held
304    ///   in its unsigned field as `transaction_id`. If not given, one is
305    ///   created for the message.
306    #[must_use]
307    pub fn txn_id(mut self, txn_id: OwnedTransactionId) -> Self {
308        self.txn_id = Some(txn_id);
309        self
310    }
311
312    /// Adds a media item to the gallery.
313    ///
314    /// # Arguments
315    ///
316    /// * `item` - Information about the item to be added.
317    #[must_use]
318    pub fn add_item(mut self, item: GalleryItemInfo) -> Self {
319        self.items.push(item);
320        self
321    }
322
323    /// Set the optional caption.
324    ///
325    /// # Arguments
326    ///
327    /// * `caption` - The optional caption.
328    pub fn caption(mut self, caption: Option<String>) -> Self {
329        self.caption = caption;
330        self
331    }
332
333    /// Set the optional formatted caption.
334    ///
335    /// # Arguments
336    ///
337    /// * `formatted_caption` - The optional formatted caption.
338    pub fn formatted_caption(mut self, formatted_caption: Option<FormattedBody>) -> Self {
339        self.formatted_caption = formatted_caption;
340        self
341    }
342
343    /// Set the mentions of the message.
344    ///
345    /// # Arguments
346    ///
347    /// * `mentions` - The mentions of the message.
348    pub fn mentions(mut self, mentions: Option<Mentions>) -> Self {
349        self.mentions = mentions;
350        self
351    }
352
353    /// Set the reply information of the message.
354    ///
355    /// # Arguments
356    ///
357    /// * `reply` - The reply information of the message.
358    pub fn reply(mut self, reply: Option<Reply>) -> Self {
359        self.reply = reply;
360        self
361    }
362
363    /// Returns the number of media items in the gallery.
364    pub fn len(&self) -> usize {
365        self.items.len()
366    }
367
368    /// Checks whether the gallery contains any media items or not.
369    pub fn is_empty(&self) -> bool {
370        self.items.is_empty()
371    }
372}
373
374#[cfg(feature = "unstable-msc4274")]
375#[derive(Debug)]
376/// Metadata for a gallery item
377pub struct GalleryItemInfo {
378    /// The filename.
379    pub filename: String,
380    /// The mime type.
381    pub content_type: mime::Mime,
382    /// The binary data.
383    pub data: Vec<u8>,
384    /// The attachment info.
385    pub attachment_info: AttachmentInfo,
386    /// The caption.
387    pub caption: Option<String>,
388    /// The formatted caption.
389    pub formatted_caption: Option<FormattedBody>,
390    /// The thumbnail.
391    pub thumbnail: Option<Thumbnail>,
392}