events: Support m.room.message
events with custom msgtype
s
This commit is contained in:
parent
704e5f89f5
commit
a76d3e24a4
@ -1,5 +1,7 @@
|
|||||||
//! Types for the *m.room.message* event.
|
//! Types for the *m.room.message* event.
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use js_int::UInt;
|
use js_int::UInt;
|
||||||
use ruma_events_macros::MessageEventContent;
|
use ruma_events_macros::MessageEventContent;
|
||||||
#[cfg(feature = "unstable-pre-spec")]
|
#[cfg(feature = "unstable-pre-spec")]
|
||||||
@ -64,6 +66,10 @@ pub enum MessageEventContent {
|
|||||||
/// A request to initiate a key verification.
|
/// A request to initiate a key verification.
|
||||||
#[cfg(feature = "unstable-pre-spec")]
|
#[cfg(feature = "unstable-pre-spec")]
|
||||||
VerificationRequest(KeyVerificationRequestEventContent),
|
VerificationRequest(KeyVerificationRequestEventContent),
|
||||||
|
|
||||||
|
/// A custom message.
|
||||||
|
#[doc(hidden)]
|
||||||
|
_Custom(CustomEventContent),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enum modeling the different ways relationships can be expressed in a
|
/// 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.
|
/// events that have a m.reference relationship with this event.
|
||||||
pub to: UserId,
|
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>,
|
||||||
|
}
|
||||||
|
@ -32,7 +32,7 @@ impl<'de> de::Deserialize<'de> for MessageEventContent {
|
|||||||
"m.video" => Self::Video(from_raw_json_value(&json)?),
|
"m.video" => Self::Video(from_raw_json_value(&json)?),
|
||||||
#[cfg(feature = "unstable-pre-spec")]
|
#[cfg(feature = "unstable-pre-spec")]
|
||||||
"m.key.verification.request" => Self::VerificationRequest(from_raw_json_value(&json)?),
|
"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)?),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use std::time::{Duration, UNIX_EPOCH};
|
use std::time::{Duration, UNIX_EPOCH};
|
||||||
|
|
||||||
use assign::assign;
|
use assign::assign;
|
||||||
|
use maplit::btreemap;
|
||||||
use matches::assert_matches;
|
use matches::assert_matches;
|
||||||
#[cfg(feature = "unstable-pre-spec")]
|
#[cfg(feature = "unstable-pre-spec")]
|
||||||
use ruma_events::{
|
use ruma_events::{
|
||||||
@ -9,7 +10,8 @@ use ruma_events::{
|
|||||||
use ruma_events::{
|
use ruma_events::{
|
||||||
room::{
|
room::{
|
||||||
message::{
|
message::{
|
||||||
AudioMessageEventContent, MessageEventContent, Relation, TextMessageEventContent,
|
AudioMessageEventContent, CustomEventContent, MessageEventContent, Relation,
|
||||||
|
TextMessageEventContent,
|
||||||
},
|
},
|
||||||
relationships::InReplyTo,
|
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]
|
#[test]
|
||||||
fn formatted_body_serialization() {
|
fn formatted_body_serialization() {
|
||||||
let message_event_content = MessageEventContent::Text(TextMessageEventContent::html(
|
let message_event_content = MessageEventContent::Text(TextMessageEventContent::html(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user