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