Remove StateResolution::apply_event, fix test serde make pdu struct
This commit is contained in:
parent
a9e248da34
commit
c2988d4b8d
@ -36,4 +36,24 @@ resolve state of 10 events 3 conflicting
|
|||||||
change: [-0.8433% -0.3270% +0.2656%] (p = 0.25 > 0.05)
|
change: [-0.8433% -0.3270% +0.2656%] (p = 0.25 > 0.05)
|
||||||
No change in performance detected.
|
No change in performance detected.
|
||||||
Found 1 outliers among 100 measurements (1.00%)
|
Found 1 outliers among 100 measurements (1.00%)
|
||||||
1 (1.00%) high mild
|
1 (1.00%) high mild
|
||||||
|
|
||||||
|
4/26/2020 BRANCH: fix-test-serde REV:
|
||||||
|
lexicographical topological sort
|
||||||
|
time: [1.6793 us 1.6823 us 1.6857 us]
|
||||||
|
Found 9 outliers among 100 measurements (9.00%)
|
||||||
|
1 (1.00%) low mild
|
||||||
|
4 (4.00%) high mild
|
||||||
|
4 (4.00%) high severe
|
||||||
|
|
||||||
|
resolve state of 5 events one fork
|
||||||
|
time: [9.9993 us 10.062 us 10.159 us]
|
||||||
|
Found 9 outliers among 100 measurements (9.00%)
|
||||||
|
7 (7.00%) high mild
|
||||||
|
2 (2.00%) high severe
|
||||||
|
|
||||||
|
resolve state of 10 events 3 conflicting
|
||||||
|
time: [26.004 us 26.092 us 26.195 us]
|
||||||
|
Found 16 outliers among 100 measurements (16.00%)
|
||||||
|
11 (11.00%) high mild
|
||||||
|
5 (5.00%) high severe
|
@ -4,12 +4,19 @@
|
|||||||
// `cargo bench unknown option --save-baseline`.
|
// `cargo bench unknown option --save-baseline`.
|
||||||
// To pass args to criterion, use this form
|
// To pass args to criterion, use this form
|
||||||
// `cargo bench --bench <name of the bench> -- --save-baseline <name>`.
|
// `cargo bench --bench <name of the bench> -- --save-baseline <name>`.
|
||||||
use std::{collections::BTreeMap, convert::TryFrom, sync::Arc, time::UNIX_EPOCH};
|
use std::{
|
||||||
|
collections::BTreeMap,
|
||||||
|
convert::TryFrom,
|
||||||
|
sync::Arc,
|
||||||
|
time::{Duration, UNIX_EPOCH},
|
||||||
|
};
|
||||||
|
|
||||||
use criterion::{criterion_group, criterion_main, Criterion};
|
use criterion::{criterion_group, criterion_main, Criterion};
|
||||||
|
use event::StateEvent;
|
||||||
use maplit::btreemap;
|
use maplit::btreemap;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
events::{
|
events::{
|
||||||
|
pdu::{EventHash, Pdu, RoomV3Pdu},
|
||||||
room::{
|
room::{
|
||||||
join_rules::JoinRule,
|
join_rules::JoinRule,
|
||||||
member::{MemberEventContent, MembershipState},
|
member::{MemberEventContent, MembershipState},
|
||||||
@ -21,7 +28,7 @@ use ruma::{
|
|||||||
use serde_json::{json, Value as JsonValue};
|
use serde_json::{json, Value as JsonValue};
|
||||||
use state_res::{Error, Event, Result, StateMap, StateResolution, StateStore};
|
use state_res::{Error, Event, Result, StateMap, StateResolution, StateStore};
|
||||||
|
|
||||||
static mut SERVER_TIMESTAMP: i32 = 0;
|
static mut SERVER_TIMESTAMP: u64 = 0;
|
||||||
|
|
||||||
fn lexico_topo_sort(c: &mut Criterion) {
|
fn lexico_topo_sort(c: &mut Criterion) {
|
||||||
c.bench_function("lexicographical topological sort", |b| {
|
c.bench_function("lexicographical topological sort", |b| {
|
||||||
@ -289,7 +296,7 @@ fn member_content_join() -> JsonValue {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_pdu_event<S>(
|
pub fn to_pdu_event<S>(
|
||||||
id: &str,
|
id: &str,
|
||||||
sender: UserId,
|
sender: UserId,
|
||||||
ev_type: EventType,
|
ev_type: EventType,
|
||||||
@ -297,7 +304,7 @@ fn to_pdu_event<S>(
|
|||||||
content: JsonValue,
|
content: JsonValue,
|
||||||
auth_events: &[S],
|
auth_events: &[S],
|
||||||
prev_events: &[S],
|
prev_events: &[S],
|
||||||
) -> Arc<event::StateEvent>
|
) -> Arc<StateEvent>
|
||||||
where
|
where
|
||||||
S: AsRef<str>,
|
S: AsRef<str>,
|
||||||
{
|
{
|
||||||
@ -323,39 +330,27 @@ where
|
|||||||
.map(event_id)
|
.map(event_id)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let json = if let Some(state_key) = state_key {
|
let state_key = state_key.map(ToString::to_string);
|
||||||
json!({
|
Arc::new(StateEvent {
|
||||||
"auth_events": auth_events,
|
event_id: EventId::try_from(id).unwrap(),
|
||||||
"prev_events": prev_events,
|
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
|
||||||
"event_id": id,
|
room_id: room_id(),
|
||||||
"sender": sender,
|
sender,
|
||||||
"type": ev_type,
|
origin_server_ts: UNIX_EPOCH + Duration::from_secs(ts),
|
||||||
"state_key": state_key,
|
state_key,
|
||||||
"content": content,
|
kind: ev_type,
|
||||||
"origin_server_ts": ts,
|
content,
|
||||||
"room_id": room_id(),
|
redacts: None,
|
||||||
"origin": "foo",
|
unsigned: btreemap! {},
|
||||||
"depth": 0,
|
#[cfg(not(feature = "unstable-pre-spec"))]
|
||||||
"hashes": { "sha256": "hello" },
|
origin: "foo".into(),
|
||||||
"signatures": {},
|
auth_events,
|
||||||
})
|
prev_events,
|
||||||
} else {
|
depth: ruma::uint!(0),
|
||||||
json!({
|
hashes: EventHash { sha256: "".into() },
|
||||||
"auth_events": auth_events,
|
signatures: btreemap! {},
|
||||||
"prev_events": prev_events,
|
}),
|
||||||
"event_id": id,
|
})
|
||||||
"sender": sender,
|
|
||||||
"type": ev_type,
|
|
||||||
"content": content,
|
|
||||||
"origin_server_ts": ts,
|
|
||||||
"room_id": room_id(),
|
|
||||||
"origin": "foo",
|
|
||||||
"depth": 0,
|
|
||||||
"hashes": { "sha256": "hello" },
|
|
||||||
"signatures": {},
|
|
||||||
})
|
|
||||||
};
|
|
||||||
Arc::new(serde_json::from_value(json).unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// all graphs start with these input events
|
// all graphs start with these input events
|
||||||
@ -491,17 +486,14 @@ pub mod event {
|
|||||||
|
|
||||||
use ruma::{
|
use ruma::{
|
||||||
events::{
|
events::{
|
||||||
from_raw_json_value,
|
|
||||||
pdu::{EventHash, Pdu},
|
pdu::{EventHash, Pdu},
|
||||||
room::member::{MemberEventContent, MembershipState},
|
room::member::{MemberEventContent, MembershipState},
|
||||||
EventDeHelper, EventType,
|
EventType,
|
||||||
},
|
},
|
||||||
serde::CanonicalJsonValue,
|
|
||||||
signatures::reference_hash,
|
|
||||||
EventId, RoomId, RoomVersionId, ServerName, ServerSigningKeyId, UInt, UserId,
|
EventId, RoomId, RoomVersionId, ServerName, ServerSigningKeyId, UInt, UserId,
|
||||||
};
|
};
|
||||||
use serde::{de, ser, Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{value::RawValue as RawJsonValue, Value as JsonValue};
|
use serde_json::Value as JsonValue;
|
||||||
|
|
||||||
use state_res::Event;
|
use state_res::Event;
|
||||||
|
|
||||||
@ -555,92 +547,10 @@ pub mod event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
struct EventIdHelper {
|
pub struct StateEvent {
|
||||||
event_id: EventId,
|
pub event_id: EventId,
|
||||||
}
|
#[serde(flatten)]
|
||||||
|
pub rest: Pdu,
|
||||||
fn event_id<E: de::Error>(json: &RawJsonValue) -> Result<EventId, E> {
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
Ok(match from_raw_json_value::<EventIdHelper, E>(&json) {
|
|
||||||
Ok(id) => id.event_id,
|
|
||||||
Err(_) => {
|
|
||||||
// panic!("NOT DURING TESTS");
|
|
||||||
EventId::try_from(format!(
|
|
||||||
"${}",
|
|
||||||
reference_hash(&from_raw_json_value(&json)?, &RoomVersionId::Version6)
|
|
||||||
.map_err(de::Error::custom)?,
|
|
||||||
))
|
|
||||||
.map_err(de::Error::custom)?
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: This no longer needs to be an enum now that PduStub is gone
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub enum StateEvent {
|
|
||||||
Full(EventId, Pdu),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for StateEvent {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: ser::Serializer,
|
|
||||||
{
|
|
||||||
use ser::Error;
|
|
||||||
use std::convert::TryInto;
|
|
||||||
|
|
||||||
match self {
|
|
||||||
Self::Full(id, ev) => {
|
|
||||||
// TODO: do we want to add the eventId when we
|
|
||||||
// serialize
|
|
||||||
let val: CanonicalJsonValue = serde_json::to_value(ev)
|
|
||||||
.map_err(S::Error::custom)?
|
|
||||||
.try_into()
|
|
||||||
.map_err(S::Error::custom)?;
|
|
||||||
|
|
||||||
match val {
|
|
||||||
CanonicalJsonValue::Object(mut obj) => {
|
|
||||||
obj.insert(
|
|
||||||
"event_id".into(),
|
|
||||||
ruma::serde::to_canonical_value(id).map_err(S::Error::custom)?,
|
|
||||||
);
|
|
||||||
obj.serialize(serializer)
|
|
||||||
}
|
|
||||||
_ => panic!("Pdu not an object"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de> de::Deserialize<'de> for StateEvent {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
||||||
where
|
|
||||||
D: de::Deserializer<'de>,
|
|
||||||
{
|
|
||||||
let json = Box::<RawJsonValue>::deserialize(deserializer)?;
|
|
||||||
let EventDeHelper {
|
|
||||||
room_id, unsigned, ..
|
|
||||||
} = from_raw_json_value(&json)?;
|
|
||||||
|
|
||||||
// TODO: do we even want to try for the existing ID
|
|
||||||
|
|
||||||
// Determine whether the event is a full or stub
|
|
||||||
// based on the fields present.
|
|
||||||
Ok(if room_id.is_some() {
|
|
||||||
match unsigned {
|
|
||||||
Some(unsigned) if unsigned.redacted_because.is_some() => {
|
|
||||||
panic!("TODO deal with redacted events")
|
|
||||||
}
|
|
||||||
_ => StateEvent::Full(
|
|
||||||
event_id(&json)?,
|
|
||||||
Pdu::RoomV3Pdu(from_raw_json_value(&json)?),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic!("Found stub event")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StateEvent {
|
impl StateEvent {
|
||||||
@ -648,209 +558,168 @@ pub mod event {
|
|||||||
id: EventId,
|
id: EventId,
|
||||||
json: serde_json::Value,
|
json: serde_json::Value,
|
||||||
) -> Result<Self, serde_json::Error> {
|
) -> Result<Self, serde_json::Error> {
|
||||||
Ok(Self::Full(
|
Ok(Self {
|
||||||
id,
|
event_id: id,
|
||||||
Pdu::RoomV3Pdu(serde_json::from_value(json)?),
|
rest: Pdu::RoomV3Pdu(serde_json::from_value(json)?),
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_id_canon_obj(
|
pub fn from_id_canon_obj(
|
||||||
id: EventId,
|
id: EventId,
|
||||||
json: ruma::serde::CanonicalJsonObject,
|
json: ruma::serde::CanonicalJsonObject,
|
||||||
) -> Result<Self, serde_json::Error> {
|
) -> Result<Self, serde_json::Error> {
|
||||||
Ok(Self::Full(
|
Ok(Self {
|
||||||
id,
|
event_id: id,
|
||||||
// TODO: this is unfortunate (from_value(to_value(json)))...
|
// TODO: this is unfortunate (from_value(to_value(json)))...
|
||||||
Pdu::RoomV3Pdu(serde_json::from_value(serde_json::to_value(json)?)?),
|
rest: Pdu::RoomV3Pdu(serde_json::from_value(serde_json::to_value(json)?)?),
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_power_event(&self) -> bool {
|
pub fn is_power_event(&self) -> bool {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, any_event) => match any_event {
|
Pdu::RoomV1Pdu(event) => match event.kind {
|
||||||
Pdu::RoomV1Pdu(event) => match event.kind {
|
EventType::RoomPowerLevels
|
||||||
EventType::RoomPowerLevels
|
| EventType::RoomJoinRules
|
||||||
| EventType::RoomJoinRules
|
| EventType::RoomCreate => event.state_key == Some("".into()),
|
||||||
| EventType::RoomCreate => event.state_key == Some("".into()),
|
EventType::RoomMember => {
|
||||||
EventType::RoomMember => {
|
// TODO fix clone
|
||||||
// TODO fix clone
|
if let Ok(content) =
|
||||||
if let Ok(content) =
|
serde_json::from_value::<MemberEventContent>(event.content.clone())
|
||||||
serde_json::from_value::<MemberEventContent>(event.content.clone())
|
{
|
||||||
|
if [MembershipState::Leave, MembershipState::Ban]
|
||||||
|
.contains(&content.membership)
|
||||||
{
|
{
|
||||||
if [MembershipState::Leave, MembershipState::Ban]
|
return event.sender.as_str()
|
||||||
.contains(&content.membership)
|
|
||||||
{
|
|
||||||
return event.sender.as_str()
|
|
||||||
// TODO is None here a failure
|
// TODO is None here a failure
|
||||||
!= event.state_key.as_deref().unwrap_or("NOT A STATE KEY");
|
!= event.state_key.as_deref().unwrap_or("NOT A STATE KEY");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
_ => false,
|
|
||||||
},
|
false
|
||||||
Pdu::RoomV3Pdu(event) => event.state_key == Some("".into()),
|
}
|
||||||
|
_ => false,
|
||||||
},
|
},
|
||||||
|
Pdu::RoomV3Pdu(event) => event.state_key == Some("".into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn deserialize_content<C: serde::de::DeserializeOwned>(
|
pub fn deserialize_content<C: serde::de::DeserializeOwned>(
|
||||||
&self,
|
&self,
|
||||||
) -> Result<C, serde_json::Error> {
|
) -> Result<C, serde_json::Error> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => serde_json::from_value(ev.content.clone()),
|
||||||
Pdu::RoomV1Pdu(ev) => serde_json::from_value(ev.content.clone()),
|
Pdu::RoomV3Pdu(ev) => serde_json::from_value(ev.content.clone()),
|
||||||
Pdu::RoomV3Pdu(ev) => serde_json::from_value(ev.content.clone()),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn origin_server_ts(&self) -> &SystemTime {
|
pub fn origin_server_ts(&self) -> &SystemTime {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.origin_server_ts,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.origin_server_ts,
|
Pdu::RoomV3Pdu(ev) => &ev.origin_server_ts,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.origin_server_ts,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn event_id(&self) -> &EventId {
|
pub fn event_id(&self) -> &EventId {
|
||||||
match self {
|
&self.event_id
|
||||||
// TODO; make this a &EventId
|
|
||||||
Self::Full(id, _) => id,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sender(&self) -> &UserId {
|
pub fn sender(&self) -> &UserId {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.sender,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.sender,
|
Pdu::RoomV3Pdu(ev) => &ev.sender,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.sender,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn redacts(&self) -> Option<&EventId> {
|
pub fn redacts(&self) -> Option<&EventId> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.redacts.as_ref(),
|
||||||
Pdu::RoomV1Pdu(ev) => ev.redacts.as_ref(),
|
Pdu::RoomV3Pdu(ev) => ev.redacts.as_ref(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.redacts.as_ref(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn room_id(&self) -> &RoomId {
|
pub fn room_id(&self) -> &RoomId {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.room_id,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.room_id,
|
Pdu::RoomV3Pdu(ev) => &ev.room_id,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.room_id,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn kind(&self) -> EventType {
|
pub fn kind(&self) -> EventType {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.kind.clone(),
|
||||||
Pdu::RoomV1Pdu(ev) => ev.kind.clone(),
|
Pdu::RoomV3Pdu(ev) => ev.kind.clone(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.kind.clone(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn state_key(&self) -> Option<String> {
|
pub fn state_key(&self) -> Option<String> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.state_key.clone(),
|
||||||
Pdu::RoomV1Pdu(ev) => ev.state_key.clone(),
|
Pdu::RoomV3Pdu(ev) => ev.state_key.clone(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.state_key.clone(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "unstable-pre-spec"))]
|
#[cfg(not(feature = "unstable-pre-spec"))]
|
||||||
pub fn origin(&self) -> String {
|
pub fn origin(&self) -> String {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.origin.clone(),
|
||||||
Pdu::RoomV1Pdu(ev) => ev.origin.clone(),
|
Pdu::RoomV3Pdu(ev) => ev.origin.clone(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.origin.clone(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prev_event_ids(&self) -> Vec<EventId> {
|
pub fn prev_event_ids(&self) -> Vec<EventId> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.prev_events.iter().map(|(id, _)| id).cloned().collect(),
|
||||||
Pdu::RoomV1Pdu(ev) => {
|
Pdu::RoomV3Pdu(ev) => ev.prev_events.clone(),
|
||||||
ev.prev_events.iter().map(|(id, _)| id).cloned().collect()
|
|
||||||
}
|
|
||||||
Pdu::RoomV3Pdu(ev) => ev.prev_events.clone(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn auth_events(&self) -> Vec<EventId> {
|
pub fn auth_events(&self) -> Vec<EventId> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.auth_events.iter().map(|(id, _)| id).cloned().collect(),
|
||||||
Pdu::RoomV1Pdu(ev) => {
|
Pdu::RoomV3Pdu(ev) => ev.auth_events.to_vec(),
|
||||||
ev.auth_events.iter().map(|(id, _)| id).cloned().collect()
|
|
||||||
}
|
|
||||||
Pdu::RoomV3Pdu(ev) => ev.auth_events.to_vec(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn content(&self) -> serde_json::Value {
|
pub fn content(&self) -> serde_json::Value {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.content.clone(),
|
||||||
Pdu::RoomV1Pdu(ev) => ev.content.clone(),
|
Pdu::RoomV3Pdu(ev) => ev.content.clone(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.content.clone(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unsigned(&self) -> &BTreeMap<String, serde_json::Value> {
|
pub fn unsigned(&self) -> &BTreeMap<String, serde_json::Value> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.unsigned,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.unsigned,
|
Pdu::RoomV3Pdu(ev) => &ev.unsigned,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.unsigned,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn signatures(
|
pub fn signatures(
|
||||||
&self,
|
&self,
|
||||||
) -> BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, String>> {
|
) -> BTreeMap<Box<ServerName>, BTreeMap<ruma::ServerSigningKeyId, String>> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(_) => maplit::btreemap! {},
|
||||||
Pdu::RoomV1Pdu(_) => maplit::btreemap! {},
|
Pdu::RoomV3Pdu(ev) => ev.signatures.clone(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.signatures.clone(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hashes(&self) -> &EventHash {
|
pub fn hashes(&self) -> &EventHash {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.hashes,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.hashes,
|
Pdu::RoomV3Pdu(ev) => &ev.hashes,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.hashes,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn depth(&self) -> &UInt {
|
pub fn depth(&self) -> &UInt {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.depth,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.depth,
|
Pdu::RoomV3Pdu(ev) => &ev.depth,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.depth,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_type_and_key(&self, ev_type: EventType, state_key: &str) -> bool {
|
pub fn is_type_and_key(&self, ev_type: EventType, state_key: &str) -> bool {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => {
|
||||||
Pdu::RoomV1Pdu(ev) => {
|
ev.kind == ev_type && ev.state_key.as_deref() == Some(state_key)
|
||||||
ev.kind == ev_type && ev.state_key.as_deref() == Some(state_key)
|
}
|
||||||
}
|
Pdu::RoomV3Pdu(ev) => {
|
||||||
Pdu::RoomV3Pdu(ev) => {
|
ev.kind == ev_type && ev.state_key.as_deref() == Some(state_key)
|
||||||
ev.kind == ev_type && ev.state_key.as_deref() == Some(state_key)
|
}
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -860,11 +729,9 @@ pub mod event {
|
|||||||
/// version 3 and above.
|
/// version 3 and above.
|
||||||
pub fn room_version(&self) -> RoomVersionId {
|
pub fn room_version(&self) -> RoomVersionId {
|
||||||
// TODO: We have to know the actual room version this is not sufficient
|
// TODO: We have to know the actual room version this is not sufficient
|
||||||
match self {
|
match self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(_) => RoomVersionId::Version1,
|
||||||
Pdu::RoomV1Pdu(_) => RoomVersionId::Version1,
|
Pdu::RoomV3Pdu(_) => RoomVersionId::Version6,
|
||||||
Pdu::RoomV3Pdu(_) => RoomVersionId::Version6,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
42
src/lib.rs
42
src/lib.rs
@ -38,48 +38,6 @@ pub type EventMap<T> = BTreeMap<EventId, T>;
|
|||||||
pub struct StateResolution;
|
pub struct StateResolution;
|
||||||
|
|
||||||
impl StateResolution {
|
impl StateResolution {
|
||||||
/// Check if the `incoming_event` can be included in the given `current_state`.
|
|
||||||
///
|
|
||||||
/// This will authenticate the event against the current state of the room. It
|
|
||||||
/// is important that the `current_state` argument is accurate and complete.
|
|
||||||
pub fn apply_event<E: Event>(
|
|
||||||
room_id: &RoomId,
|
|
||||||
room_version: &RoomVersionId,
|
|
||||||
incoming_event: Arc<E>,
|
|
||||||
current_state: &StateMap<EventId>,
|
|
||||||
event_map: &EventMap<Arc<E>>,
|
|
||||||
) -> Result<bool> {
|
|
||||||
let state_key = incoming_event
|
|
||||||
.state_key()
|
|
||||||
.ok_or_else(|| Error::InvalidPdu("State event had no state key".to_owned()))?;
|
|
||||||
|
|
||||||
log::info!("Applying a single event, state resolution starting");
|
|
||||||
let ev = incoming_event;
|
|
||||||
|
|
||||||
let prev_event = if let Some(id) = ev.prev_events().first() {
|
|
||||||
event_map.get(id).map(Arc::clone)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut auth_events = StateMap::new();
|
|
||||||
for key in event_auth::auth_types_for_event(
|
|
||||||
&ev.kind(),
|
|
||||||
&ev.sender(),
|
|
||||||
Some(state_key),
|
|
||||||
ev.content(),
|
|
||||||
) {
|
|
||||||
if let Some(ev_id) = current_state.get(&key) {
|
|
||||||
if let Ok(event) = StateResolution::get_or_load_event(room_id, ev_id, event_map) {
|
|
||||||
// TODO synapse checks `rejected_reason` is None here
|
|
||||||
auth_events.insert(key.clone(), event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
event_auth::auth_check(room_version, &ev, prev_event, &auth_events, None)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Resolve sets of state events as they come in. Internally `StateResolution` builds a graph
|
/// Resolve sets of state events as they come in. Internally `StateResolution` builds a graph
|
||||||
/// and an auth chain to allow for state conflict resolution.
|
/// and an auth chain to allow for state conflict resolution.
|
||||||
///
|
///
|
||||||
|
496
tests/utils.rs
496
tests/utils.rs
@ -4,11 +4,13 @@ use std::{
|
|||||||
collections::BTreeMap,
|
collections::BTreeMap,
|
||||||
convert::TryFrom,
|
convert::TryFrom,
|
||||||
sync::{Arc, Once},
|
sync::{Arc, Once},
|
||||||
time::UNIX_EPOCH,
|
time::{Duration, UNIX_EPOCH},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use maplit::btreemap;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
events::{
|
events::{
|
||||||
|
pdu::{EventHash, Pdu, RoomV3Pdu},
|
||||||
room::{
|
room::{
|
||||||
join_rules::JoinRule,
|
join_rules::JoinRule,
|
||||||
member::{MemberEventContent, MembershipState},
|
member::{MemberEventContent, MembershipState},
|
||||||
@ -25,7 +27,7 @@ pub use event::StateEvent;
|
|||||||
|
|
||||||
pub static LOGGER: Once = Once::new();
|
pub static LOGGER: Once = Once::new();
|
||||||
|
|
||||||
static mut SERVER_TIMESTAMP: i32 = 0;
|
static mut SERVER_TIMESTAMP: u64 = 0;
|
||||||
|
|
||||||
pub fn do_check(
|
pub fn do_check(
|
||||||
events: &[Arc<StateEvent>],
|
events: &[Arc<StateEvent>],
|
||||||
@ -39,8 +41,10 @@ pub fn do_check(
|
|||||||
.init()
|
.init()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let init_events = INITIAL_EVENTS();
|
||||||
|
|
||||||
let mut store = TestStore(
|
let mut store = TestStore(
|
||||||
INITIAL_EVENTS()
|
init_events
|
||||||
.values()
|
.values()
|
||||||
.chain(events)
|
.chain(events)
|
||||||
.map(|ev| (ev.event_id().clone(), ev.clone()))
|
.map(|ev| (ev.event_id().clone(), ev.clone()))
|
||||||
@ -54,7 +58,7 @@ pub fn do_check(
|
|||||||
|
|
||||||
// create the DB of events that led up to this point
|
// create the DB of events that led up to this point
|
||||||
// TODO maybe clean up some of these clones it is just tests but...
|
// TODO maybe clean up some of these clones it is just tests but...
|
||||||
for ev in INITIAL_EVENTS().values().chain(events) {
|
for ev in init_events.values().chain(events) {
|
||||||
graph.insert(ev.event_id().clone(), vec![]);
|
graph.insert(ev.event_id().clone(), vec![]);
|
||||||
fake_event_map.insert(ev.event_id().clone(), ev.clone());
|
fake_event_map.insert(ev.event_id().clone(), ev.clone());
|
||||||
}
|
}
|
||||||
@ -164,34 +168,6 @@ pub fn do_check(
|
|||||||
prev_events,
|
prev_events,
|
||||||
);
|
);
|
||||||
|
|
||||||
// This can be used to sort of test this function
|
|
||||||
// match StateResolution::apply_event(
|
|
||||||
// &room_id(),
|
|
||||||
// &RoomVersionId::Version6,
|
|
||||||
// Arc::clone(&event),
|
|
||||||
// &state_after,
|
|
||||||
// Some(event_map.clone()),
|
|
||||||
// &store,
|
|
||||||
// ) {
|
|
||||||
// Ok(res) => {
|
|
||||||
// println!(
|
|
||||||
// "res contains: {} passed: {} for {}\n{:?}",
|
|
||||||
// state_after
|
|
||||||
// .get(&(event.kind, event.state_key()))
|
|
||||||
// .map(|id| id == &ev_id)
|
|
||||||
// .unwrap_or_default(),
|
|
||||||
// res,
|
|
||||||
// event.event_id.clone().as_str(),
|
|
||||||
// event
|
|
||||||
// .prev_event_ids()
|
|
||||||
// .iter()
|
|
||||||
// .map(|id| id.to_string())
|
|
||||||
// .collect::<Vec<_>>()
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// Err(e) => panic!("resolution for {} failed: {}", node, e),
|
|
||||||
// }
|
|
||||||
|
|
||||||
// we have to update our store, an actual user of this lib would
|
// we have to update our store, an actual user of this lib would
|
||||||
// be giving us state from a DB.
|
// be giving us state from a DB.
|
||||||
store.0.insert(ev_id.clone(), event.clone());
|
store.0.insert(ev_id.clone(), event.clone());
|
||||||
@ -316,39 +292,27 @@ pub fn to_init_pdu_event(
|
|||||||
format!("${}:foo", id)
|
format!("${}:foo", id)
|
||||||
};
|
};
|
||||||
|
|
||||||
let json = if let Some(state_key) = state_key {
|
let state_key = state_key.map(ToString::to_string);
|
||||||
json!({
|
Arc::new(StateEvent {
|
||||||
"auth_events": [],
|
event_id: EventId::try_from(id).unwrap(),
|
||||||
"prev_events": [],
|
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
|
||||||
"event_id": id,
|
room_id: room_id(),
|
||||||
"sender": sender,
|
sender,
|
||||||
"type": ev_type,
|
origin_server_ts: UNIX_EPOCH + Duration::from_secs(ts),
|
||||||
"state_key": state_key,
|
state_key,
|
||||||
"content": content,
|
kind: ev_type,
|
||||||
"origin_server_ts": ts,
|
content,
|
||||||
"room_id": room_id(),
|
redacts: None,
|
||||||
"origin": "foo",
|
unsigned: btreemap! {},
|
||||||
"depth": 0,
|
#[cfg(not(feature = "unstable-pre-spec"))]
|
||||||
"hashes": { "sha256": "hello" },
|
origin: "foo".into(),
|
||||||
"signatures": {},
|
auth_events: vec![],
|
||||||
})
|
prev_events: vec![],
|
||||||
} else {
|
depth: ruma::uint!(0),
|
||||||
json!({
|
hashes: EventHash { sha256: "".into() },
|
||||||
"auth_events": [],
|
signatures: btreemap! {},
|
||||||
"prev_events": [],
|
}),
|
||||||
"event_id": id,
|
})
|
||||||
"sender": sender,
|
|
||||||
"type": ev_type,
|
|
||||||
"content": content,
|
|
||||||
"origin_server_ts": ts,
|
|
||||||
"room_id": room_id(),
|
|
||||||
"origin": "foo",
|
|
||||||
"depth": 0,
|
|
||||||
"hashes": { "sha256": "hello" },
|
|
||||||
"signatures": {},
|
|
||||||
})
|
|
||||||
};
|
|
||||||
Arc::new(serde_json::from_value(json).unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_pdu_event<S>(
|
pub fn to_pdu_event<S>(
|
||||||
@ -385,39 +349,27 @@ where
|
|||||||
.map(event_id)
|
.map(event_id)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let json = if let Some(state_key) = state_key {
|
let state_key = state_key.map(ToString::to_string);
|
||||||
json!({
|
Arc::new(StateEvent {
|
||||||
"auth_events": auth_events,
|
event_id: EventId::try_from(id).unwrap(),
|
||||||
"prev_events": prev_events,
|
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
|
||||||
"event_id": id,
|
room_id: room_id(),
|
||||||
"sender": sender,
|
sender,
|
||||||
"type": ev_type,
|
origin_server_ts: UNIX_EPOCH + Duration::from_secs(ts),
|
||||||
"state_key": state_key,
|
state_key,
|
||||||
"content": content,
|
kind: ev_type,
|
||||||
"origin_server_ts": ts,
|
content,
|
||||||
"room_id": room_id(),
|
redacts: None,
|
||||||
"origin": "foo",
|
unsigned: btreemap! {},
|
||||||
"depth": 0,
|
#[cfg(not(feature = "unstable-pre-spec"))]
|
||||||
"hashes": { "sha256": "hello" },
|
origin: "foo".into(),
|
||||||
"signatures": {},
|
auth_events,
|
||||||
})
|
prev_events,
|
||||||
} else {
|
depth: ruma::uint!(0),
|
||||||
json!({
|
hashes: EventHash { sha256: "".into() },
|
||||||
"auth_events": auth_events,
|
signatures: btreemap! {},
|
||||||
"prev_events": prev_events,
|
}),
|
||||||
"event_id": id,
|
})
|
||||||
"sender": sender,
|
|
||||||
"type": ev_type,
|
|
||||||
"content": content,
|
|
||||||
"origin_server_ts": ts,
|
|
||||||
"room_id": room_id(),
|
|
||||||
"origin": "foo",
|
|
||||||
"depth": 0,
|
|
||||||
"hashes": { "sha256": "hello" },
|
|
||||||
"signatures": {},
|
|
||||||
})
|
|
||||||
};
|
|
||||||
Arc::new(serde_json::from_value(json).unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// all graphs start with these input events
|
// all graphs start with these input events
|
||||||
@ -522,17 +474,14 @@ pub mod event {
|
|||||||
|
|
||||||
use ruma::{
|
use ruma::{
|
||||||
events::{
|
events::{
|
||||||
from_raw_json_value,
|
|
||||||
pdu::{EventHash, Pdu},
|
pdu::{EventHash, Pdu},
|
||||||
room::member::{MemberEventContent, MembershipState},
|
room::member::{MemberEventContent, MembershipState},
|
||||||
EventDeHelper, EventType,
|
EventType,
|
||||||
},
|
},
|
||||||
serde::CanonicalJsonValue,
|
|
||||||
signatures::reference_hash,
|
|
||||||
EventId, RoomId, RoomVersionId, ServerName, UInt, UserId,
|
EventId, RoomId, RoomVersionId, ServerName, UInt, UserId,
|
||||||
};
|
};
|
||||||
use serde::{de, ser, Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{value::RawValue as RawJsonValue, Value as JsonValue};
|
use serde_json::Value as JsonValue;
|
||||||
|
|
||||||
use state_res::Event;
|
use state_res::Event;
|
||||||
|
|
||||||
@ -588,92 +537,10 @@ pub mod event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
struct EventIdHelper {
|
pub struct StateEvent {
|
||||||
event_id: EventId,
|
pub event_id: EventId,
|
||||||
}
|
#[serde(flatten)]
|
||||||
|
pub rest: Pdu,
|
||||||
fn event_id<E: de::Error>(json: &RawJsonValue) -> Result<EventId, E> {
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
Ok(match from_raw_json_value::<EventIdHelper, E>(&json) {
|
|
||||||
Ok(id) => id.event_id,
|
|
||||||
Err(_) => {
|
|
||||||
// panic!("NOT DURING TESTS");
|
|
||||||
EventId::try_from(format!(
|
|
||||||
"${}",
|
|
||||||
reference_hash(&from_raw_json_value(&json)?, &RoomVersionId::Version6)
|
|
||||||
.map_err(de::Error::custom)?,
|
|
||||||
))
|
|
||||||
.map_err(de::Error::custom)?
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: This no longer needs to be an enum now that PduStub is gone
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub enum StateEvent {
|
|
||||||
Full(EventId, Pdu),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for StateEvent {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: ser::Serializer,
|
|
||||||
{
|
|
||||||
use ser::Error;
|
|
||||||
use std::convert::TryInto;
|
|
||||||
|
|
||||||
match self {
|
|
||||||
Self::Full(id, ev) => {
|
|
||||||
// TODO: do we want to add the eventId when we
|
|
||||||
// serialize
|
|
||||||
let val: CanonicalJsonValue = serde_json::to_value(ev)
|
|
||||||
.map_err(S::Error::custom)?
|
|
||||||
.try_into()
|
|
||||||
.map_err(S::Error::custom)?;
|
|
||||||
|
|
||||||
match val {
|
|
||||||
CanonicalJsonValue::Object(mut obj) => {
|
|
||||||
obj.insert(
|
|
||||||
"event_id".into(),
|
|
||||||
ruma::serde::to_canonical_value(id).map_err(S::Error::custom)?,
|
|
||||||
);
|
|
||||||
obj.serialize(serializer)
|
|
||||||
}
|
|
||||||
_ => panic!("Pdu not an object"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de> de::Deserialize<'de> for StateEvent {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
||||||
where
|
|
||||||
D: de::Deserializer<'de>,
|
|
||||||
{
|
|
||||||
let json = Box::<RawJsonValue>::deserialize(deserializer)?;
|
|
||||||
let EventDeHelper {
|
|
||||||
room_id, unsigned, ..
|
|
||||||
} = from_raw_json_value(&json)?;
|
|
||||||
|
|
||||||
// TODO: do we even want to try for the existing ID
|
|
||||||
|
|
||||||
// Determine whether the event is a full or stub
|
|
||||||
// based on the fields present.
|
|
||||||
Ok(if room_id.is_some() {
|
|
||||||
match unsigned {
|
|
||||||
Some(unsigned) if unsigned.redacted_because.is_some() => {
|
|
||||||
panic!("TODO deal with redacted events")
|
|
||||||
}
|
|
||||||
_ => StateEvent::Full(
|
|
||||||
event_id(&json)?,
|
|
||||||
Pdu::RoomV3Pdu(from_raw_json_value(&json)?),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic!("Found stub event")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StateEvent {
|
impl StateEvent {
|
||||||
@ -681,209 +548,168 @@ pub mod event {
|
|||||||
id: EventId,
|
id: EventId,
|
||||||
json: serde_json::Value,
|
json: serde_json::Value,
|
||||||
) -> Result<Self, serde_json::Error> {
|
) -> Result<Self, serde_json::Error> {
|
||||||
Ok(Self::Full(
|
Ok(Self {
|
||||||
id,
|
event_id: id,
|
||||||
Pdu::RoomV3Pdu(serde_json::from_value(json)?),
|
rest: Pdu::RoomV3Pdu(serde_json::from_value(json)?),
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_id_canon_obj(
|
pub fn from_id_canon_obj(
|
||||||
id: EventId,
|
id: EventId,
|
||||||
json: ruma::serde::CanonicalJsonObject,
|
json: ruma::serde::CanonicalJsonObject,
|
||||||
) -> Result<Self, serde_json::Error> {
|
) -> Result<Self, serde_json::Error> {
|
||||||
Ok(Self::Full(
|
Ok(Self {
|
||||||
id,
|
event_id: id,
|
||||||
// TODO: this is unfortunate (from_value(to_value(json)))...
|
// TODO: this is unfortunate (from_value(to_value(json)))...
|
||||||
Pdu::RoomV3Pdu(serde_json::from_value(serde_json::to_value(json)?)?),
|
rest: Pdu::RoomV3Pdu(serde_json::from_value(serde_json::to_value(json)?)?),
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_power_event(&self) -> bool {
|
pub fn is_power_event(&self) -> bool {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, any_event) => match any_event {
|
Pdu::RoomV1Pdu(event) => match event.kind {
|
||||||
Pdu::RoomV1Pdu(event) => match event.kind {
|
EventType::RoomPowerLevels
|
||||||
EventType::RoomPowerLevels
|
| EventType::RoomJoinRules
|
||||||
| EventType::RoomJoinRules
|
| EventType::RoomCreate => event.state_key == Some("".into()),
|
||||||
| EventType::RoomCreate => event.state_key == Some("".into()),
|
EventType::RoomMember => {
|
||||||
EventType::RoomMember => {
|
// TODO fix clone
|
||||||
// TODO fix clone
|
if let Ok(content) =
|
||||||
if let Ok(content) =
|
serde_json::from_value::<MemberEventContent>(event.content.clone())
|
||||||
serde_json::from_value::<MemberEventContent>(event.content.clone())
|
{
|
||||||
|
if [MembershipState::Leave, MembershipState::Ban]
|
||||||
|
.contains(&content.membership)
|
||||||
{
|
{
|
||||||
if [MembershipState::Leave, MembershipState::Ban]
|
return event.sender.as_str()
|
||||||
.contains(&content.membership)
|
|
||||||
{
|
|
||||||
return event.sender.as_str()
|
|
||||||
// TODO is None here a failure
|
// TODO is None here a failure
|
||||||
!= event.state_key.as_deref().unwrap_or("NOT A STATE KEY");
|
!= event.state_key.as_deref().unwrap_or("NOT A STATE KEY");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
_ => false,
|
|
||||||
},
|
false
|
||||||
Pdu::RoomV3Pdu(event) => event.state_key == Some("".into()),
|
}
|
||||||
|
_ => false,
|
||||||
},
|
},
|
||||||
|
Pdu::RoomV3Pdu(event) => event.state_key == Some("".into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn deserialize_content<C: serde::de::DeserializeOwned>(
|
pub fn deserialize_content<C: serde::de::DeserializeOwned>(
|
||||||
&self,
|
&self,
|
||||||
) -> Result<C, serde_json::Error> {
|
) -> Result<C, serde_json::Error> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => serde_json::from_value(ev.content.clone()),
|
||||||
Pdu::RoomV1Pdu(ev) => serde_json::from_value(ev.content.clone()),
|
Pdu::RoomV3Pdu(ev) => serde_json::from_value(ev.content.clone()),
|
||||||
Pdu::RoomV3Pdu(ev) => serde_json::from_value(ev.content.clone()),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn origin_server_ts(&self) -> &SystemTime {
|
pub fn origin_server_ts(&self) -> &SystemTime {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.origin_server_ts,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.origin_server_ts,
|
Pdu::RoomV3Pdu(ev) => &ev.origin_server_ts,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.origin_server_ts,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn event_id(&self) -> &EventId {
|
pub fn event_id(&self) -> &EventId {
|
||||||
match self {
|
&self.event_id
|
||||||
// TODO; make this a &EventId
|
|
||||||
Self::Full(id, _) => id,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sender(&self) -> &UserId {
|
pub fn sender(&self) -> &UserId {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.sender,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.sender,
|
Pdu::RoomV3Pdu(ev) => &ev.sender,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.sender,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn redacts(&self) -> Option<&EventId> {
|
pub fn redacts(&self) -> Option<&EventId> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.redacts.as_ref(),
|
||||||
Pdu::RoomV1Pdu(ev) => ev.redacts.as_ref(),
|
Pdu::RoomV3Pdu(ev) => ev.redacts.as_ref(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.redacts.as_ref(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn room_id(&self) -> &RoomId {
|
pub fn room_id(&self) -> &RoomId {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.room_id,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.room_id,
|
Pdu::RoomV3Pdu(ev) => &ev.room_id,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.room_id,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn kind(&self) -> EventType {
|
pub fn kind(&self) -> EventType {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.kind.clone(),
|
||||||
Pdu::RoomV1Pdu(ev) => ev.kind.clone(),
|
Pdu::RoomV3Pdu(ev) => ev.kind.clone(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.kind.clone(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn state_key(&self) -> String {
|
pub fn state_key(&self) -> String {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.state_key.clone().unwrap(),
|
||||||
Pdu::RoomV1Pdu(ev) => ev.state_key.clone().unwrap(),
|
Pdu::RoomV3Pdu(ev) => ev.state_key.clone().unwrap(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.state_key.clone().unwrap(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "unstable-pre-spec"))]
|
#[cfg(not(feature = "unstable-pre-spec"))]
|
||||||
pub fn origin(&self) -> String {
|
pub fn origin(&self) -> String {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.origin.clone(),
|
||||||
Pdu::RoomV1Pdu(ev) => ev.origin.clone(),
|
Pdu::RoomV3Pdu(ev) => ev.origin.clone(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.origin.clone(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prev_event_ids(&self) -> Vec<EventId> {
|
pub fn prev_event_ids(&self) -> Vec<EventId> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.prev_events.iter().map(|(id, _)| id).cloned().collect(),
|
||||||
Pdu::RoomV1Pdu(ev) => {
|
Pdu::RoomV3Pdu(ev) => ev.prev_events.clone(),
|
||||||
ev.prev_events.iter().map(|(id, _)| id).cloned().collect()
|
|
||||||
}
|
|
||||||
Pdu::RoomV3Pdu(ev) => ev.prev_events.clone(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn auth_events(&self) -> Vec<EventId> {
|
pub fn auth_events(&self) -> Vec<EventId> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.auth_events.iter().map(|(id, _)| id).cloned().collect(),
|
||||||
Pdu::RoomV1Pdu(ev) => {
|
Pdu::RoomV3Pdu(ev) => ev.auth_events.to_vec(),
|
||||||
ev.auth_events.iter().map(|(id, _)| id).cloned().collect()
|
|
||||||
}
|
|
||||||
Pdu::RoomV3Pdu(ev) => ev.auth_events.to_vec(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn content(&self) -> serde_json::Value {
|
pub fn content(&self) -> serde_json::Value {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => ev.content.clone(),
|
||||||
Pdu::RoomV1Pdu(ev) => ev.content.clone(),
|
Pdu::RoomV3Pdu(ev) => ev.content.clone(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.content.clone(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unsigned(&self) -> &BTreeMap<String, serde_json::Value> {
|
pub fn unsigned(&self) -> &BTreeMap<String, serde_json::Value> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.unsigned,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.unsigned,
|
Pdu::RoomV3Pdu(ev) => &ev.unsigned,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.unsigned,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn signatures(
|
pub fn signatures(
|
||||||
&self,
|
&self,
|
||||||
) -> BTreeMap<Box<ServerName>, BTreeMap<ruma::ServerSigningKeyId, String>> {
|
) -> BTreeMap<Box<ServerName>, BTreeMap<ruma::ServerSigningKeyId, String>> {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(_) => maplit::btreemap! {},
|
||||||
Pdu::RoomV1Pdu(_) => maplit::btreemap! {},
|
Pdu::RoomV3Pdu(ev) => ev.signatures.clone(),
|
||||||
Pdu::RoomV3Pdu(ev) => ev.signatures.clone(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hashes(&self) -> &EventHash {
|
pub fn hashes(&self) -> &EventHash {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.hashes,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.hashes,
|
Pdu::RoomV3Pdu(ev) => &ev.hashes,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.hashes,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn depth(&self) -> &UInt {
|
pub fn depth(&self) -> &UInt {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => &ev.depth,
|
||||||
Pdu::RoomV1Pdu(ev) => &ev.depth,
|
Pdu::RoomV3Pdu(ev) => &ev.depth,
|
||||||
Pdu::RoomV3Pdu(ev) => &ev.depth,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_type_and_key(&self, ev_type: EventType, state_key: &str) -> bool {
|
pub fn is_type_and_key(&self, ev_type: EventType, state_key: &str) -> bool {
|
||||||
match self {
|
match &self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(ev) => {
|
||||||
Pdu::RoomV1Pdu(ev) => {
|
ev.kind == ev_type && ev.state_key.as_deref() == Some(state_key)
|
||||||
ev.kind == ev_type && ev.state_key.as_deref() == Some(state_key)
|
}
|
||||||
}
|
Pdu::RoomV3Pdu(ev) => {
|
||||||
Pdu::RoomV3Pdu(ev) => {
|
ev.kind == ev_type && ev.state_key.as_deref() == Some(state_key)
|
||||||
ev.kind == ev_type && ev.state_key.as_deref() == Some(state_key)
|
}
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -893,54 +719,10 @@ pub mod event {
|
|||||||
/// version 3 and above.
|
/// version 3 and above.
|
||||||
pub fn room_version(&self) -> RoomVersionId {
|
pub fn room_version(&self) -> RoomVersionId {
|
||||||
// TODO: We have to know the actual room version this is not sufficient
|
// TODO: We have to know the actual room version this is not sufficient
|
||||||
match self {
|
match self.rest {
|
||||||
Self::Full(_, ev) => match ev {
|
Pdu::RoomV1Pdu(_) => RoomVersionId::Version1,
|
||||||
Pdu::RoomV1Pdu(_) => RoomVersionId::Version1,
|
Pdu::RoomV3Pdu(_) => RoomVersionId::Version6,
|
||||||
Pdu::RoomV3Pdu(_) => RoomVersionId::Version6,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn deserialize_pdu() {
|
|
||||||
let non_canonical_json = r#"{"auth_events": ["$FEKmyWTamMqoL3zkEC3mVPg3qkcXcUShxxaq5BltsCE", "$Oc8MYrZ3-eM4yBbhlj8YkYYluF9KHFDKU5uDpO-Ewcc", "$3ImCSXY6bbWbZ5S2N6BMplHHlP7RkxWZCM9fMbdM2NY", "$8Lfs0rVCE9bHQrUztEF9kbsrT4zASnPEtpImZN4L2n8"], "content": {"membership": "join"}, "depth": 135, "hashes": {"sha256": "Q7OehFJaB32W3NINZKesQZH7+ba7xZVFuyCtuWQ2emk"}, "origin": "pc.koesters.xyz:59003", "origin_server_ts": 1599901756522, "prev_events": ["$Oc8MYrZ3-eM4yBbhlj8YkYYluF9KHFDKU5uDpO-Ewcc"], "prev_state": [], "room_id": "!eGNyCFvnKcpsnIZiEV:koesters.xyz", "sender": "@timo:pc.koesters.xyz:59003", "state_key": "@timo:pc.koesters.xyz:59003", "type": "m.room.member", "signatures": {"koesters.xyz": {"ed25519:a_wwQy": "bb8T5haywaEXKNxUUjeNBfjYi/Qe32R/dGliduIs3Ct913WGzXYLjWh7xHqapie7viHPzkDw/KYJacpAYKvMBA"}, "pc.koesters.xyz:59003": {"ed25519:key1": "/B3tpaMZKoLNITrup4fbFhbIMWixxEKM49nS4MiKOFfyJjDGuC5nWsurw0m2eYzrffhkF5qQQ8+RlFvkqwqkBw"}}, "unsigned": {"age": 30, "replaces_state": "$Oc8MYrZ3-eM4yBbhlj8YkYYluF9KHFDKU5uDpO-Ewcc", "prev_content": {"membership": "join"}, "prev_sender": "@timo:pc.koesters.xyz:59003"}}"#;
|
|
||||||
|
|
||||||
let pdu = serde_json::from_str::<StateEvent>(non_canonical_json).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
match &pdu {
|
|
||||||
StateEvent::Full(id, _) => id,
|
|
||||||
},
|
|
||||||
&ruma::event_id!("$Sfx_o8eLfo4idpTO8_IGrKSPKoRMC1CmQugVw9tu_MU")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[cfg_attr(not(feature = "unstable-pre-spec"), ignore)]
|
|
||||||
fn serialize_pdu() {
|
|
||||||
let non_canonical_json = r#"{"auth_events": ["$FEKmyWTamMqoL3zkEC3mVPg3qkcXcUShxxaq5BltsCE", "$Oc8MYrZ3-eM4yBbhlj8YkYYluF9KHFDKU5uDpO-Ewcc", "$3ImCSXY6bbWbZ5S2N6BMplHHlP7RkxWZCM9fMbdM2NY", "$8Lfs0rVCE9bHQrUztEF9kbsrT4zASnPEtpImZN4L2n8"], "content": {"membership": "join"}, "depth": 135, "hashes": {"sha256": "Q7OehFJaB32W3NINZKesQZH7+ba7xZVFuyCtuWQ2emk"}, "origin": "pc.koesters.xyz:59003", "origin_server_ts": 1599901756522, "prev_events": ["$Oc8MYrZ3-eM4yBbhlj8YkYYluF9KHFDKU5uDpO-Ewcc"], "prev_state": [], "room_id": "!eGNyCFvnKcpsnIZiEV:koesters.xyz", "sender": "@timo:pc.koesters.xyz:59003", "state_key": "@timo:pc.koesters.xyz:59003", "type": "m.room.member", "signatures": {"koesters.xyz": {"ed25519:a_wwQy": "bb8T5haywaEXKNxUUjeNBfjYi/Qe32R/dGliduIs3Ct913WGzXYLjWh7xHqapie7viHPzkDw/KYJacpAYKvMBA"}, "pc.koesters.xyz:59003": {"ed25519:key1": "/B3tpaMZKoLNITrup4fbFhbIMWixxEKM49nS4MiKOFfyJjDGuC5nWsurw0m2eYzrffhkF5qQQ8+RlFvkqwqkBw"}}, "unsigned": {"age": 30, "replaces_state": "$Oc8MYrZ3-eM4yBbhlj8YkYYluF9KHFDKU5uDpO-Ewcc", "prev_content": {"membership": "join"}, "prev_sender": "@timo:pc.koesters.xyz:59003"}}"#;
|
|
||||||
|
|
||||||
let pdu = serde_json::from_str::<StateEvent>(non_canonical_json).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
match &pdu {
|
|
||||||
StateEvent::Full(id, _) => id,
|
|
||||||
},
|
|
||||||
&ruma::event_id!("$Sfx_o8eLfo4idpTO8_IGrKSPKoRMC1CmQugVw9tu_MU")
|
|
||||||
);
|
|
||||||
|
|
||||||
// TODO: the `origin` field is left out, though it seems it should be part of the eventId hashing
|
|
||||||
// For testing we must serialize the PDU with the `event_id` field this is probably not correct for production
|
|
||||||
// although without them we get "Invalid bytes in DB" errors in conduit
|
|
||||||
assert_eq!(
|
|
||||||
ruma::serde::to_canonical_json_string(&pdu).unwrap(),
|
|
||||||
r#"{"auth_events":["$FEKmyWTamMqoL3zkEC3mVPg3qkcXcUShxxaq5BltsCE","$Oc8MYrZ3-eM4yBbhlj8YkYYluF9KHFDKU5uDpO-Ewcc","$3ImCSXY6bbWbZ5S2N6BMplHHlP7RkxWZCM9fMbdM2NY","$8Lfs0rVCE9bHQrUztEF9kbsrT4zASnPEtpImZN4L2n8"],"content":{"membership":"join"},"depth":135,"event_id":"$Sfx_o8eLfo4idpTO8_IGrKSPKoRMC1CmQugVw9tu_MU","hashes":{"sha256":"Q7OehFJaB32W3NINZKesQZH7+ba7xZVFuyCtuWQ2emk"},"origin_server_ts":1599901756522,"prev_events":["$Oc8MYrZ3-eM4yBbhlj8YkYYluF9KHFDKU5uDpO-Ewcc"],"room_id":"!eGNyCFvnKcpsnIZiEV:koesters.xyz","sender":"@timo:pc.koesters.xyz:59003","signatures":{"koesters.xyz":{"ed25519:a_wwQy":"bb8T5haywaEXKNxUUjeNBfjYi/Qe32R/dGliduIs3Ct913WGzXYLjWh7xHqapie7viHPzkDw/KYJacpAYKvMBA"},"pc.koesters.xyz:59003":{"ed25519:key1":"/B3tpaMZKoLNITrup4fbFhbIMWixxEKM49nS4MiKOFfyJjDGuC5nWsurw0m2eYzrffhkF5qQQ8+RlFvkqwqkBw"}},"state_key":"@timo:pc.koesters.xyz:59003","type":"m.room.member","unsigned":{"age":30,"prev_content":{"membership":"join"},"prev_sender":"@timo:pc.koesters.xyz:59003","replaces_state":"$Oc8MYrZ3-eM4yBbhlj8YkYYluF9KHFDKU5uDpO-Ewcc"}}"#,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user