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