events: Support m.room.message events with custom msgtypes

This commit is contained in:
Akshay 2021-02-12 14:00:27 +01:00 committed by Jonas Platte
parent 704e5f89f5
commit a76d3e24a4
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
3 changed files with 69 additions and 2 deletions

View File

@ -1,5 +1,7 @@
//! Types for the *m.room.message* event.
use std::collections::BTreeMap;
use js_int::UInt;
use ruma_events_macros::MessageEventContent;
#[cfg(feature = "unstable-pre-spec")]
@ -64,6 +66,10 @@ pub enum MessageEventContent {
/// A request to initiate a key verification.
#[cfg(feature = "unstable-pre-spec")]
VerificationRequest(KeyVerificationRequestEventContent),
/// A custom message.
#[doc(hidden)]
_Custom(CustomEventContent),
}
/// Enum modeling the different ways relationships can be expressed in a
@ -586,3 +592,15 @@ pub struct KeyVerificationRequestEventContent {
/// events that have a m.reference relationship with this event.
pub to: UserId,
}
/// The payload for a custom message event.
#[doc(hidden)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CustomEventContent {
/// A custom msgtype
pub msgtype: String,
/// Remaining event content
#[serde(flatten)]
pub data: BTreeMap<String, JsonValue>,
}

View File

@ -32,7 +32,7 @@ impl<'de> de::Deserialize<'de> for MessageEventContent {
"m.video" => Self::Video(from_raw_json_value(&json)?),
#[cfg(feature = "unstable-pre-spec")]
"m.key.verification.request" => Self::VerificationRequest(from_raw_json_value(&json)?),
_ => return Err(de::Error::custom("unknown msgtype")),
_ => Self::_Custom(from_raw_json_value(&json)?),
})
}
}

View File

@ -1,6 +1,7 @@
use std::time::{Duration, UNIX_EPOCH};
use assign::assign;
use maplit::btreemap;
use matches::assert_matches;
#[cfg(feature = "unstable-pre-spec")]
use ruma_events::{
@ -9,7 +10,8 @@ use ruma_events::{
use ruma_events::{
room::{
message::{
AudioMessageEventContent, MessageEventContent, Relation, TextMessageEventContent,
AudioMessageEventContent, CustomEventContent, MessageEventContent, Relation,
TextMessageEventContent,
},
relationships::InReplyTo,
},
@ -73,6 +75,53 @@ fn content_serialization() {
);
}
#[test]
fn custom_content_serialization() {
let json_data = btreemap! {
"custom_field".into() => json!("baba"),
"another_one".into() => json!("abab"),
};
let custom_event_content = MessageEventContent::_Custom(CustomEventContent {
msgtype: "my_custom_msgtype".into(),
data: json_data,
});
assert_eq!(
to_json_value(&custom_event_content).unwrap(),
json!({
"msgtype": "my_custom_msgtype",
"custom_field": "baba",
"another_one": "abab",
})
);
}
#[test]
fn custom_content_deserialization() {
let json_data = json!({
"msgtype": "my_custom_msgtype",
"custom_field": "baba",
"another_one": "abab",
});
let expected_json_data = btreemap! {
"custom_field".into() => json!("baba"),
"another_one".into() => json!("abab"),
};
assert_matches!(
from_json_value::<Raw<MessageEventContent>>(json_data)
.unwrap()
.deserialize()
.unwrap(),
MessageEventContent::_Custom(CustomEventContent {
msgtype,
data
}) if msgtype == "my_custom_msgtype"
&& data == expected_json_data
);
}
#[test]
fn formatted_body_serialization() {
let message_event_content = MessageEventContent::Text(TextMessageEventContent::html(