ruma_events/room/message/
video.rs

1use std::time::Duration;
2
3use js_int::UInt;
4#[cfg(feature = "unstable-msc2448")]
5use ruma_common::serde::Base64;
6use ruma_common::OwnedMxcUri;
7use serde::{Deserialize, Serialize};
8
9use super::FormattedBody;
10use crate::room::{
11    message::media_caption::{caption, formatted_caption},
12    EncryptedFile, MediaSource, ThumbnailInfo,
13};
14
15/// The payload for a video message.
16#[derive(Clone, Debug, Deserialize, Serialize)]
17#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
18pub struct VideoMessageEventContent {
19    /// A description of the video.
20    ///
21    /// If the `filename` field is not set or has the same value, this is the filename of the
22    /// uploaded file. Otherwise, this should be interpreted as a user-written media caption.
23    pub body: String,
24
25    /// Formatted form of the message `body`.
26    ///
27    /// This should only be set if the body represents a caption.
28    #[serde(flatten)]
29    pub formatted: Option<FormattedBody>,
30
31    /// The original filename of the uploaded file as deserialized from the event.
32    ///
33    /// It is recommended to use the `filename` method to get the filename which automatically
34    /// falls back to the `body` field when the `filename` field is not set.
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub filename: Option<String>,
37
38    /// The source of the video clip.
39    #[serde(flatten)]
40    pub source: MediaSource,
41
42    /// Metadata about the video clip referred to in `source`.
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub info: Option<Box<VideoInfo>>,
45}
46
47impl VideoMessageEventContent {
48    /// Creates a new `VideoMessageEventContent` with the given body and source.
49    pub fn new(body: String, source: MediaSource) -> Self {
50        Self { body, formatted: None, filename: None, source, info: None }
51    }
52
53    /// Creates a new non-encrypted `VideoMessageEventContent` with the given body and url.
54    pub fn plain(body: String, url: OwnedMxcUri) -> Self {
55        Self::new(body, MediaSource::Plain(url))
56    }
57
58    /// Creates a new encrypted `VideoMessageEventContent` with the given body and encrypted
59    /// file.
60    pub fn encrypted(body: String, file: EncryptedFile) -> Self {
61        Self::new(body, MediaSource::Encrypted(Box::new(file)))
62    }
63
64    /// Creates a new `VideoMessageEventContent` from `self` with the `info` field set to the given
65    /// value.
66    ///
67    /// Since the field is public, you can also assign to it directly. This method merely acts
68    /// as a shorthand for that, because it is very common to set this field.
69    pub fn info(self, info: impl Into<Option<Box<VideoInfo>>>) -> Self {
70        Self { info: info.into(), ..self }
71    }
72
73    /// Computes the filename of the video as defined by the [spec](https://spec.matrix.org/latest/client-server-api/#media-captions).
74    ///
75    /// This differs from the `filename` field as this method falls back to the `body` field when
76    /// the `filename` field is not set.
77    pub fn filename(&self) -> &str {
78        self.filename.as_deref().unwrap_or(&self.body)
79    }
80
81    /// Returns the caption of the video as defined by the [spec](https://spec.matrix.org/latest/client-server-api/#media-captions).
82    ///
83    /// In short, this is the `body` field if the `filename` field exists and has a different value,
84    /// otherwise the media file does not have a caption.
85    pub fn caption(&self) -> Option<&str> {
86        caption(&self.body, self.filename.as_deref())
87    }
88
89    /// Returns the formatted caption of the video as defined by the [spec](https://spec.matrix.org/latest/client-server-api/#media-captions).
90    ///
91    /// This is the same as `caption`, but returns the formatted body instead of the plain body.
92    pub fn formatted_caption(&self) -> Option<&FormattedBody> {
93        formatted_caption(&self.body, self.formatted.as_ref(), self.filename.as_deref())
94    }
95}
96
97/// Metadata about a video.
98#[derive(Clone, Debug, Default, Deserialize, Serialize)]
99#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
100pub struct VideoInfo {
101    /// The duration of the video in milliseconds.
102    #[serde(
103        with = "ruma_common::serde::duration::opt_ms",
104        default,
105        skip_serializing_if = "Option::is_none"
106    )]
107    pub duration: Option<Duration>,
108
109    /// The height of the video in pixels.
110    #[serde(rename = "h", skip_serializing_if = "Option::is_none")]
111    pub height: Option<UInt>,
112
113    /// The width of the video in pixels.
114    #[serde(rename = "w", skip_serializing_if = "Option::is_none")]
115    pub width: Option<UInt>,
116
117    /// The mimetype of the video, e.g. "video/mp4".
118    #[serde(skip_serializing_if = "Option::is_none")]
119    pub mimetype: Option<String>,
120
121    /// The size of the video in bytes.
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub size: Option<UInt>,
124
125    /// Metadata about the image referred to in `thumbnail_source`.
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub thumbnail_info: Option<Box<ThumbnailInfo>>,
128
129    /// The source of the thumbnail of the video clip.
130    #[serde(
131        flatten,
132        with = "crate::room::thumbnail_source_serde",
133        skip_serializing_if = "Option::is_none"
134    )]
135    pub thumbnail_source: Option<MediaSource>,
136
137    /// The [BlurHash](https://blurha.sh) for this video.
138    ///
139    /// This uses the unstable prefix in
140    /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448).
141    #[cfg(feature = "unstable-msc2448")]
142    #[serde(rename = "xyz.amorgan.blurhash", skip_serializing_if = "Option::is_none")]
143    pub blurhash: Option<String>,
144
145    /// The [ThumbHash](https://evanw.github.io/thumbhash/) for this video.
146    ///
147    /// This uses the unstable prefix in
148    /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448).
149    #[cfg(feature = "unstable-msc2448")]
150    #[serde(rename = "xyz.amorgan.thumbhash", skip_serializing_if = "Option::is_none")]
151    pub thumbhash: Option<Base64>,
152}
153
154impl VideoInfo {
155    /// Creates an empty `VideoInfo`.
156    pub fn new() -> Self {
157        Self::default()
158    }
159}