Remove PduStub

Removed PduStub because is not used anymore on any public endpoint.
This commit is contained in:
Guillem Nieto 2020-11-18 18:44:08 +01:00 committed by Jonas Platte
parent b168c38d83
commit 34c4b6c110
3 changed files with 2 additions and 536 deletions

View File

@ -25,6 +25,7 @@ Breaking changes:
through `.as_str()`. This ensures that new event types doesn't break existing code.
* Remove the implementations of `From<EventType>` and `From<key::verification::cancel::CancelCode>`
for `String`. Use the `Display` or `ToString` implementations for those types instead.
* Remove `PduStub`, `RoomV1PduStub` and `RoomV3PduStub` types
Improvements:

View File

@ -146,184 +146,6 @@ pub struct RoomV3Pdu {
pub signatures: BTreeMap<ServerNameBox, BTreeMap<ServerSigningKeyId, String>>,
}
/// PDU type without event and room IDs.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum PduStub {
/// Stub for PDUs of room version 1 and 2.
RoomV1PduStub(RoomV1PduStub),
/// Stub for PDUs of room versions 3 and above.
RoomV3PduStub(RoomV3PduStub),
}
impl PduStub {
/// Helper method to get PDU from a PDU stub.
pub fn into_pdu(self, room_id: RoomId, event_id: EventId) -> Pdu {
match self {
PduStub::RoomV1PduStub(v1_stub) => {
Pdu::RoomV1Pdu(v1_stub.into_v1_pdu(room_id, event_id))
}
PduStub::RoomV3PduStub(v3_stub) => Pdu::RoomV3Pdu(v3_stub.into_v3_pdu(room_id)),
}
}
}
/// Stub for PDUs of room version 1 and 2.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RoomV1PduStub {
/// The user id of the user who sent this event.
pub sender: UserId,
#[cfg(not(feature = "unstable-pre-spec"))]
/// The `server_name` of the homeserver that created this event.
pub origin: String,
/// Timestamp (milliseconds since the UNIX epoch) on originating homeserver
/// of when this event was created.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub origin_server_ts: SystemTime,
// TODO: Encode event type as content enum variant, like event enums do
/// The event's type.
#[serde(rename = "type")]
pub kind: EventType,
/// The event's content.
pub content: JsonValue,
/// A key that determines which piece of room state the event represents.
#[serde(skip_serializing_if = "Option::is_none")]
pub state_key: Option<String>,
/// Event IDs for the most recent events in the room that the homeserver was
/// aware of when it created this event.
pub prev_events: Vec<(EventId, EventHash)>,
/// The maximum depth of the `prev_events`, plus one.
pub depth: UInt,
/// Event IDs for the authorization events that would allow this event to be
/// in the room.
pub auth_events: Vec<(EventId, EventHash)>,
/// For redaction events, the ID of the event being redacted.
#[serde(skip_serializing_if = "Option::is_none")]
pub redacts: Option<EventId>,
/// Additional data added by the origin server but not covered by the
/// signatures.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub unsigned: BTreeMap<String, JsonValue>,
/// Content hashes of the PDU.
pub hashes: EventHash,
/// Signatures for the PDU.
pub signatures: BTreeMap<ServerNameBox, BTreeMap<ServerSigningKeyId, String>>,
}
impl RoomV1PduStub {
/// Converts a V1 PDU stub into a full V1 PDU.
pub fn into_v1_pdu(self, room_id: RoomId, event_id: EventId) -> RoomV1Pdu {
RoomV1Pdu {
event_id,
room_id,
sender: self.sender,
#[cfg(not(feature = "unstable-pre-spec"))]
origin: self.origin,
origin_server_ts: self.origin_server_ts,
kind: self.kind,
content: self.content,
state_key: self.state_key,
prev_events: self.prev_events,
depth: self.depth,
auth_events: self.auth_events,
redacts: self.redacts,
unsigned: self.unsigned,
hashes: self.hashes,
signatures: self.signatures,
}
}
}
/// Stub for PDUs of room versions 3 and above.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RoomV3PduStub {
/// The user id of the user who sent this event.
pub sender: UserId,
#[cfg(not(feature = "unstable-pre-spec"))]
/// The `server_name` of the homeserver that created this event.
pub origin: String,
/// Timestamp (milliseconds since the UNIX epoch) on originating homeserver
/// of when this event was created.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub origin_server_ts: SystemTime,
// TODO: Encode event type as content enum variant, like event enums do
/// The event's type.
#[serde(rename = "type")]
pub kind: EventType,
/// The event's content.
pub content: JsonValue,
/// A key that determines which piece of room state the event represents.
#[serde(skip_serializing_if = "Option::is_none")]
pub state_key: Option<String>,
/// Event IDs for the most recent events in the room that the homeserver was
/// aware of when it created this event.
pub prev_events: Vec<EventId>,
/// The maximum depth of the `prev_events`, plus one.
pub depth: UInt,
/// Event IDs for the authorization events that would allow this event to be
/// in the room.
pub auth_events: Vec<EventId>,
/// For redaction events, the ID of the event being redacted.
#[serde(skip_serializing_if = "Option::is_none")]
pub redacts: Option<EventId>,
/// Additional data added by the origin server but not covered by the
/// signatures.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub unsigned: BTreeMap<String, JsonValue>,
/// Content hashes of the PDU.
pub hashes: EventHash,
/// Signatures for the PDU.
pub signatures: BTreeMap<ServerNameBox, BTreeMap<ServerSigningKeyId, String>>,
}
impl RoomV3PduStub {
/// Converts a V3 PDU stub into a full V3 PDU.
pub fn into_v3_pdu(self, room_id: RoomId) -> RoomV3Pdu {
RoomV3Pdu {
room_id,
sender: self.sender,
#[cfg(not(feature = "unstable-pre-spec"))]
origin: self.origin,
origin_server_ts: self.origin_server_ts,
kind: self.kind,
content: self.content,
state_key: self.state_key,
prev_events: self.prev_events,
depth: self.depth,
auth_events: self.auth_events,
redacts: self.redacts,
unsigned: self.unsigned,
hashes: self.hashes,
signatures: self.signatures,
}
}
}
/// Content hashes of a PDU.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EventHash {

View File

@ -5,230 +5,13 @@ use std::{
time::{Duration, SystemTime},
};
use matches::assert_matches;
use ruma_events::{
pdu::{EventHash, Pdu, PduStub, RoomV1Pdu, RoomV1PduStub, RoomV3Pdu, RoomV3PduStub},
pdu::{EventHash, Pdu, RoomV1Pdu, RoomV3Pdu},
EventType,
};
use ruma_identifiers::{event_id, room_id, server_key_id, server_name, user_id};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
#[test]
fn serialize_stub_as_v1() {
let mut signatures = BTreeMap::new();
let mut inner_signature = BTreeMap::new();
inner_signature.insert(
server_key_id!("ed25519:key_version"),
"86BytesOfSignatureOfTheRedactedEvent".into(),
);
signatures.insert(server_name!("example.com"), inner_signature);
let mut unsigned = BTreeMap::new();
unsigned.insert("somekey".into(), json!({"a": 456}));
let v1_stub = RoomV1PduStub {
sender: user_id!("@sender:example.com"),
origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}),
state_key: Some("state".into()),
prev_events: vec![(
event_id!("$previousevent:matrix.org"),
EventHash { sha256: "123567".into() },
)],
depth: 2_u32.into(),
auth_events: vec![(
event_id!("$someauthevent:matrix.org"),
EventHash { sha256: "21389CFEDABC".into() },
)],
redacts: Some(event_id!("$9654:matrix.org")),
unsigned,
hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures,
};
let pdu_stub = PduStub::RoomV1PduStub(v1_stub);
let json = json!({
"sender": "@sender:example.com",
"origin": "matrix.org",
"origin_server_ts": 1_592_050_773_658usize,
"type": "m.room.power_levels",
"content": {
"testing": 123
},
"state_key": "state",
"prev_events": [
[ "$previousevent:matrix.org", {"sha256": "123567"} ]
],
"depth": 2,
"auth_events": [
["$someauthevent:matrix.org", {"sha256": "21389CFEDABC"}]
],
"redacts": "$9654:matrix.org",
"unsigned": {
"somekey": { "a": 456 } },
"hashes": { "sha256": "1233543bABACDEF" },
"signatures": {
"example.com": { "ed25519:key_version":"86BytesOfSignatureOfTheRedactedEvent" }
}
});
assert_eq!(to_json_value(&pdu_stub).unwrap(), json);
}
#[test]
fn serialize_stub_as_v3() {
let mut signatures = BTreeMap::new();
let mut inner_signature = BTreeMap::new();
inner_signature.insert(
server_key_id!("ed25519:key_version"),
"86BytesOfSignatureOfTheRedactedEvent".into(),
);
signatures.insert(server_name!("example.com"), inner_signature);
let mut unsigned = BTreeMap::new();
unsigned.insert("somekey".into(), json!({"a": 456}));
let v3_stub = RoomV3PduStub {
sender: user_id!("@sender:example.com"),
origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}),
state_key: Some("state".into()),
prev_events: vec![event_id!("$previousevent:matrix.org")],
depth: 2_u32.into(),
auth_events: vec![event_id!("$someauthevent:matrix.org")],
redacts: Some(event_id!("$9654:matrix.org")),
unsigned,
hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures,
};
let pdu_stub = PduStub::RoomV3PduStub(v3_stub);
let json = json!({
"sender": "@sender:example.com",
"origin": "matrix.org",
"origin_server_ts": 1_592_050_773_658usize,
"type": "m.room.power_levels",
"content": {
"testing": 123
},
"state_key": "state",
"prev_events": [ "$previousevent:matrix.org" ],
"depth": 2,
"auth_events": ["$someauthevent:matrix.org" ],
"redacts": "$9654:matrix.org",
"unsigned": {
"somekey": { "a": 456 } },
"hashes": { "sha256": "1233543bABACDEF" },
"signatures": {
"example.com": { "ed25519:key_version":"86BytesOfSignatureOfTheRedactedEvent" }
}
});
assert_eq!(to_json_value(&pdu_stub).unwrap(), json);
}
#[test]
fn deserialize_stub_as_v1() {
let json = json!({
"auth_events": [
[
"$abc123:matrix.org",
{
"sha256": "Base64EncodedSha256HashesShouldBe43BytesLong"
}
]
],
"content": {
"key": "value"
},
"depth": 12,
"event_id": "$a4ecee13e2accdadf56c1025:example.com",
"hashes": {
"sha256": "ThisHashCoversAllFieldsInCaseThisIsRedacted"
},
"origin": "matrix.org",
"origin_server_ts": 1_234_567_890,
"prev_events": [
[
"$abc123:matrix.org",
{
"sha256": "Base64EncodedSha256HashesShouldBe43BytesLong"
}
]
],
"redacts": "$def456:matrix.org",
"room_id": "!abc123:matrix.org",
"sender": "@someone:matrix.org",
"signatures": {
"example.com": {
"ed25519:key_version": "86BytesOfSignatureOfTheRedactedEvent"
}
},
"state_key": "my_key",
"type": "m.room.message",
"unsigned": {
"key": "value"
}
});
let parsed = from_json_value::<PduStub>(json).unwrap();
match parsed {
PduStub::RoomV1PduStub(v1_stub) => {
assert_eq!(v1_stub.auth_events.first().unwrap().0, event_id!("$abc123:matrix.org"));
assert_eq!(
v1_stub.auth_events.first().unwrap().1.sha256,
"Base64EncodedSha256HashesShouldBe43BytesLong"
);
}
PduStub::RoomV3PduStub(_) => panic!("Matched V3 stub"),
}
}
#[test]
fn deserialize_stub_as_v3() {
let json = json!({
"auth_events": [
"$abc123:matrix.org"
],
"content": {
"key": "value"
},
"depth": 12,
"event_id": "$a4ecee13e2accdadf56c1025:example.com",
"hashes": {
"sha256": "ThisHashCoversAllFieldsInCaseThisIsRedacted"
},
"origin": "matrix.org",
"origin_server_ts": 1_234_567_890,
"prev_events": [
"$abc123:matrix.org"
],
"redacts": "$def456:matrix.org",
"room_id": "!abc123:matrix.org",
"sender": "@someone:matrix.org",
"signatures": {
"example.com": {
"ed25519:key_version": "86BytesOfSignatureOfTheRedactedEvent"
}
},
"state_key": "my_key",
"type": "m.room.message",
"unsigned": {
"key": "value"
}
});
let parsed = from_json_value::<PduStub>(json).unwrap();
match parsed {
PduStub::RoomV1PduStub(_) => panic!("Matched V1 stub"),
PduStub::RoomV3PduStub(v3_stub) => {
assert_eq!(v3_stub.auth_events.first().unwrap(), &event_id!("$abc123:matrix.org"));
}
}
}
#[test]
fn serialize_pdu_as_v1() {
let mut signatures = BTreeMap::new();
@ -454,143 +237,3 @@ fn deserialize_pdu_as_v3() {
}
}
}
#[test]
fn convert_v1_stub_to_pdu() {
let mut signatures = BTreeMap::new();
let mut inner_signature = BTreeMap::new();
inner_signature.insert(
server_key_id!("ed25519:key_version"),
"86BytesOfSignatureOfTheRedactedEvent".into(),
);
signatures.insert(server_name!("example.com"), inner_signature);
let mut unsigned = BTreeMap::new();
unsigned.insert("somekey".into(), json!({"a": 456}));
let v1_stub = RoomV1PduStub {
sender: user_id!("@sender:example.com"),
origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}),
state_key: Some("state".into()),
prev_events: vec![(
event_id!("$previousevent:matrix.org"),
EventHash { sha256: "123567".into() },
)],
depth: 2_u32.into(),
auth_events: vec![(
event_id!("$someauthevent:matrix.org"),
EventHash { sha256: "21389CFEDABC".into() },
)],
redacts: Some(event_id!("$9654:matrix.org")),
unsigned: (&unsigned).clone(),
hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures: (&signatures).clone(),
};
assert_matches!(
v1_stub.into_v1_pdu(
room_id!("!n8f893n9:example.com"),
event_id!("$somejoinevent:matrix.org")
),
RoomV1Pdu {
room_id,
event_id,
sender,
origin,
origin_server_ts,
kind,
content,
state_key,
prev_events,
depth,
auth_events,
redacts,
unsigned,
hashes: EventHash { sha256 },
signatures,
} if room_id == room_id!("!n8f893n9:example.com")
&& event_id == event_id!("$somejoinevent:matrix.org")
&& sender == user_id!("@sender:example.com")
&& origin == "matrix.org"
&& origin_server_ts == SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658)
&& kind == EventType::RoomPowerLevels
&& content == json!({"testing": 123})
&& state_key == Some("state".into())
&& prev_events[0].0 == event_id!("$previousevent:matrix.org")
&& prev_events[0].1.sha256 == "123567"
&& depth == 2_u32.into()
&& auth_events.first().unwrap().0 == event_id!("$someauthevent:matrix.org")
&& auth_events.first().unwrap().1.sha256 == "21389CFEDABC"
&& redacts == Some(event_id!("$9654:matrix.org"))
&& unsigned == (&unsigned).clone()
&& sha256 == "1233543bABACDEF"
&& signatures == (&signatures).clone()
);
}
#[test]
fn convert_v3_stub_to_pdu() {
let mut signatures = BTreeMap::new();
let mut inner_signature = BTreeMap::new();
inner_signature.insert(
server_key_id!("ed25519:key_version"),
"86BytesOfSignatureOfTheRedactedEvent".into(),
);
signatures.insert(server_name!("example.com"), inner_signature);
let mut unsigned = BTreeMap::new();
unsigned.insert("somekey".into(), json!({"a": 456}));
let v3_stub = RoomV3PduStub {
sender: user_id!("@sender:example.com"),
origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}),
state_key: Some("state".into()),
prev_events: vec![event_id!("$previousevent:matrix.org")],
depth: 2_u32.into(),
auth_events: vec![event_id!("$someauthevent:matrix.org")],
redacts: Some(event_id!("$9654:matrix.org")),
unsigned: (&unsigned).clone(),
hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures: (&signatures).clone(),
};
assert_matches!(
v3_stub.into_v3_pdu(room_id!("!n8f893n9:example.com")),
RoomV3Pdu {
room_id,
sender,
origin,
origin_server_ts,
kind,
content,
state_key,
prev_events,
depth,
auth_events,
redacts,
unsigned,
hashes: EventHash { sha256 },
signatures,
} if room_id == room_id!("!n8f893n9:example.com")
&& sender == user_id!("@sender:example.com")
&& origin == "matrix.org"
&& origin_server_ts == SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658)
&& kind == EventType::RoomPowerLevels
&& content == json!({"testing": 123})
&& state_key == Some("state".into())
&& prev_events == vec![event_id!("$previousevent:matrix.org")]
&& depth == 2_u32.into()
&& auth_events == vec![event_id!("$someauthevent:matrix.org")]
&& redacts == Some(event_id!("$9654:matrix.org"))
&& unsigned == (&unsigned).clone()
&& sha256 == "1233543bABACDEF"
&& signatures == (&signatures).clone()
);
}