events: Update types according to changes in MSC3551

This commit is contained in:
Kévin Commaille 2023-01-04 20:00:31 +01:00 committed by Kévin Commaille
parent 8477efb2ef
commit 2de1cecec6
11 changed files with 329 additions and 291 deletions

View File

@ -13,7 +13,7 @@ mod waveform_serde;
use waveform_serde::WaveformSerDeHelper;
use super::{file::FileContent, message::TextContentBlock, room::message::Relation};
use super::{file::FileContentBlock, message::TextContentBlock, room::message::Relation};
/// The payload for an extensible audio message.
///
@ -31,8 +31,8 @@ pub struct AudioEventContent {
pub text: TextContentBlock,
/// The file content of the message.
#[serde(rename = "m.file")]
pub file: FileContent,
#[serde(rename = "org.matrix.msc1767.file")]
pub file: FileContentBlock,
/// The audio content of the message.
#[serde(rename = "m.audio")]
@ -49,13 +49,13 @@ pub struct AudioEventContent {
impl AudioEventContent {
/// Creates a new `AudioEventContent` with the given text fallback and file.
pub fn new(text: TextContentBlock, file: FileContent) -> Self {
pub fn new(text: TextContentBlock, file: FileContentBlock) -> Self {
Self { text, file, audio: Default::default(), relates_to: None }
}
/// Creates a new `AudioEventContent` with the given plain text fallback representation and
/// file.
pub fn plain(text_fallback: impl Into<String>, file: FileContent) -> Self {
pub fn plain(text_fallback: impl Into<String>, file: FileContentBlock) -> Self {
Self {
text: TextContentBlock::plain(text_fallback),
file,

View File

@ -52,7 +52,8 @@ event_enum! {
#[ruma_enum(alias = "m.encrypted")]
"org.matrix.msc1767.encrypted" => super::encrypted,
#[cfg(feature = "unstable-msc3551")]
"m.file" => super::file,
#[ruma_enum(alias = "m.file")]
"org.matrix.msc1767.file" => super::file,
#[cfg(feature = "unstable-msc3552")]
"m.image" => super::image,
"m.key.verification.ready" => super::key::verification::ready,

View File

@ -23,15 +23,28 @@ use crate::{serde::Base64, OwnedMxcUri};
/// [`message`]: super::message
#[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.file", kind = MessageLike, without_relation)]
#[ruma_event(type = "org.matrix.msc1767.file", kind = MessageLike, without_relation)]
pub struct FileEventContent {
/// The text representation of the message.
#[serde(rename = "org.matrix.msc1767.text")]
pub text: TextContentBlock,
/// The file content of the message.
#[serde(rename = "m.file")]
pub file: FileContent,
#[serde(rename = "org.matrix.msc1767.file")]
pub file: FileContentBlock,
/// The caption of the message, if any.
#[serde(rename = "org.matrix.msc1767.caption", skip_serializing_if = "Option::is_none")]
pub caption: Option<CaptionContentBlock>,
/// Whether this message is automated.
#[cfg(feature = "unstable-msc3955")]
#[serde(
default,
skip_serializing_if = "crate::serde::is_default",
rename = "org.matrix.msc1767.automated"
)]
pub automated: bool,
/// Information about related messages.
#[serde(
@ -45,24 +58,30 @@ pub struct FileEventContent {
impl FileEventContent {
/// Creates a new non-encrypted `FileEventContent` with the given fallback representation, url
/// and file info.
pub fn plain(
text: TextContentBlock,
url: OwnedMxcUri,
info: Option<Box<FileContentInfo>>,
) -> Self {
Self { text, file: FileContent::plain(url, info), relates_to: None }
pub fn plain(text: TextContentBlock, url: OwnedMxcUri, name: String) -> Self {
Self {
text,
file: FileContentBlock::plain(url, name),
caption: None,
#[cfg(feature = "unstable-msc3955")]
automated: false,
relates_to: None,
}
}
/// Creates a new non-encrypted `FileEventContent` with the given plain text fallback
/// representation, url and file info.
pub fn plain_with_text(
text: impl Into<String>,
pub fn plain_with_plain_text(
plain_text: impl Into<String>,
url: OwnedMxcUri,
info: Option<Box<FileContentInfo>>,
name: String,
) -> Self {
Self {
text: TextContentBlock::plain(text),
file: FileContent::plain(url, info),
text: TextContentBlock::plain(plain_text),
file: FileContentBlock::plain(url, name),
caption: None,
#[cfg(feature = "unstable-msc3955")]
automated: false,
relates_to: None,
}
}
@ -72,74 +91,47 @@ impl FileEventContent {
pub fn encrypted(
text: TextContentBlock,
url: OwnedMxcUri,
name: String,
encryption_info: EncryptedContent,
info: Option<Box<FileContentInfo>>,
) -> Self {
Self { text, file: FileContent::encrypted(url, encryption_info, info), relates_to: None }
Self {
text,
file: FileContentBlock::encrypted(url, name, encryption_info),
caption: None,
#[cfg(feature = "unstable-msc3955")]
automated: false,
relates_to: None,
}
}
/// Creates a new encrypted `FileEventContent` with the given plain text fallback
/// representation, url, encryption info and file info.
pub fn encrypted_with_text(
text: impl Into<String>,
pub fn encrypted_with_plain_text(
plain_text: impl Into<String>,
url: OwnedMxcUri,
name: String,
encryption_info: EncryptedContent,
info: Option<Box<FileContentInfo>>,
) -> Self {
Self {
text: TextContentBlock::plain(text),
file: FileContent::encrypted(url, encryption_info, info),
text: TextContentBlock::plain(plain_text),
file: FileContentBlock::encrypted(url, name, encryption_info),
caption: None,
#[cfg(feature = "unstable-msc3955")]
automated: false,
relates_to: None,
}
}
}
/// File content.
/// A block for file content.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct FileContent {
pub struct FileContentBlock {
/// The URL to the file.
pub url: OwnedMxcUri,
/// Information about the uploaded file.
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub info: Option<Box<FileContentInfo>>,
/// Information on the encrypted file.
///
/// Required if the file is encrypted.
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub encryption_info: Option<Box<EncryptedContent>>,
}
impl FileContent {
/// Creates a new non-encrypted `FileContent` with the given url and file info.
pub fn plain(url: OwnedMxcUri, info: Option<Box<FileContentInfo>>) -> Self {
Self { url, info, encryption_info: None }
}
/// Creates a new encrypted `FileContent` with the given url, encryption info and file info.
pub fn encrypted(
url: OwnedMxcUri,
encryption_info: EncryptedContent,
info: Option<Box<FileContentInfo>>,
) -> Self {
Self { url, info, encryption_info: Some(Box::new(encryption_info)) }
}
/// Whether the file is encrypted.
pub fn is_encrypted(&self) -> bool {
self.encryption_info.is_some()
}
}
/// Information about a file content.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct FileContentInfo {
/// The original filename of the uploaded file.
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub name: String,
/// The mimetype of the file, e.g. "application/msword".
#[serde(skip_serializing_if = "Option::is_none")]
@ -148,12 +140,34 @@ pub struct FileContentInfo {
/// The size of the file in bytes.
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<UInt>,
/// Information on the encrypted file.
///
/// Required if the file is encrypted.
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub encryption_info: Option<Box<EncryptedContent>>,
}
impl FileContentInfo {
/// Creates an empty `FileContentInfo`.
pub fn new() -> Self {
Self::default()
impl FileContentBlock {
/// Creates a new non-encrypted `FileContentBlock` with the given url and name.
pub fn plain(url: OwnedMxcUri, name: String) -> Self {
Self { url, name, mimetype: None, size: None, encryption_info: None }
}
/// Creates a new encrypted `FileContentBlock` with the given url, name and encryption info.
pub fn encrypted(url: OwnedMxcUri, name: String, encryption_info: EncryptedContent) -> Self {
Self {
url,
name,
mimetype: None,
size: None,
encryption_info: Some(Box::new(encryption_info)),
}
}
/// Whether the file is encrypted.
pub fn is_encrypted(&self) -> bool {
self.encryption_info.is_some()
}
}
@ -218,3 +232,44 @@ impl From<&EncryptedFile> for EncryptedContent {
Self { key: key.to_owned(), iv: iv.to_owned(), hashes: hashes.to_owned(), v: v.to_owned() }
}
}
/// A block for caption content.
///
/// A caption is usually a text message that should be displayed next to some media content.
///
/// To construct a `CaptionContentBlock` with a custom [`TextContentBlock`], convert it with
/// `CaptionContentBlock::from()` / `.into()`.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct CaptionContentBlock {
/// The text message of the caption.
#[serde(rename = "org.matrix.msc1767.text")]
pub text: TextContentBlock,
}
impl CaptionContentBlock {
/// A convenience constructor to create a plain text caption content block.
pub fn plain(body: impl Into<String>) -> Self {
Self { text: TextContentBlock::plain(body) }
}
/// A convenience constructor to create an HTML caption content block.
pub fn html(body: impl Into<String>, html_body: impl Into<String>) -> Self {
Self { text: TextContentBlock::html(body, html_body) }
}
/// A convenience constructor to create a caption content block from Markdown.
///
/// The content includes an HTML message if some Markdown formatting was detected, otherwise
/// only a plain text message is included.
#[cfg(feature = "markdown")]
pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
Self { text: TextContentBlock::markdown(body) }
}
}
impl From<TextContentBlock> for CaptionContentBlock {
fn from(text: TextContentBlock) -> Self {
Self { text }
}
}

View File

@ -7,7 +7,7 @@ use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use super::{
file::{EncryptedContent, FileContent},
file::{EncryptedContent, FileContentBlock},
message::TextContentBlock,
room::message::Relation,
};
@ -29,8 +29,8 @@ pub struct ImageEventContent {
pub text: TextContentBlock,
/// The file content of the message.
#[serde(rename = "m.file")]
pub file: FileContent,
#[serde(rename = "org.matrix.msc1767.file")]
pub file: FileContentBlock,
/// The image content of the message.
#[serde(rename = "m.image")]
@ -56,7 +56,7 @@ pub struct ImageEventContent {
impl ImageEventContent {
/// Creates a new `ImageEventContent` with the given fallback representation and
/// file.
pub fn new(text: TextContentBlock, file: FileContent) -> Self {
pub fn new(text: TextContentBlock, file: FileContentBlock) -> Self {
Self {
text,
file,
@ -69,7 +69,7 @@ impl ImageEventContent {
/// Creates a new `ImageEventContent` with the given plain text fallback representation and
/// file.
pub fn plain(text_fallback: impl Into<String>, file: FileContent) -> Self {
pub fn plain(text_fallback: impl Into<String>, file: FileContentBlock) -> Self {
Self {
text: TextContentBlock::plain(text_fallback),
file,

View File

@ -9,7 +9,8 @@ use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use super::{
file::FileContent, image::ThumbnailContent, message::TextContentBlock, room::message::Relation,
file::FileContentBlock, image::ThumbnailContent, message::TextContentBlock,
room::message::Relation,
};
/// The payload for an extensible video message.
@ -28,8 +29,8 @@ pub struct VideoEventContent {
pub text: TextContentBlock,
/// The file content of the message.
#[serde(rename = "m.file")]
pub file: FileContent,
#[serde(rename = "org.matrix.msc1767.file")]
pub file: FileContentBlock,
/// The video content of the message.
#[serde(rename = "m.video")]
@ -54,7 +55,7 @@ pub struct VideoEventContent {
impl VideoEventContent {
/// Creates a new `VideoEventContent` with the given fallback representation and file.
pub fn new(text: TextContentBlock, file: FileContent) -> Self {
pub fn new(text: TextContentBlock, file: FileContentBlock) -> Self {
Self {
text,
file,
@ -67,7 +68,7 @@ impl VideoEventContent {
/// Creates a new `VideoEventContent` with the given plain text fallback representation and
/// file.
pub fn plain(text: impl Into<String>, file: FileContent) -> Self {
pub fn plain(text: impl Into<String>, file: FileContentBlock) -> Self {
Self {
text: TextContentBlock::plain(text),
file,

View File

@ -6,7 +6,7 @@ use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use super::{
audio::AudioContent, file::FileContent, message::TextContentBlock, room::message::Relation,
audio::AudioContent, file::FileContentBlock, message::TextContentBlock, room::message::Relation,
};
/// The payload for an extensible voice message.
@ -26,8 +26,8 @@ pub struct VoiceEventContent {
pub text: TextContentBlock,
/// The file content of the message.
#[serde(rename = "m.file")]
pub file: FileContent,
#[serde(rename = "org.matrix.msc1767.file")]
pub file: FileContentBlock,
/// The audio content of the message.
#[serde(rename = "m.audio")]
@ -48,13 +48,13 @@ pub struct VoiceEventContent {
impl VoiceEventContent {
/// Creates a new `VoiceEventContent` with the given fallback representation and file.
pub fn new(text: TextContentBlock, file: FileContent) -> Self {
pub fn new(text: TextContentBlock, file: FileContentBlock) -> Self {
Self { text, file, audio: Default::default(), voice: Default::default(), relates_to: None }
}
/// Creates a new `VoiceEventContent` with the given plain text fallback representation and
/// file.
pub fn plain(text: impl Into<String>, file: FileContent) -> Self {
pub fn plain(text: impl Into<String>, file: FileContentBlock) -> Self {
Self {
text: TextContentBlock::plain(text),
file,

View File

@ -9,7 +9,7 @@ use ruma_common::{
event_id,
events::{
audio::{Amplitude, AudioContent, AudioEventContent, Waveform, WaveformError},
file::{EncryptedContentInit, FileContent, FileContentInfo},
file::{EncryptedContentInit, FileContentBlock},
message::TextContentBlock,
relation::InReplyTo,
room::{message::Relation, JsonWebKeyInit},
@ -57,7 +57,10 @@ fn waveform_deserialization_clamp_amplitude() {
fn plain_content_serialization() {
let event_content = AudioEventContent::plain(
"Upload: my_sound.ogg",
FileContent::plain(mxc_uri!("mxc://notareal.hs/abcdef").to_owned(), None),
FileContentBlock::plain(
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
"my_sound.ogg".to_owned(),
),
);
assert_eq!(
@ -66,8 +69,9 @@ fn plain_content_serialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_sound.ogg" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_sound.ogg",
},
"m.audio": {}
})
@ -78,8 +82,9 @@ fn plain_content_serialization() {
fn encrypted_content_serialization() {
let event_content = AudioEventContent::plain(
"Upload: my_sound.ogg",
FileContent::encrypted(
FileContentBlock::encrypted(
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
"my_sound.ogg".to_owned(),
EncryptedContentInit {
key: JsonWebKeyInit {
kty: "oct".to_owned(),
@ -98,7 +103,6 @@ fn encrypted_content_serialization() {
v: "v2".to_owned(),
}
.into(),
None,
),
);
@ -108,8 +112,9 @@ fn encrypted_content_serialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_sound.ogg" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_sound.ogg",
"key": {
"kty": "oct",
"key_ops": ["encrypt", "decrypt"],
@ -121,7 +126,7 @@ fn encrypted_content_serialization() {
"hashes": {
"sha256": "aWOHudBnDkJ9IwaR1Nd8XKoI7DOrqDTwt6xDPfVGN6Q"
},
"v": "v2"
"v": "v2",
},
"m.audio": {}
})
@ -136,16 +141,15 @@ fn event_serialization() {
"Upload: my_mix.mp3",
"Upload: <strong>my_mix.mp3</strong>",
),
FileContent::plain(
assign!(
FileContentBlock::plain(
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
Some(Box::new(assign!(
FileContentInfo::new(),
"my_mix.mp3".to_owned()
),
{
name: Some("my_mix.mp3".to_owned()),
mimetype: Some("audio/mp3".to_owned()),
size: Some(uint!(897_774)),
}
))),
)
),
{
@ -168,7 +172,7 @@ fn event_serialization() {
{ "mimetype": "text/html", "body": "Upload: <strong>my_mix.mp3</strong>" },
{ "body": "Upload: my_mix.mp3"},
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_mix.mp3",
"mimetype": "audio/mp3",
@ -192,8 +196,9 @@ fn plain_content_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_new_song.webm" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_new_song.webm",
},
"m.audio": {
"waveform": [
@ -257,6 +262,7 @@ fn plain_content_deserialization() {
assert_eq!(content.text.find_plain(), Some("Upload: my_new_song.webm"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
assert_eq!(content.file.name, "my_new_song.webm");
let waveform = content.audio.waveform.unwrap();
assert_eq!(waveform.amplitudes().len(), 52);
}
@ -267,8 +273,9 @@ fn encrypted_content_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_file.txt" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_file.txt",
"key": {
"kty": "oct",
"key_ops": ["encrypt", "decrypt"],
@ -289,6 +296,7 @@ fn encrypted_content_deserialization() {
assert_eq!(content.text.find_plain(), Some("Upload: my_file.txt"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
assert_eq!(content.file.name, "my_file.txt");
assert!(content.file.encryption_info.is_some());
}
@ -299,7 +307,7 @@ fn message_event_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: airplane_sound.opus" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "airplane_sound.opus",
"mimetype": "audio/opus",
@ -331,10 +339,9 @@ fn message_event_deserialization() {
assert_eq!(content.text.find_plain(), Some("Upload: airplane_sound.opus"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
let info = content.file.info.unwrap();
assert_eq!(info.name.as_deref(), Some("airplane_sound.opus"));
assert_eq!(info.mimetype.as_deref(), Some("audio/opus"));
assert_eq!(info.size, Some(uint!(123_774)));
assert_eq!(content.file.name, "airplane_sound.opus");
assert_eq!(content.file.mimetype.as_deref(), Some("audio/opus"));
assert_eq!(content.file.size, Some(uint!(123_774)));
assert_eq!(content.audio.duration, Some(Duration::from_millis(5_300)));
assert_matches!(content.audio.waveform, None);
}

View File

@ -1,12 +1,11 @@
#![cfg(feature = "unstable-msc3551")]
use assert_matches::assert_matches;
use assign::assign;
use js_int::uint;
use ruma_common::{
event_id,
events::{
file::{EncryptedContentInit, FileContentInfo, FileEventContent},
file::{EncryptedContentInit, FileEventContent},
message::TextContentBlock,
relation::InReplyTo,
room::{message::Relation, JsonWebKeyInit},
@ -20,10 +19,10 @@ use serde_json::{from_value as from_json_value, json, to_value as to_json_value}
#[test]
fn plain_content_serialization() {
let event_content = FileEventContent::plain_with_text(
let event_content = FileEventContent::plain_with_plain_text(
"Upload: my_file.txt",
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
None,
"my_file.txt".to_owned(),
);
assert_eq!(
@ -32,8 +31,9 @@ fn plain_content_serialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_file.txt" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_file.txt",
}
})
);
@ -41,9 +41,10 @@ fn plain_content_serialization() {
#[test]
fn encrypted_content_serialization() {
let event_content = FileEventContent::encrypted_with_text(
let event_content = FileEventContent::encrypted_with_plain_text(
"Upload: my_file.txt",
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
"my_file.txt".to_owned(),
EncryptedContentInit {
key: JsonWebKeyInit {
kty: "oct".to_owned(),
@ -62,7 +63,6 @@ fn encrypted_content_serialization() {
v: "v2".to_owned(),
}
.into(),
None,
);
assert_eq!(
@ -71,8 +71,9 @@ fn encrypted_content_serialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_file.txt" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_file.txt",
"key": {
"kty": "oct",
"key_ops": ["encrypt", "decrypt"],
@ -84,7 +85,7 @@ fn encrypted_content_serialization() {
"hashes": {
"sha256": "aWOHudBnDkJ9IwaR1Nd8XKoI7DOrqDTwt6xDPfVGN6Q"
},
"v": "v2"
"v": "v2",
}
})
);
@ -92,28 +93,16 @@ fn encrypted_content_serialization() {
#[test]
fn file_event_serialization() {
let content = assign!(
FileEventContent::plain(
TextContentBlock::html(
"Upload: my_file.txt",
"Upload: <strong>my_file.txt</strong>",
),
let mut content = FileEventContent::plain(
TextContentBlock::html("Upload: my_file.txt", "Upload: <strong>my_file.txt</strong>"),
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
Some(Box::new(assign!(
FileContentInfo::new(),
{
name: Some("my_file.txt".to_owned()),
mimetype: Some("text/plain".to_owned()),
size: Some(uint!(774)),
}
))),
),
{
relates_to: Some(Relation::Reply {
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
}),
}
"my_file.txt".to_owned(),
);
content.file.mimetype = Some("text/plain".to_owned());
content.file.size = Some(uint!(774));
content.relates_to = Some(Relation::Reply {
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
});
assert_eq!(
to_json_value(&content).unwrap(),
@ -122,7 +111,7 @@ fn file_event_serialization() {
{ "mimetype": "text/html", "body": "Upload: <strong>my_file.txt</strong>" },
{ "body": "Upload: my_file.txt" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_file.txt",
"mimetype": "text/plain",
@ -143,8 +132,9 @@ fn plain_content_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_file.txt" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_file.txt",
}
});
@ -152,6 +142,7 @@ fn plain_content_deserialization() {
assert_eq!(content.text.find_plain(), Some("Upload: my_file.txt"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
assert_eq!(content.file.name, "my_file.txt");
}
#[test]
@ -160,8 +151,9 @@ fn encrypted_content_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_file.txt" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "",
"key": {
"kty": "oct",
"key_ops": ["encrypt", "decrypt"],
@ -181,6 +173,7 @@ fn encrypted_content_deserialization() {
assert_eq!(content.text.find_plain(), Some("Upload: my_file.txt"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
assert_eq!(content.file.name, "");
assert!(content.file.encryption_info.is_some());
}
@ -192,7 +185,7 @@ fn message_event_deserialization() {
{ "body": "Upload: <strong>my_file.txt</strong>", "mimetype": "text/html"},
{ "body": "Upload: my_file.txt", "mimetype": "text/plain"},
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_file.txt",
"mimetype": "text/plain",
@ -203,7 +196,7 @@ fn message_event_deserialization() {
"origin_server_ts": 134_829_848,
"room_id": "!roomid:notareal.hs",
"sender": "@user:notareal.hs",
"type": "m.file",
"type": "org.matrix.msc1767.file",
});
let message_event = assert_matches!(
@ -215,10 +208,9 @@ fn message_event_deserialization() {
assert_eq!(content.text.find_plain(), Some("Upload: my_file.txt"));
assert_eq!(content.text.find_html(), Some("Upload: <strong>my_file.txt</strong>"));
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
let info = content.file.info.unwrap();
assert_eq!(info.name.as_deref(), Some("my_file.txt"));
assert_eq!(info.mimetype.as_deref(), Some("text/plain"));
assert_eq!(info.size, Some(uint!(774)));
assert_eq!(content.file.name, "my_file.txt");
assert_eq!(content.file.mimetype.as_deref(), Some("text/plain"));
assert_eq!(content.file.size, Some(uint!(774)));
assert_eq!(message_event.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(134_829_848)));
assert_eq!(message_event.room_id, "!roomid:notareal.hs");
assert_eq!(message_event.sender, "@user:notareal.hs");

View File

@ -6,7 +6,7 @@ use js_int::uint;
use ruma_common::{
event_id,
events::{
file::{EncryptedContentInit, FileContent, FileContentInfo},
file::{EncryptedContentInit, FileContentBlock},
image::{
ImageContent, ImageEventContent, ThumbnailContent, ThumbnailFileContent,
ThumbnailFileContentInfo,
@ -26,7 +26,10 @@ use serde_json::{from_value as from_json_value, json, to_value as to_json_value}
fn plain_content_serialization() {
let event_content = ImageEventContent::plain(
"Upload: my_image.jpg",
FileContent::plain(mxc_uri!("mxc://notareal.hs/abcdef").to_owned(), None),
FileContentBlock::plain(
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
"my_image.jpg".to_owned(),
),
);
assert_eq!(
@ -35,8 +38,9 @@ fn plain_content_serialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_image.jpg" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_image.jpg",
},
"m.image": {}
})
@ -47,8 +51,9 @@ fn plain_content_serialization() {
fn encrypted_content_serialization() {
let event_content = ImageEventContent::plain(
"Upload: my_image.jpg",
FileContent::encrypted(
FileContentBlock::encrypted(
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
"my_image.jpg".to_owned(),
EncryptedContentInit {
key: JsonWebKeyInit {
kty: "oct".to_owned(),
@ -67,7 +72,6 @@ fn encrypted_content_serialization() {
v: "v2".to_owned(),
}
.into(),
None,
),
);
@ -77,8 +81,9 @@ fn encrypted_content_serialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_image.jpg" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_image.jpg",
"key": {
"kty": "oct",
"key_ops": ["encrypt", "decrypt"],
@ -99,42 +104,31 @@ fn encrypted_content_serialization() {
#[test]
fn image_event_serialization() {
let content = assign!(
ImageEventContent::new(
TextContentBlock::html(
"Upload: my_house.jpg",
"Upload: <strong>my_house.jpg</strong>",
),
FileContent::plain(
let mut content = ImageEventContent::new(
TextContentBlock::html("Upload: my_house.jpg", "Upload: <strong>my_house.jpg</strong>"),
FileContentBlock::plain(
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
Some(Box::new(assign!(
FileContentInfo::new(),
{
name: Some("my_house.jpg".to_owned()),
mimetype: Some("image/jpeg".to_owned()),
size: Some(uint!(897_774)),
}
))),
)
"my_house.jpg".to_owned(),
),
{
image: Box::new(ImageContent::with_size(uint!(1920), uint!(1080))),
thumbnail: vec![ThumbnailContent::new(
);
content.file.mimetype = Some("image/jpeg".to_owned());
content.file.size = Some(uint!(897_774));
content.image = Box::new(ImageContent::with_size(uint!(1920), uint!(1080)));
content.thumbnail = vec![ThumbnailContent::new(
ThumbnailFileContent::plain(
mxc_uri!("mxc://notareal.hs/thumbnail").to_owned(),
Some(Box::new(assign!(ThumbnailFileContentInfo::new(), {
mimetype: Some("image/jpeg".to_owned()),
size: Some(uint!(334_593)),
})))
}))),
),
None
)],
caption: TextContentBlock::plain("This is my house"),
relates_to: Some(Relation::Reply {
None,
)];
content.caption = TextContentBlock::plain("This is my house");
content.relates_to = Some(Relation::Reply {
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
}),
}
);
});
assert_eq!(
to_json_value(&content).unwrap(),
@ -143,7 +137,7 @@ fn image_event_serialization() {
{ "mimetype": "text/html", "body": "Upload: <strong>my_house.jpg</strong>" },
{ "body": "Upload: my_house.jpg" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_house.jpg",
"mimetype": "image/jpeg",
@ -180,8 +174,9 @@ fn plain_content_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_cat.png" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_cat.png",
},
"m.image": {
"width": 668,
@ -197,6 +192,7 @@ fn plain_content_deserialization() {
assert_eq!(content.text.find_plain(), Some("Upload: my_cat.png"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
assert_eq!(content.file.name, "my_cat.png");
assert_matches!(content.file.encryption_info, None);
assert_eq!(content.image.width, Some(uint!(668)));
assert_eq!(content.image.height, None);
@ -211,8 +207,9 @@ fn encrypted_content_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_cat.png" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_cat.png",
"key": {
"kty": "oct",
"key_ops": ["encrypt", "decrypt"],
@ -238,6 +235,7 @@ fn encrypted_content_deserialization() {
assert_eq!(content.text.find_plain(), Some("Upload: my_cat.png"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
assert_eq!(content.file.name, "my_cat.png");
assert!(content.file.encryption_info.is_some());
assert_eq!(content.image.width, None);
assert_eq!(content.image.height, None);
@ -253,7 +251,7 @@ fn message_event_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_gnome.webp" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_gnome.webp",
"mimetype": "image/webp",
@ -285,10 +283,9 @@ fn message_event_deserialization() {
assert_eq!(content.text.find_plain(), Some("Upload: my_gnome.webp"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
let info = content.file.info.unwrap();
assert_eq!(info.name.as_deref(), Some("my_gnome.webp"));
assert_eq!(info.mimetype.as_deref(), Some("image/webp"));
assert_eq!(info.size, Some(uint!(123_774)));
assert_eq!(content.file.name, "my_gnome.webp");
assert_eq!(content.file.mimetype.as_deref(), Some("image/webp"));
assert_eq!(content.file.size, Some(uint!(123_774)));
assert_eq!(content.image.width, Some(uint!(1300)));
assert_eq!(content.image.height, Some(uint!(837)));
assert_eq!(content.thumbnail.len(), 0);

View File

@ -8,7 +8,7 @@ use js_int::uint;
use ruma_common::{
event_id,
events::{
file::{EncryptedContentInit, FileContent, FileContentInfo},
file::{EncryptedContentInit, FileContentBlock},
image::{ThumbnailContent, ThumbnailFileContent, ThumbnailFileContentInfo},
message::TextContentBlock,
relation::InReplyTo,
@ -26,7 +26,10 @@ use serde_json::{from_value as from_json_value, json, to_value as to_json_value}
fn plain_content_serialization() {
let event_content = VideoEventContent::plain(
"Upload: my_video.webm",
FileContent::plain(mxc_uri!("mxc://notareal.hs/abcdef").to_owned(), None),
FileContentBlock::plain(
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
"my_video.webm".to_owned(),
),
);
assert_eq!(
@ -35,8 +38,9 @@ fn plain_content_serialization() {
"org.matrix.msc1767.text": [
{"body": "Upload: my_video.webm" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_video.webm",
},
"m.video": {}
})
@ -47,8 +51,9 @@ fn plain_content_serialization() {
fn encrypted_content_serialization() {
let event_content = VideoEventContent::plain(
"Upload: my_video.webm",
FileContent::encrypted(
FileContentBlock::encrypted(
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
"my_video.webm".to_owned(),
EncryptedContentInit {
key: JsonWebKeyInit {
kty: "oct".to_owned(),
@ -67,7 +72,6 @@ fn encrypted_content_serialization() {
v: "v2".to_owned(),
}
.into(),
None,
),
);
@ -77,8 +81,9 @@ fn encrypted_content_serialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_video.webm" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_video.webm",
"key": {
"kty": "oct",
"key_ops": ["encrypt", "decrypt"],
@ -99,49 +104,41 @@ fn encrypted_content_serialization() {
#[test]
fn event_serialization() {
let content = assign!(
VideoEventContent::new(
let mut content = VideoEventContent::new(
TextContentBlock::html(
"Upload: my_lava_lamp.webm",
"Upload: <strong>my_lava_lamp.webm</strong>",
),
FileContent::plain(
FileContentBlock::plain(
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
Some(Box::new(assign!(
FileContentInfo::new(),
{
name: Some("my_lava_lamp.webm".to_owned()),
mimetype: Some("video/webm".to_owned()),
size: Some(uint!(1_897_774)),
}
))),
)
"my_lava_lamp.webm".to_owned(),
),
{
video: Box::new(assign!(
);
content.file.mimetype = Some("video/webm".to_owned());
content.file.size = Some(uint!(1_897_774));
content.video = Box::new(assign!(
VideoContent::new(),
{
width: Some(uint!(1920)),
height: Some(uint!(1080)),
duration: Some(Duration::from_secs(15)),
}
)),
thumbnail: vec![ThumbnailContent::new(
));
content.thumbnail = vec![ThumbnailContent::new(
ThumbnailFileContent::plain(
mxc_uri!("mxc://notareal.hs/thumbnail").to_owned(),
Some(Box::new(assign!(ThumbnailFileContentInfo::new(), {
mimetype: Some("image/jpeg".to_owned()),
size: Some(uint!(334_593)),
})))
}))),
),
None
)],
caption: TextContentBlock::plain("This is my awesome vintage lava lamp"),
relates_to: Some(Relation::Reply {
None,
)];
content.caption = TextContentBlock::plain("This is my awesome vintage lava lamp");
content.relates_to = Some(Relation::Reply {
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
}),
}
);
});
assert_eq!(
to_json_value(&content).unwrap(),
@ -150,7 +147,7 @@ fn event_serialization() {
{ "mimetype": "text/html", "body": "Upload: <strong>my_lava_lamp.webm</strong>" },
{ "body": "Upload: my_lava_lamp.webm" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_lava_lamp.webm",
"mimetype": "video/webm",
@ -188,8 +185,9 @@ fn plain_content_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Video: my_cat.mp4" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_cat.mp4",
},
"m.video": {
"duration": 5_668,
@ -205,6 +203,7 @@ fn plain_content_deserialization() {
assert_eq!(content.text.find_plain(), Some("Video: my_cat.mp4"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
assert_eq!(content.file.name, "my_cat.mp4");
assert_matches!(content.file.encryption_info, None);
assert_eq!(content.video.width, None);
assert_eq!(content.video.height, None);
@ -220,8 +219,9 @@ fn encrypted_content_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Video: my_cat.mp4" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_cat.mp4",
"key": {
"kty": "oct",
"key_ops": ["encrypt", "decrypt"],
@ -247,6 +247,7 @@ fn encrypted_content_deserialization() {
assert_eq!(content.text.find_plain(), Some("Video: my_cat.mp4"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
assert_eq!(content.file.name, "my_cat.mp4");
assert!(content.file.encryption_info.is_some());
assert_eq!(content.video.width, None);
assert_eq!(content.video.height, None);
@ -263,7 +264,7 @@ fn message_event_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Upload: my_gnome.webm" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "my_gnome.webm",
"mimetype": "video/webm",
@ -295,13 +296,11 @@ fn message_event_deserialization() {
assert_eq!(content.text.find_plain(), Some("Upload: my_gnome.webm"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
assert_eq!(content.file.name, "my_gnome.webm");
assert_eq!(content.file.mimetype.as_deref(), Some("video/webm"));
assert_eq!(content.file.size, Some(uint!(123_774)));
assert_eq!(content.video.width, Some(uint!(1300)));
assert_eq!(content.video.height, Some(uint!(837)));
assert_eq!(content.video.duration, None);
assert_eq!(content.thumbnail.len(), 0);
let info = content.file.info.unwrap();
assert_eq!(info.name.as_deref(), Some("my_gnome.webm"));
assert_eq!(info.mimetype.as_deref(), Some("video/webm"));
assert_eq!(info.size, Some(uint!(123_774)));
}

View File

@ -8,12 +8,8 @@ use js_int::uint;
use ruma_common::{
event_id,
events::{
audio::AudioContent,
file::{FileContent, FileContentInfo},
relation::InReplyTo,
room::message::Relation,
voice::VoiceEventContent,
AnyMessageLikeEvent, MessageLikeEvent,
audio::AudioContent, file::FileContentBlock, relation::InReplyTo, room::message::Relation,
voice::VoiceEventContent, AnyMessageLikeEvent, MessageLikeEvent,
},
mxc_uri,
serde::CanBeEmpty,
@ -23,33 +19,25 @@ use serde_json::{from_value as from_json_value, json, to_value as to_json_value}
#[test]
fn event_serialization() {
let content = assign!(
VoiceEventContent::plain(
let mut content = VoiceEventContent::plain(
"Voice message",
FileContent::plain(
FileContentBlock::plain(
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
Some(Box::new(assign!(
FileContentInfo::new(),
{
name: Some("voice_message.ogg".to_owned()),
mimetype: Some("audio/opus".to_owned()),
size: Some(uint!(897_774)),
}
))),
)
"voice_message.ogg".to_owned(),
),
{
audio: assign!(
);
content.file.mimetype = Some("audio/opus".to_owned());
content.file.size = Some(uint!(897_774));
content.audio = assign!(
AudioContent::new(),
{
duration: Some(Duration::from_secs(23))
}
),
relates_to: Some(Relation::Reply {
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
}),
}
);
content.relates_to = Some(Relation::Reply {
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
});
assert_eq!(
to_json_value(&content).unwrap(),
@ -57,7 +45,7 @@ fn event_serialization() {
"org.matrix.msc1767.text": [
{ "body": "Voice message" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "voice_message.ogg",
"mimetype": "audio/opus",
@ -83,7 +71,7 @@ fn message_event_deserialization() {
"org.matrix.msc1767.text": [
{ "body": "Voice message" },
],
"m.file": {
"org.matrix.msc1767.file": {
"url": "mxc://notareal.hs/abcdef",
"name": "voice_message.ogg",
"mimetype": "audio/opus",
@ -115,11 +103,9 @@ fn message_event_deserialization() {
assert_eq!(content.text.find_plain(), Some("Voice message"));
assert_eq!(content.text.find_html(), None);
assert_eq!(content.file.url, "mxc://notareal.hs/abcdef");
assert_eq!(content.file.name, "voice_message.ogg");
assert_eq!(content.file.mimetype.as_deref(), Some("audio/opus"));
assert_eq!(content.file.size, Some(uint!(123_774)));
assert_eq!(content.audio.duration, Some(Duration::from_millis(5_300)));
assert_matches!(content.audio.waveform, None);
let info = content.file.info.unwrap();
assert_eq!(info.name.as_deref(), Some("voice_message.ogg"));
assert_eq!(info.mimetype.as_deref(), Some("audio/opus"));
assert_eq!(info.size, Some(uint!(123_774)));
}