events: Streamline constructors of media event content structs

This commit is contained in:
Jonas Platte 2023-06-13 16:51:57 +02:00
parent 59fd881521
commit c1fa8d9406
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
5 changed files with 33 additions and 18 deletions

View File

@ -26,16 +26,21 @@ pub struct AudioMessageEventContent {
} }
impl AudioMessageEventContent { impl AudioMessageEventContent {
/// Creates a new `AudioMessageEventContent` with the given body and source.
pub fn new(body: String, source: MediaSource) -> Self {
Self { body, source, info: None }
}
/// Creates a new non-encrypted `AudioMessageEventContent` with the given body, url and /// Creates a new non-encrypted `AudioMessageEventContent` with the given body, url and
/// optional extra info. /// optional extra info.
pub fn plain(body: String, url: OwnedMxcUri, info: Option<Box<AudioInfo>>) -> Self { pub fn plain(body: String, url: OwnedMxcUri) -> Self {
Self { body, source: MediaSource::Plain(url), info } Self::new(body, MediaSource::Plain(url))
} }
/// Creates a new encrypted `AudioMessageEventContent` with the given body and encrypted /// Creates a new encrypted `AudioMessageEventContent` with the given body and encrypted
/// file. /// file.
pub fn encrypted(body: String, file: EncryptedFile) -> Self { pub fn encrypted(body: String, file: EncryptedFile) -> Self {
Self { body, source: MediaSource::Encrypted(Box::new(file)), info: None } Self::new(body, MediaSource::Encrypted(Box::new(file)))
} }
} }

View File

@ -30,16 +30,21 @@ pub struct FileMessageEventContent {
} }
impl FileMessageEventContent { impl FileMessageEventContent {
/// Creates a new `FileMessageEventContent` with the given body and source.
pub fn new(body: String, source: MediaSource) -> Self {
Self { body, filename: None, source, info: None }
}
/// Creates a new non-encrypted `FileMessageEventContent` with the given body, url and /// Creates a new non-encrypted `FileMessageEventContent` with the given body, url and
/// optional extra info. /// optional extra info.
pub fn plain(body: String, url: OwnedMxcUri, info: Option<Box<FileInfo>>) -> Self { pub fn plain(body: String, url: OwnedMxcUri) -> Self {
Self { body, filename: None, source: MediaSource::Plain(url), info } Self::new(body, MediaSource::Plain(url))
} }
/// Creates a new encrypted `FileMessageEventContent` with the given body and encrypted /// Creates a new encrypted `FileMessageEventContent` with the given body and encrypted
/// file. /// file.
pub fn encrypted(body: String, file: EncryptedFile) -> Self { pub fn encrypted(body: String, file: EncryptedFile) -> Self {
Self { body, filename: None, source: MediaSource::Encrypted(Box::new(file)), info: None } Self::new(body, MediaSource::Encrypted(Box::new(file)))
} }
} }

View File

@ -26,15 +26,19 @@ pub struct ImageMessageEventContent {
} }
impl ImageMessageEventContent { impl ImageMessageEventContent {
/// Creates a new non-encrypted `ImageMessageEventContent` with the given body, url and /// Creates a new `ImageMessageEventContent` with the given body and source.
/// optional extra info. pub fn new(body: String, source: MediaSource) -> Self {
pub fn plain(body: String, url: OwnedMxcUri, info: Option<Box<ImageInfo>>) -> Self { Self { body, source, info: None }
Self { body, source: MediaSource::Plain(url), info } }
/// Creates a new non-encrypted `ImageMessageEventContent` with the given body and url.
pub fn plain(body: String, url: OwnedMxcUri) -> Self {
Self::new(body, MediaSource::Plain(url))
} }
/// Creates a new encrypted `ImageMessageEventContent` with the given body and encrypted /// Creates a new encrypted `ImageMessageEventContent` with the given body and encrypted
/// file. /// file.
pub fn encrypted(body: String, file: EncryptedFile) -> Self { pub fn encrypted(body: String, file: EncryptedFile) -> Self {
Self { body, source: MediaSource::Encrypted(Box::new(file)), info: None } Self::new(body, MediaSource::Encrypted(Box::new(file)))
} }
} }

View File

@ -27,16 +27,21 @@ pub struct VideoMessageEventContent {
} }
impl VideoMessageEventContent { impl VideoMessageEventContent {
/// Creates a new `VideoMessageEventContent` with the given body and source.
pub fn new(body: String, source: MediaSource) -> Self {
Self { body, source, info: None }
}
/// Creates a new non-encrypted `VideoMessageEventContent` with the given body, url and /// Creates a new non-encrypted `VideoMessageEventContent` with the given body, url and
/// optional extra info. /// optional extra info.
pub fn plain(body: String, url: OwnedMxcUri, info: Option<Box<VideoInfo>>) -> Self { pub fn plain(body: String, url: OwnedMxcUri) -> Self {
Self { body, source: MediaSource::Plain(url), info } Self::new(body, MediaSource::Plain(url))
} }
/// Creates a new encrypted `VideoMessageEventContent` with the given body and encrypted /// Creates a new encrypted `VideoMessageEventContent` with the given body and encrypted
/// file. /// file.
pub fn encrypted(body: String, file: EncryptedFile) -> Self { pub fn encrypted(body: String, file: EncryptedFile) -> Self {
Self { body, source: MediaSource::Encrypted(Box::new(file)), info: None } Self::new(body, MediaSource::Encrypted(Box::new(file)))
} }
} }

View File

@ -475,7 +475,6 @@ fn audio_msgtype_serialization() {
RoomMessageEventContent::new(MessageType::Audio(AudioMessageEventContent::plain( RoomMessageEventContent::new(MessageType::Audio(AudioMessageEventContent::plain(
"Upload: my_song.mp3".to_owned(), "Upload: my_song.mp3".to_owned(),
mxc_uri!("mxc://notareal.hs/file").to_owned(), mxc_uri!("mxc://notareal.hs/file").to_owned(),
None,
))); )));
assert_eq!( assert_eq!(
@ -509,7 +508,6 @@ fn file_msgtype_plain_content_serialization() {
RoomMessageEventContent::new(MessageType::File(FileMessageEventContent::plain( RoomMessageEventContent::new(MessageType::File(FileMessageEventContent::plain(
"Upload: my_file.txt".to_owned(), "Upload: my_file.txt".to_owned(),
mxc_uri!("mxc://notareal.hs/file").to_owned(), mxc_uri!("mxc://notareal.hs/file").to_owned(),
None,
))); )));
assert_eq!( assert_eq!(
@ -622,7 +620,6 @@ fn image_msgtype_serialization() {
RoomMessageEventContent::new(MessageType::Image(ImageMessageEventContent::plain( RoomMessageEventContent::new(MessageType::Image(ImageMessageEventContent::plain(
"Upload: my_image.jpg".to_owned(), "Upload: my_image.jpg".to_owned(),
mxc_uri!("mxc://notareal.hs/file").to_owned(), mxc_uri!("mxc://notareal.hs/file").to_owned(),
None,
))); )));
assert_eq!( assert_eq!(
@ -777,7 +774,6 @@ fn video_msgtype_serialization() {
RoomMessageEventContent::new(MessageType::Video(VideoMessageEventContent::plain( RoomMessageEventContent::new(MessageType::Video(VideoMessageEventContent::plain(
"Upload: my_video.mp4".to_owned(), "Upload: my_video.mp4".to_owned(),
mxc_uri!("mxc://notareal.hs/file").to_owned(), mxc_uri!("mxc://notareal.hs/file").to_owned(),
None,
))); )));
assert_eq!( assert_eq!(