use std::time::Duration;
use ruma::{
OwnedTransactionId, UInt, assign,
events::{
Mentions,
room::{
ImageInfo, ThumbnailInfo,
message::{AudioInfo, FileInfo, TextMessageEventContent, VideoInfo},
},
},
};
use crate::room::reply::Reply;
#[derive(Debug, Clone, Default)]
pub struct BaseImageInfo {
pub height: Option<UInt>,
pub width: Option<UInt>,
pub size: Option<UInt>,
pub blurhash: Option<String>,
pub is_animated: Option<bool>,
}
#[derive(Debug, Clone, Default)]
pub struct BaseVideoInfo {
pub duration: Option<Duration>,
pub height: Option<UInt>,
pub width: Option<UInt>,
pub size: Option<UInt>,
pub blurhash: Option<String>,
}
#[derive(Debug, Clone, Default)]
pub struct BaseAudioInfo {
pub duration: Option<Duration>,
pub size: Option<UInt>,
pub waveform: Option<Vec<f32>>,
}
#[derive(Debug, Clone, Default)]
pub struct BaseFileInfo {
pub size: Option<UInt>,
}
#[derive(Debug)]
pub enum AttachmentInfo {
Image(BaseImageInfo),
Video(BaseVideoInfo),
Audio(BaseAudioInfo),
File(BaseFileInfo),
Voice(BaseAudioInfo),
}
impl From<AttachmentInfo> for ImageInfo {
fn from(info: AttachmentInfo) -> Self {
match info {
AttachmentInfo::Image(info) => assign!(ImageInfo::new(), {
height: info.height,
width: info.width,
size: info.size,
blurhash: info.blurhash,
is_animated: info.is_animated,
}),
_ => ImageInfo::new(),
}
}
}
impl From<AttachmentInfo> for VideoInfo {
fn from(info: AttachmentInfo) -> Self {
match info {
AttachmentInfo::Video(info) => assign!(VideoInfo::new(), {
duration: info.duration,
height: info.height,
width: info.width,
size: info.size,
blurhash: info.blurhash,
}),
_ => VideoInfo::new(),
}
}
}
impl From<AttachmentInfo> for AudioInfo {
fn from(info: AttachmentInfo) -> Self {
match info {
AttachmentInfo::Audio(info) | AttachmentInfo::Voice(info) => {
assign!(AudioInfo::new(), {
duration: info.duration,
size: info.size,
})
}
_ => AudioInfo::new(),
}
}
}
impl From<AttachmentInfo> for FileInfo {
fn from(info: AttachmentInfo) -> Self {
match info {
AttachmentInfo::File(info) => assign!(FileInfo::new(), {
size: info.size,
}),
_ => FileInfo::new(),
}
}
}
#[derive(Debug)]
pub struct Thumbnail {
pub data: Vec<u8>,
pub content_type: mime::Mime,
pub height: UInt,
pub width: UInt,
pub size: UInt,
}
impl Thumbnail {
pub fn into_parts(self) -> (Vec<u8>, mime::Mime, Box<ThumbnailInfo>) {
let thumbnail_info = assign!(ThumbnailInfo::new(), {
height: Some(self.height),
width: Some(self.width),
size: Some(self.size),
mimetype: Some(self.content_type.to_string())
});
(self.data, self.content_type, Box::new(thumbnail_info))
}
}
#[derive(Debug, Default)]
pub struct AttachmentConfig {
pub txn_id: Option<OwnedTransactionId>,
pub info: Option<AttachmentInfo>,
pub thumbnail: Option<Thumbnail>,
pub caption: Option<TextMessageEventContent>,
pub mentions: Option<Mentions>,
pub reply: Option<Reply>,
}
impl AttachmentConfig {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn thumbnail(mut self, thumbnail: Option<Thumbnail>) -> Self {
self.thumbnail = thumbnail;
self
}
#[must_use]
pub fn txn_id(mut self, txn_id: OwnedTransactionId) -> Self {
self.txn_id = Some(txn_id);
self
}
#[must_use]
pub fn info(mut self, info: AttachmentInfo) -> Self {
self.info = Some(info);
self
}
pub fn caption(mut self, caption: Option<TextMessageEventContent>) -> Self {
self.caption = caption;
self
}
pub fn mentions(mut self, mentions: Option<Mentions>) -> Self {
self.mentions = mentions;
self
}
pub fn reply(mut self, reply: Option<Reply>) -> Self {
self.reply = reply;
self
}
}
#[cfg(feature = "unstable-msc4274")]
#[derive(Debug, Default)]
pub struct GalleryConfig {
pub(crate) txn_id: Option<OwnedTransactionId>,
pub(crate) items: Vec<GalleryItemInfo>,
pub(crate) caption: Option<TextMessageEventContent>,
pub(crate) mentions: Option<Mentions>,
pub(crate) reply: Option<Reply>,
}
#[cfg(feature = "unstable-msc4274")]
impl GalleryConfig {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn txn_id(mut self, txn_id: OwnedTransactionId) -> Self {
self.txn_id = Some(txn_id);
self
}
#[must_use]
pub fn add_item(mut self, item: GalleryItemInfo) -> Self {
self.items.push(item);
self
}
pub fn caption(mut self, caption: Option<TextMessageEventContent>) -> Self {
self.caption = caption;
self
}
pub fn mentions(mut self, mentions: Option<Mentions>) -> Self {
self.mentions = mentions;
self
}
pub fn reply(mut self, reply: Option<Reply>) -> Self {
self.reply = reply;
self
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
}
#[cfg(feature = "unstable-msc4274")]
#[derive(Debug)]
pub struct GalleryItemInfo {
pub filename: String,
pub content_type: mime::Mime,
pub data: Vec<u8>,
pub attachment_info: AttachmentInfo,
pub caption: Option<TextMessageEventContent>,
pub thumbnail: Option<Thumbnail>,
}