use std::ops::Deref;
use js_int::UInt;
use ruma_common::OwnedMxcUri;
use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use super::{
file::{CaptionContentBlock, EncryptedContent, FileContentBlock},
message::TextContentBlock,
room::message::Relation,
};
#[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "org.matrix.msc1767.image", kind = MessageLike, without_relation)]
pub struct ImageEventContent {
#[serde(rename = "org.matrix.msc1767.text")]
pub text: TextContentBlock,
#[serde(rename = "org.matrix.msc1767.file")]
pub file: FileContentBlock,
#[serde(rename = "org.matrix.msc1767.image_details", skip_serializing_if = "Option::is_none")]
pub image_details: Option<ImageDetailsContentBlock>,
#[serde(
rename = "org.matrix.msc1767.thumbnail",
default,
skip_serializing_if = "ThumbnailContentBlock::is_empty"
)]
pub thumbnail: ThumbnailContentBlock,
#[serde(rename = "org.matrix.msc1767.caption", skip_serializing_if = "Option::is_none")]
pub caption: Option<CaptionContentBlock>,
#[serde(rename = "org.matrix.msc1767.alt_text", skip_serializing_if = "Option::is_none")]
pub alt_text: Option<AltTextContentBlock>,
#[cfg(feature = "unstable-msc3955")]
#[serde(
default,
skip_serializing_if = "ruma_common::serde::is_default",
rename = "org.matrix.msc1767.automated"
)]
pub automated: bool,
#[serde(
flatten,
skip_serializing_if = "Option::is_none",
deserialize_with = "crate::room::message::relation_serde::deserialize_relation"
)]
pub relates_to: Option<Relation<ImageEventContentWithoutRelation>>,
}
impl ImageEventContent {
pub fn new(text: TextContentBlock, file: FileContentBlock) -> Self {
Self {
text,
file,
image_details: None,
thumbnail: Default::default(),
caption: None,
alt_text: None,
#[cfg(feature = "unstable-msc3955")]
automated: false,
relates_to: None,
}
}
pub fn with_plain_text(plain_text: impl Into<String>, file: FileContentBlock) -> Self {
Self {
text: TextContentBlock::plain(plain_text),
file,
image_details: None,
thumbnail: Default::default(),
caption: None,
alt_text: None,
#[cfg(feature = "unstable-msc3955")]
automated: false,
relates_to: None,
}
}
}
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct ImageDetailsContentBlock {
pub height: UInt,
pub width: UInt,
#[serde(
rename = "org.matrix.msc1767.sticker",
default,
skip_serializing_if = "ruma_common::serde::is_default"
)]
pub sticker: bool,
}
impl ImageDetailsContentBlock {
pub fn new(width: UInt, height: UInt) -> Self {
Self { height, width, sticker: Default::default() }
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[allow(clippy::exhaustive_structs)]
pub struct ThumbnailContentBlock(Vec<Thumbnail>);
impl ThumbnailContentBlock {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl From<Vec<Thumbnail>> for ThumbnailContentBlock {
fn from(thumbnails: Vec<Thumbnail>) -> Self {
Self(thumbnails)
}
}
impl FromIterator<Thumbnail> for ThumbnailContentBlock {
fn from_iter<T: IntoIterator<Item = Thumbnail>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
impl Deref for ThumbnailContentBlock {
type Target = [Thumbnail];
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Thumbnail {
#[serde(rename = "org.matrix.msc1767.file")]
pub file: ThumbnailFileContentBlock,
#[serde(rename = "org.matrix.msc1767.image_details")]
pub image_details: ThumbnailImageDetailsContentBlock,
}
impl Thumbnail {
pub fn new(
file: ThumbnailFileContentBlock,
image_details: ThumbnailImageDetailsContentBlock,
) -> Self {
Self { file, image_details }
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct ThumbnailFileContentBlock {
pub url: OwnedMxcUri,
pub mimetype: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<UInt>,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub encryption_info: Option<Box<EncryptedContent>>,
}
impl ThumbnailFileContentBlock {
pub fn plain(url: OwnedMxcUri, mimetype: String) -> Self {
Self { url, mimetype, name: None, size: None, encryption_info: None }
}
pub fn encrypted(
url: OwnedMxcUri,
mimetype: String,
encryption_info: EncryptedContent,
) -> Self {
Self {
url,
mimetype,
name: None,
size: None,
encryption_info: Some(Box::new(encryption_info)),
}
}
pub fn is_encrypted(&self) -> bool {
self.encryption_info.is_some()
}
}
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct ThumbnailImageDetailsContentBlock {
pub height: UInt,
pub width: UInt,
}
impl ThumbnailImageDetailsContentBlock {
pub fn new(width: UInt, height: UInt) -> Self {
Self { height, width }
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct AltTextContentBlock {
#[serde(rename = "org.matrix.msc1767.text")]
pub text: TextContentBlock,
}
impl AltTextContentBlock {
pub fn plain(body: impl Into<String>) -> Self {
Self { text: TextContentBlock::plain(body) }
}
}
impl From<TextContentBlock> for AltTextContentBlock {
fn from(text: TextContentBlock) -> Self {
Self { text }
}
}