ruma_events/room/message/video.rs
1use std::time::Duration;
2
3use js_int::UInt;
4use ruma_common::OwnedMxcUri;
5#[cfg(feature = "unstable-msc2448")]
6use ruma_common::serde::Base64;
7use serde::{Deserialize, Serialize};
8
9use super::FormattedBody;
10use crate::room::{
11 EncryptedFile, MediaSource, ThumbnailInfo,
12 message::media_caption::{caption, formatted_caption},
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 /// Whether this video should be displayed as a Circle.
47 ///
48 /// Defaults to `false`.
49 ///
50 /// This uses the unstable prefix in
51 /// [MSC4546](https://github.com/matrix-org/matrix-spec-proposals/pull/4546).
52 #[cfg(feature = "unstable-msc4546")]
53 #[serde(
54 default,
55 rename = "org.interferolog.circle",
56 skip_serializing_if = "ruma_common::serde::is_default",
57 deserialize_with = "ruma_common::serde::default_on_error"
58 )]
59 pub circle: bool,
60}
61
62impl VideoMessageEventContent {
63 /// Creates a new `VideoMessageEventContent` with the given body and source.
64 pub fn new(body: String, source: MediaSource) -> Self {
65 Self {
66 body,
67 formatted: None,
68 filename: None,
69 source,
70 info: None,
71 #[cfg(feature = "unstable-msc4546")]
72 circle: false,
73 }
74 }
75
76 /// Creates a new non-encrypted `VideoMessageEventContent` with the given body and url.
77 pub fn plain(body: String, url: OwnedMxcUri) -> Self {
78 Self::new(body, MediaSource::Plain(url))
79 }
80
81 /// Creates a new encrypted `VideoMessageEventContent` with the given body and encrypted
82 /// file.
83 pub fn encrypted(body: String, file: EncryptedFile) -> Self {
84 Self::new(body, MediaSource::Encrypted(Box::new(file)))
85 }
86
87 /// Creates a new `VideoMessageEventContent` from `self` with the `info` field set to the given
88 /// value.
89 ///
90 /// Since the field is public, you can also assign to it directly. This method merely acts
91 /// as a shorthand for that, because it is very common to set this field.
92 pub fn info(self, info: impl Into<Option<Box<VideoInfo>>>) -> Self {
93 Self { info: info.into(), ..self }
94 }
95
96 /// Creates a new `VideoMessageEventContent` from `self` with the `circle` field set to the
97 /// given value.
98 ///
99 /// Since the field is public, you can also assign to it directly. This method merely acts
100 /// as a shorthand for that.
101 #[cfg(feature = "unstable-msc4546")]
102 pub fn circle(self, circle: bool) -> Self {
103 Self { circle, ..self }
104 }
105
106 /// Computes the filename of the video as defined by the [spec](https://spec.matrix.org/v1.19/client-server-api/#media-captions).
107 ///
108 /// This differs from the `filename` field as this method falls back to the `body` field when
109 /// the `filename` field is not set.
110 pub fn filename(&self) -> &str {
111 self.filename.as_deref().unwrap_or(&self.body)
112 }
113
114 /// Returns the caption of the video as defined by the [spec](https://spec.matrix.org/v1.19/client-server-api/#media-captions).
115 ///
116 /// In short, this is the `body` field if the `filename` field exists and has a different value,
117 /// otherwise the media file does not have a caption.
118 pub fn caption(&self) -> Option<&str> {
119 caption(&self.body, self.filename.as_deref())
120 }
121
122 /// Returns the formatted caption of the video as defined by the [spec](https://spec.matrix.org/v1.19/client-server-api/#media-captions).
123 ///
124 /// This is the same as `caption`, but returns the formatted body instead of the plain body.
125 pub fn formatted_caption(&self) -> Option<&FormattedBody> {
126 formatted_caption(&self.body, self.formatted.as_ref(), self.filename.as_deref())
127 }
128}
129
130/// Metadata about a video.
131#[derive(Clone, Debug, Default, Deserialize, Serialize)]
132#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
133pub struct VideoInfo {
134 /// The duration of the video in milliseconds.
135 #[serde(
136 with = "ruma_common::serde::duration::opt_ms",
137 default,
138 skip_serializing_if = "Option::is_none"
139 )]
140 pub duration: Option<Duration>,
141
142 /// The height of the video in pixels.
143 #[serde(rename = "h", skip_serializing_if = "Option::is_none")]
144 pub height: Option<UInt>,
145
146 /// The width of the video in pixels.
147 #[serde(rename = "w", skip_serializing_if = "Option::is_none")]
148 pub width: Option<UInt>,
149
150 /// The mimetype of the video, e.g. "video/mp4".
151 #[serde(skip_serializing_if = "Option::is_none")]
152 pub mimetype: Option<String>,
153
154 /// The size of the video in bytes.
155 #[serde(skip_serializing_if = "Option::is_none")]
156 pub size: Option<UInt>,
157
158 /// Metadata about the image referred to in `thumbnail_source`.
159 #[serde(skip_serializing_if = "Option::is_none")]
160 pub thumbnail_info: Option<Box<ThumbnailInfo>>,
161
162 /// The source of the thumbnail of the video clip.
163 #[serde(
164 flatten,
165 with = "crate::room::thumbnail_source_serde",
166 skip_serializing_if = "Option::is_none"
167 )]
168 pub thumbnail_source: Option<MediaSource>,
169
170 /// The [BlurHash](https://blurha.sh) for this video.
171 ///
172 /// This uses the unstable prefix in
173 /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448).
174 #[cfg(feature = "unstable-msc2448")]
175 #[serde(rename = "xyz.amorgan.blurhash", skip_serializing_if = "Option::is_none")]
176 pub blurhash: Option<String>,
177
178 /// The [ThumbHash](https://evanw.github.io/thumbhash/) for this video.
179 ///
180 /// This uses the unstable prefix in
181 /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448).
182 #[cfg(feature = "unstable-msc2448")]
183 #[serde(rename = "xyz.amorgan.thumbhash", skip_serializing_if = "Option::is_none")]
184 pub thumbhash: Option<Base64>,
185}
186
187impl VideoInfo {
188 /// Creates an empty `VideoInfo`.
189 pub fn new() -> Self {
190 Self::default()
191 }
192}