events: Make Custom* struct rest fields consistent
This commit is contained in:
parent
5a7dac446e
commit
cd2c540e92
@ -1,5 +1,7 @@
|
||||
//! Types for custom events outside of the Matrix specification.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use ruma_identifiers::RoomVersionId;
|
||||
use serde::Serialize;
|
||||
use serde_json::{value::RawValue as RawJsonValue, Value as JsonValue};
|
||||
@ -19,7 +21,7 @@ pub struct CustomEventContent {
|
||||
|
||||
/// The actual `content` JSON object.
|
||||
#[serde(flatten)]
|
||||
pub json: JsonValue,
|
||||
pub data: BTreeMap<String, JsonValue>,
|
||||
}
|
||||
|
||||
impl CustomEventContent {
|
||||
@ -35,8 +37,8 @@ impl EventContent for CustomEventContent {
|
||||
}
|
||||
|
||||
fn from_parts(event_type: &str, content: Box<RawJsonValue>) -> Result<Self, serde_json::Error> {
|
||||
let json = serde_json::from_str(content.get())?;
|
||||
Ok(Self { event_type: event_type.to_string(), json })
|
||||
let data = serde_json::from_str(content.get())?;
|
||||
Ok(Self { event_type: event_type.to_string(), data })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,7 +70,7 @@ pub struct CustomContent {
|
||||
|
||||
/// The additional fields that the method contains.
|
||||
#[serde(flatten)]
|
||||
pub fields: BTreeMap<String, JsonValue>,
|
||||
pub data: BTreeMap<String, JsonValue>,
|
||||
}
|
||||
|
||||
/// The payload of an *m.key.verification.accept* event using the *m.sas.v1* method.
|
||||
@ -214,7 +214,7 @@ mod tests {
|
||||
transaction_id: "456".into(),
|
||||
method: AcceptMethod::Custom(CustomContent {
|
||||
method: "m.sas.custom".to_owned(),
|
||||
fields: vec![("test".to_string(), JsonValue::from("field"))]
|
||||
data: vec![("test".to_string(), JsonValue::from("field"))]
|
||||
.into_iter()
|
||||
.collect::<BTreeMap<String, JsonValue>>(),
|
||||
}),
|
||||
@ -359,13 +359,13 @@ mod tests {
|
||||
transaction_id,
|
||||
method: AcceptMethod::Custom(CustomContent {
|
||||
method,
|
||||
fields,
|
||||
data,
|
||||
})
|
||||
}
|
||||
} if transaction_id == "456"
|
||||
&& sender == user_id!("@example:localhost")
|
||||
&& method == "m.sas.custom"
|
||||
&& fields.get("test").unwrap() == &JsonValue::from("field")
|
||||
&& data.get("test").unwrap() == &JsonValue::from("field")
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -79,7 +79,7 @@ pub struct CustomContent {
|
||||
|
||||
/// The additional fields that the method contains.
|
||||
#[serde(flatten)]
|
||||
pub fields: BTreeMap<String, JsonValue>,
|
||||
pub data: BTreeMap<String, JsonValue>,
|
||||
}
|
||||
|
||||
/// The payload of an *m.key.verification.start* event using the *m.sas.v1* method.
|
||||
@ -329,7 +329,7 @@ mod tests {
|
||||
transaction_id: "456".into(),
|
||||
method: StartMethod::Custom(CustomContent {
|
||||
method: "m.sas.custom".to_owned(),
|
||||
fields: vec![("test".to_string(), JsonValue::from("field"))]
|
||||
data: vec![("test".to_string(), JsonValue::from("field"))]
|
||||
.into_iter()
|
||||
.collect::<BTreeMap<String, JsonValue>>(),
|
||||
}),
|
||||
@ -478,14 +478,14 @@ mod tests {
|
||||
transaction_id,
|
||||
method: StartMethod::Custom(CustomContent {
|
||||
method,
|
||||
fields,
|
||||
data,
|
||||
})
|
||||
}
|
||||
} if from_device == "123"
|
||||
&& sender == user_id!("@example:localhost")
|
||||
&& transaction_id == "456"
|
||||
&& method == "m.sas.custom"
|
||||
&& fields.get("test").unwrap() == &JsonValue::from("field")
|
||||
&& data.get("test").unwrap() == &JsonValue::from("field")
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
use std::time::{Duration, UNIX_EPOCH};
|
||||
|
||||
use maplit::btreemap;
|
||||
use matches::assert_matches;
|
||||
use ruma_events::{
|
||||
custom::CustomEventContent, AnyMessageEvent, AnyStateEvent, AnyStateEventContent,
|
||||
@ -37,18 +38,18 @@ fn custom_state_event() -> JsonValue {
|
||||
fn serialize_custom_message_event() {
|
||||
let aliases_event = AnyMessageEvent::Custom(MessageEvent {
|
||||
content: CustomEventContent {
|
||||
json: json!({
|
||||
"body": " * edited message",
|
||||
"m.new_content": {
|
||||
data: btreemap! {
|
||||
"body".into() => " * edited message".into(),
|
||||
"m.new_content".into() => json!({
|
||||
"body": "edited message",
|
||||
"msgtype": "m.text"
|
||||
},
|
||||
"m.relates_to": {
|
||||
}),
|
||||
"m.relates_to".into() => json!({
|
||||
"event_id": "some event id",
|
||||
"rel_type": "m.replace"
|
||||
},
|
||||
"msgtype": "m.text"
|
||||
}),
|
||||
}),
|
||||
"msgtype".into() => "m.text".into()
|
||||
},
|
||||
event_type: "m.room.message".into(),
|
||||
},
|
||||
event_id: event_id!("$h29iv0s8:example.com"),
|
||||
@ -86,9 +87,9 @@ fn serialize_custom_message_event() {
|
||||
fn serialize_custom_state_event() {
|
||||
let aliases_event = AnyStateEvent::Custom(StateEvent {
|
||||
content: CustomEventContent {
|
||||
json: json!({
|
||||
"custom": 10
|
||||
}),
|
||||
data: btreemap! {
|
||||
"custom".into() => 10.into()
|
||||
},
|
||||
event_type: "m.made.up".into(),
|
||||
},
|
||||
event_id: event_id!("$h29iv0s8:example.com"),
|
||||
@ -120,13 +121,13 @@ fn serialize_custom_state_event() {
|
||||
fn deserialize_custom_state_event() {
|
||||
let json_data = custom_state_event();
|
||||
|
||||
let expected_content = json!({
|
||||
"m.relates_to": {
|
||||
let expected_content = btreemap! {
|
||||
"m.relates_to".into() => json!({
|
||||
"event_id": "$MDitXXXXXX",
|
||||
"key": "👍",
|
||||
"rel_type": "m.annotation"
|
||||
}
|
||||
});
|
||||
}),
|
||||
};
|
||||
|
||||
assert_matches!(
|
||||
from_json_value::<Raw<AnyStateEvent>>(json_data)
|
||||
@ -135,7 +136,7 @@ fn deserialize_custom_state_event() {
|
||||
.unwrap(),
|
||||
AnyStateEvent::Custom(StateEvent {
|
||||
content: CustomEventContent {
|
||||
json, event_type,
|
||||
data, event_type,
|
||||
},
|
||||
event_id,
|
||||
origin_server_ts,
|
||||
@ -144,7 +145,7 @@ fn deserialize_custom_state_event() {
|
||||
prev_content: None,
|
||||
state_key,
|
||||
unsigned,
|
||||
}) if json == expected_content && event_type == "m.reaction"
|
||||
}) if data == expected_content && event_type == "m.reaction"
|
||||
&& event_id == event_id!("$h29iv0s8:example.com")
|
||||
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(10)
|
||||
&& sender == user_id!("@carl:example.com")
|
||||
@ -158,20 +159,21 @@ fn deserialize_custom_state_event() {
|
||||
fn deserialize_custom_state_sync_event() {
|
||||
let json_data = custom_state_event();
|
||||
|
||||
let expected_content = json!({
|
||||
"m.relates_to": {
|
||||
let expected_content = btreemap! {
|
||||
"m.relates_to".into() => json!({
|
||||
"event_id": "$MDitXXXXXX",
|
||||
"key": "👍",
|
||||
"rel_type": "m.annotation"
|
||||
}
|
||||
});
|
||||
}),
|
||||
};
|
||||
|
||||
assert_matches!(
|
||||
from_json_value::<SyncStateEvent<AnyStateEventContent>>(json_data)
|
||||
.unwrap(),
|
||||
SyncStateEvent {
|
||||
content: AnyStateEventContent::Custom(CustomEventContent {
|
||||
json, event_type,
|
||||
data,
|
||||
event_type,
|
||||
}),
|
||||
event_id,
|
||||
origin_server_ts,
|
||||
@ -179,7 +181,7 @@ fn deserialize_custom_state_sync_event() {
|
||||
prev_content: None,
|
||||
state_key,
|
||||
unsigned,
|
||||
} if json == expected_content && event_type == "m.reaction"
|
||||
} if data == expected_content && event_type == "m.reaction"
|
||||
&& event_id == event_id!("$h29iv0s8:example.com")
|
||||
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(10)
|
||||
&& sender == user_id!("@carl:example.com")
|
||||
@ -204,22 +206,20 @@ fn deserialize_custom_message_sync_event() {
|
||||
}
|
||||
});
|
||||
|
||||
let expected_content = json!({
|
||||
"body": "👍",
|
||||
});
|
||||
let expected_content = btreemap! {
|
||||
"body".into() => "👍".into(),
|
||||
};
|
||||
|
||||
assert_matches!(
|
||||
from_json_value::<AnySyncRoomEvent>(json_data)
|
||||
.unwrap(),
|
||||
AnySyncRoomEvent::Message(AnySyncMessageEvent::Custom(SyncMessageEvent {
|
||||
content: CustomEventContent {
|
||||
json, event_type,
|
||||
},
|
||||
content: CustomEventContent { data, event_type },
|
||||
event_id,
|
||||
origin_server_ts,
|
||||
sender,
|
||||
unsigned,
|
||||
})) if json == expected_content && event_type == "m.ruma_custom"
|
||||
})) if data == expected_content && event_type == "m.ruma_custom"
|
||||
&& event_id == event_id!("$h29iv0s8:example.com")
|
||||
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(10)
|
||||
&& sender == user_id!("@carl:example.com")
|
||||
|
Loading…
x
Reference in New Issue
Block a user