events: Update types according to changes in MSC3245

This commit is contained in:
Kévin Commaille 2023-01-05 12:46:30 +01:00 committed by Kévin Commaille
parent 41be6ac18b
commit 08aa2ca04c
3 changed files with 75 additions and 39 deletions

View File

@ -89,7 +89,8 @@ event_enum! {
#[ruma_enum(alias = "m.video")] #[ruma_enum(alias = "m.video")]
"org.matrix.msc1767.video" => super::video, "org.matrix.msc1767.video" => super::video,
#[cfg(feature = "unstable-msc3245")] #[cfg(feature = "unstable-msc3245")]
"m.voice" => super::voice, #[ruma_enum(alias = "m.voice")]
"org.matrix.msc3245.voice.v2" => super::voice,
} }
/// Any state event. /// Any state event.
@ -330,7 +331,7 @@ impl AnyMessageLikeEventContent {
Self::Encrypted(ev) => ev.relates_to.clone(), Self::Encrypted(ev) => ev.relates_to.clone(),
#[cfg(feature = "unstable-msc3245")] #[cfg(feature = "unstable-msc3245")]
Self::Voice(ev) => ev.relates_to.clone().map(Into::into), Self::Voice(ev) => ev.relates_to.clone().map(Into::into),
#[cfg(feature = "unstable-msc3246")] #[cfg(feature = "unstable-msc3927")]
Self::Audio(ev) => ev.relates_to.clone().map(Into::into), Self::Audio(ev) => ev.relates_to.clone().map(Into::into),
#[cfg(feature = "unstable-msc3488")] #[cfg(feature = "unstable-msc3488")]
Self::Location(ev) => ev.relates_to.clone().map(Into::into), Self::Location(ev) => ev.relates_to.clone().map(Into::into),

View File

@ -2,12 +2,13 @@
//! //!
//! [MSC3245]: https://github.com/matrix-org/matrix-spec-proposals/pull/3245 //! [MSC3245]: https://github.com/matrix-org/matrix-spec-proposals/pull/3245
use std::time::Duration;
use ruma_macros::EventContent; use ruma_macros::EventContent;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::{ use super::{
audio::AudioDetailsContentBlock, file::FileContentBlock, message::TextContentBlock, audio::Amplitude, file::FileContentBlock, message::TextContentBlock, room::message::Relation,
room::message::Relation,
}; };
/// The payload for an extensible voice message. /// The payload for an extensible voice message.
@ -20,7 +21,7 @@ use super::{
/// [`message`]: super::message /// [`message`]: super::message
#[derive(Clone, Debug, Serialize, Deserialize, EventContent)] #[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.voice", kind = MessageLike, without_relation)] #[ruma_event(type = "org.matrix.msc3245.voice.v2", kind = MessageLike, without_relation)]
pub struct VoiceEventContent { pub struct VoiceEventContent {
/// The text representation of the message. /// The text representation of the message.
#[serde(rename = "org.matrix.msc1767.text")] #[serde(rename = "org.matrix.msc1767.text")]
@ -31,12 +32,17 @@ pub struct VoiceEventContent {
pub file: FileContentBlock, pub file: FileContentBlock,
/// The audio content of the message. /// The audio content of the message.
#[serde(rename = "org.matrix.msc1767.audio_details", skip_serializing_if = "Option::is_none")] #[serde(rename = "org.matrix.msc1767.audio_details")]
pub audio_details: Option<AudioDetailsContentBlock>, pub audio_details: VoiceAudioDetailsContentBlock,
/// The voice content of the message. /// Whether this message is automated.
#[serde(rename = "m.voice")] #[cfg(feature = "unstable-msc3955")]
pub voice: VoiceContent, #[serde(
default,
skip_serializing_if = "crate::serde::is_default",
rename = "org.matrix.msc1767.automated"
)]
pub automated: bool,
/// Information about related messages. /// Information about related messages.
#[serde( #[serde(
@ -48,35 +54,58 @@ pub struct VoiceEventContent {
} }
impl VoiceEventContent { impl VoiceEventContent {
/// Creates a new `VoiceEventContent` with the given fallback representation and file. /// Creates a new `VoiceEventContent` with the given fallback representation, file and audio
pub fn new(text: TextContentBlock, file: FileContentBlock) -> Self { /// details.
Self { text, file, audio_details: None, voice: Default::default(), relates_to: None } pub fn new(
text: TextContentBlock,
file: FileContentBlock,
audio_details: VoiceAudioDetailsContentBlock,
) -> Self {
Self {
text,
file,
audio_details,
#[cfg(feature = "unstable-msc3955")]
automated: false,
relates_to: None,
}
} }
/// Creates a new `VoiceEventContent` with the given plain text fallback representation and /// Creates a new `VoiceEventContent` with the given plain text fallback representation, file
/// file. /// and audio details.
pub fn plain(text: impl Into<String>, file: FileContentBlock) -> Self { pub fn with_plain_text(
plain_text: impl Into<String>,
file: FileContentBlock,
audio_details: VoiceAudioDetailsContentBlock,
) -> Self {
Self { Self {
text: TextContentBlock::plain(text), text: TextContentBlock::plain(plain_text),
file, file,
audio_details: None, audio_details,
voice: Default::default(), #[cfg(feature = "unstable-msc3955")]
automated: false,
relates_to: None, relates_to: None,
} }
} }
} }
/// Voice content. /// A block for details of voice audio content.
/// #[derive(Clone, Debug, Serialize, Deserialize)]
/// This is currently empty and used as a flag to mark an audio event that should be displayed as a
/// voice message.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct VoiceContent {} pub struct VoiceAudioDetailsContentBlock {
/// The duration of the audio in seconds.
#[serde(with = "ruma_common::serde::duration::secs")]
pub duration: Duration,
impl VoiceContent { /// The waveform representation of the content.
/// Creates a new empty `VoiceContent`. #[serde(rename = "org.matrix.msc3246.waveform")]
pub fn new() -> Self { pub waveform: Vec<Amplitude>,
Self::default() }
impl VoiceAudioDetailsContentBlock {
/// Creates a new `AudioDetailsContentBlock` with the given duration and waveform
/// representation.
pub fn new(duration: Duration, waveform: Vec<Amplitude>) -> Self {
Self { duration, waveform }
} }
} }

View File

@ -7,8 +7,12 @@ use js_int::uint;
use ruma_common::{ use ruma_common::{
event_id, event_id,
events::{ events::{
audio::AudioDetailsContentBlock, file::FileContentBlock, relation::InReplyTo, audio::Amplitude,
room::message::Relation, voice::VoiceEventContent, AnyMessageLikeEvent, MessageLikeEvent, file::FileContentBlock,
relation::InReplyTo,
room::message::Relation,
voice::{VoiceAudioDetailsContentBlock, VoiceEventContent},
AnyMessageLikeEvent, MessageLikeEvent,
}, },
mxc_uri, mxc_uri,
serde::CanBeEmpty, serde::CanBeEmpty,
@ -18,17 +22,20 @@ use serde_json::{from_value as from_json_value, json, to_value as to_json_value}
#[test] #[test]
fn event_serialization() { fn event_serialization() {
let mut content = VoiceEventContent::plain( let mut content = VoiceEventContent::with_plain_text(
"Voice message", "Voice message",
FileContentBlock::plain( FileContentBlock::plain(
mxc_uri!("mxc://notareal.hs/abcdef").to_owned(), mxc_uri!("mxc://notareal.hs/abcdef").to_owned(),
"voice_message.ogg".to_owned(), "voice_message.ogg".to_owned(),
), ),
VoiceAudioDetailsContentBlock::new(
Duration::from_secs(23),
vec![Amplitude::from(255), Amplitude::from(0)],
),
); );
content.file.mimetype = Some("audio/opus".to_owned()); content.file.mimetype = Some("audio/opus".to_owned());
content.file.size = Some(uint!(897_774)); content.file.size = Some(uint!(897_774));
content.audio_details = Some(AudioDetailsContentBlock::new(Duration::from_secs(23)));
content.relates_to = Some(Relation::Reply { content.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()),
}); });
@ -47,8 +54,8 @@ fn event_serialization() {
}, },
"org.matrix.msc1767.audio_details": { "org.matrix.msc1767.audio_details": {
"duration": 23, "duration": 23,
"org.matrix.msc3246.waveform": [255, 0],
}, },
"m.voice": {},
"m.relates_to": { "m.relates_to": {
"m.in_reply_to": { "m.in_reply_to": {
"event_id": "$replyevent:example.com" "event_id": "$replyevent:example.com"
@ -73,14 +80,14 @@ fn message_event_deserialization() {
}, },
"org.matrix.msc1767.audio_details": { "org.matrix.msc1767.audio_details": {
"duration": 53, "duration": 53,
"org.matrix.msc3246.waveform": [255, 0],
}, },
"m.voice": {},
}, },
"event_id": "$event:notareal.hs", "event_id": "$event:notareal.hs",
"origin_server_ts": 134_829_848, "origin_server_ts": 134_829_848,
"room_id": "!roomid:notareal.hs", "room_id": "!roomid:notareal.hs",
"sender": "@user:notareal.hs", "sender": "@user:notareal.hs",
"type": "m.voice", "type": "org.matrix.msc3245.voice.v2",
}); });
let ev = assert_matches!( let ev = assert_matches!(
@ -100,7 +107,6 @@ fn message_event_deserialization() {
assert_eq!(content.file.name, "voice_message.ogg"); assert_eq!(content.file.name, "voice_message.ogg");
assert_eq!(content.file.mimetype.as_deref(), Some("audio/opus")); assert_eq!(content.file.mimetype.as_deref(), Some("audio/opus"));
assert_eq!(content.file.size, Some(uint!(123_774))); assert_eq!(content.file.size, Some(uint!(123_774)));
let audio_details = content.audio_details.unwrap(); assert_eq!(content.audio_details.duration, Duration::from_secs(53));
assert_eq!(audio_details.duration, Duration::from_secs(53)); assert_eq!(content.audio_details.waveform.len(), 2);
assert!(audio_details.waveform.is_empty());
} }