events: Replace Thumbnails with a Vec

This commit is contained in:
Kévin Commaille 2022-03-25 16:37:58 +01:00 committed by Kévin Commaille
parent bf6687c92d
commit e4463dda22
4 changed files with 13 additions and 40 deletions

View File

@ -31,8 +31,8 @@ pub struct ImageEventContent {
pub image: Box<ImageContent>, pub image: Box<ImageContent>,
/// The thumbnails of the message. /// The thumbnails of the message.
#[serde(rename = "m.thumbnail", default, skip_serializing_if = "Thumbnails::is_empty")] #[serde(rename = "m.thumbnail", default, skip_serializing_if = "Vec::is_empty")]
pub thumbnail: Thumbnails, pub thumbnail: Vec<ThumbnailContent>,
/// The captions of the message. /// The captions of the message.
#[serde(rename = "m.caption", default, skip_serializing_if = "Captions::is_empty")] #[serde(rename = "m.caption", default, skip_serializing_if = "Captions::is_empty")]
@ -151,31 +151,6 @@ impl ThumbnailContent {
} }
} }
/// An array of thumbnails.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Thumbnails(pub(crate) Vec<ThumbnailContent>);
impl Thumbnails {
/// Creates a new `Thumbnails` with the given thumbnails.
///
/// The thumbnails must be ordered by most preferred first.
pub fn new(thumbnails: &[ThumbnailContent]) -> Self {
Self(thumbnails.to_owned())
}
/// Get the thumbnails.
///
/// The thumbnails are ordered by most preferred first.
pub fn thumbnails(&self) -> &[ThumbnailContent] {
&self.0
}
/// Whether this is empty.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
/// An array of captions. /// An array of captions.
#[derive(Clone, Debug, Default, Serialize, Deserialize)] #[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Captions(pub(crate) Vec<Text>); pub struct Captions(pub(crate) Vec<Text>);

View File

@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
use super::{ use super::{
file::FileContent, file::FileContent,
image::{Captions, Thumbnails}, image::{Captions, ThumbnailContent},
message::MessageContent, message::MessageContent,
room::message::Relation, room::message::Relation,
}; };
@ -33,8 +33,8 @@ pub struct VideoEventContent {
pub video: Box<VideoContent>, pub video: Box<VideoContent>,
/// The thumbnails of the message. /// The thumbnails of the message.
#[serde(rename = "m.thumbnail", default, skip_serializing_if = "Thumbnails::is_empty")] #[serde(rename = "m.thumbnail", default, skip_serializing_if = "Vec::is_empty")]
pub thumbnail: Thumbnails, pub thumbnail: Vec<ThumbnailContent>,
/// The captions of the message. /// The captions of the message.
#[serde(rename = "m.caption", default, skip_serializing_if = "Captions::is_empty")] #[serde(rename = "m.caption", default, skip_serializing_if = "Captions::is_empty")]

View File

@ -9,7 +9,7 @@ use ruma_common::{
file::{EncryptedContentInit, FileContent, FileContentInfo}, file::{EncryptedContentInit, FileContent, FileContentInfo},
image::{ image::{
Captions, ImageContent, ImageEventContent, ThumbnailContent, ThumbnailFileContent, Captions, ImageContent, ImageEventContent, ThumbnailContent, ThumbnailFileContent,
ThumbnailFileContentInfo, Thumbnails, ThumbnailFileContentInfo,
}, },
message::MessageContent, message::MessageContent,
room::{ room::{
@ -118,7 +118,7 @@ fn image_event_serialization() {
), ),
{ {
image: Box::new(ImageContent::with_size(uint!(1920), uint!(1080))), image: Box::new(ImageContent::with_size(uint!(1920), uint!(1080))),
thumbnail: Thumbnails::new(&[ThumbnailContent::new( thumbnail: vec![ThumbnailContent::new(
ThumbnailFileContent::plain( ThumbnailFileContent::plain(
mxc_uri!("mxc://notareal.hs/thumbnail").to_owned(), mxc_uri!("mxc://notareal.hs/thumbnail").to_owned(),
Some(Box::new(assign!(ThumbnailFileContentInfo::new(), { Some(Box::new(assign!(ThumbnailFileContentInfo::new(), {
@ -127,7 +127,7 @@ fn image_event_serialization() {
}))) })))
), ),
None None
)]), )],
caption: Captions::plain("This is my house"), caption: Captions::plain("This is my house"),
relates_to: Some(Relation::Reply { relates_to: Some(Relation::Reply {
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()), in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
@ -255,7 +255,7 @@ fn encrypted_content_deserialization() {
&& file.encryption_info.is_some() && file.encryption_info.is_some()
&& image.width.is_none() && image.width.is_none()
&& image.height.is_none() && image.height.is_none()
&& thumbnail.thumbnails()[0].file.url == "mxc://notareal.hs/thumbnail" && thumbnail[0].file.url == "mxc://notareal.hs/thumbnail"
&& caption.is_empty() && caption.is_empty()
); );
} }

View File

@ -9,9 +9,7 @@ use ruma_common::{
event_id, event_id,
events::{ events::{
file::{EncryptedContentInit, FileContent, FileContentInfo}, file::{EncryptedContentInit, FileContent, FileContentInfo},
image::{ image::{Captions, ThumbnailContent, ThumbnailFileContent, ThumbnailFileContentInfo},
Captions, ThumbnailContent, ThumbnailFileContent, ThumbnailFileContentInfo, Thumbnails,
},
message::MessageContent, message::MessageContent,
room::{ room::{
message::{InReplyTo, Relation}, message::{InReplyTo, Relation},
@ -127,7 +125,7 @@ fn event_serialization() {
duration: Some(Duration::from_secs(15)), duration: Some(Duration::from_secs(15)),
} }
)), )),
thumbnail: Thumbnails::new(&[ThumbnailContent::new( thumbnail: vec![ThumbnailContent::new(
ThumbnailFileContent::plain( ThumbnailFileContent::plain(
mxc_uri!("mxc://notareal.hs/thumbnail").to_owned(), mxc_uri!("mxc://notareal.hs/thumbnail").to_owned(),
Some(Box::new(assign!(ThumbnailFileContentInfo::new(), { Some(Box::new(assign!(ThumbnailFileContentInfo::new(), {
@ -136,7 +134,7 @@ fn event_serialization() {
}))) })))
), ),
None None
)]), )],
caption: Captions::plain("This is my awesome vintage lava lamp"), caption: Captions::plain("This is my awesome vintage lava lamp"),
relates_to: Some(Relation::Reply { relates_to: Some(Relation::Reply {
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()), in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
@ -267,7 +265,7 @@ fn encrypted_content_deserialization() {
&& video.width.is_none() && video.width.is_none()
&& video.height.is_none() && video.height.is_none()
&& video.duration.is_none() && video.duration.is_none()
&& thumbnail.thumbnails()[0].file.url == "mxc://notareal.hs/thumbnail" && thumbnail[0].file.url == "mxc://notareal.hs/thumbnail"
&& caption.is_empty() && caption.is_empty()
); );
} }