use std::time::Duration;
use js_int::UInt;
use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
#[cfg(feature = "unstable-msc3246")]
mod amplitude_serde;
use super::{
file::{CaptionContentBlock, 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.audio", kind = MessageLike, without_relation)]
pub struct AudioEventContent {
#[serde(rename = "org.matrix.msc1767.text")]
pub text: TextContentBlock,
#[serde(rename = "org.matrix.msc1767.file")]
pub file: FileContentBlock,
#[serde(rename = "org.matrix.msc1767.audio_details", skip_serializing_if = "Option::is_none")]
pub audio_details: Option<AudioDetailsContentBlock>,
#[serde(rename = "org.matrix.msc1767.caption", skip_serializing_if = "Option::is_none")]
pub caption: Option<CaptionContentBlock>,
#[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<AudioEventContentWithoutRelation>>,
}
impl AudioEventContent {
pub fn new(text: TextContentBlock, file: FileContentBlock) -> Self {
Self {
text,
file,
audio_details: None,
caption: 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,
audio_details: None,
caption: None,
#[cfg(feature = "unstable-msc3955")]
automated: false,
relates_to: None,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct AudioDetailsContentBlock {
#[serde(with = "ruma_common::serde::duration::secs")]
pub duration: Duration,
#[cfg(feature = "unstable-msc3246")]
#[serde(
rename = "org.matrix.msc3246.waveform",
default,
skip_serializing_if = "Vec::is_empty"
)]
pub waveform: Vec<Amplitude>,
}
impl AudioDetailsContentBlock {
pub fn new(duration: Duration) -> Self {
Self {
duration,
#[cfg(feature = "unstable-msc3246")]
waveform: Default::default(),
}
}
}
#[cfg(feature = "unstable-msc3246")]
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub struct Amplitude(UInt);
#[cfg(feature = "unstable-msc3246")]
impl Amplitude {
pub const MIN: u16 = 0;
pub const MAX: u16 = 256;
pub fn new(value: u16) -> Self {
Self(value.min(Self::MAX).into())
}
pub fn get(&self) -> UInt {
self.0
}
}
#[cfg(feature = "unstable-msc3246")]
impl From<u16> for Amplitude {
fn from(value: u16) -> Self {
Self::new(value)
}
}