Various improvements

This commit is contained in:
Timo Kösters 2020-09-12 21:17:37 +02:00 committed by Devin Ragotzy
parent f587b88a60
commit ad4fb6420a
4 changed files with 21 additions and 66 deletions

View File

@ -22,15 +22,16 @@ maplit = "1.0.2"
thiserror = "1.0.20" thiserror = "1.0.20"
tracing-subscriber = "0.2.11" tracing-subscriber = "0.2.11"
[dependencies.ruma]
path = "../ruma/ruma"
features = ["client-api", "federation-api", "appservice-api"]
#[dependencies.ruma] #[dependencies.ruma]
#git = "https://github.com/ruma/ruma" #path = "../ruma/ruma"
#rev = "aff914050eb297bd82b8aafb12158c88a9e480e1"
#features = ["client-api", "federation-api", "appservice-api"] #features = ["client-api", "federation-api", "appservice-api"]
[dependencies.ruma]
git = "https://github.com/timokoesters/ruma"
branch = "timo-fed-fixes"
#rev = "aff914050eb297bd82b8aafb12158c88a9e480e1"
features = ["client-api", "federation-api", "appservice-api"]
[features] [features]
unstable-pre-spec = ["ruma/unstable-pre-spec"] unstable-pre-spec = ["ruma/unstable-pre-spec"]
@ -41,4 +42,3 @@ rand = "0.7.3"
[[bench]] [[bench]]
name = "state_res_bench" name = "state_res_bench"
harness = false harness = false

View File

@ -377,7 +377,7 @@ pub fn valid_membership_change(
.join_rule; .join_rule;
} }
if let Some(prev) = dbg!(prev_event) { if let Some(prev) = prev_event {
if prev.kind() == EventType::RoomCreate && prev.prev_event_ids().is_empty() { if prev.kind() == EventType::RoomCreate && prev.prev_event_ids().is_empty() {
return Ok(true); return Ok(true);
} }

View File

@ -109,7 +109,7 @@ impl StateResolution {
.unwrap(); .unwrap();
// update event_map to include the fetched events // update event_map to include the fetched events
event_map.extend(events.into_iter().map(|ev| (ev.event_id(), ev))); event_map.extend(events.into_iter().map(|ev| (ev.event_id().clone(), ev)));
// at this point our event_map == store there should be no missing events // at this point our event_map == store there should be no missing events
tracing::debug!("event map size: {}", event_map.len()); tracing::debug!("event map size: {}", event_map.len());
@ -337,7 +337,7 @@ impl StateResolution {
// This return value is the key used for sorting events, // This return value is the key used for sorting events,
// events are then sorted by power level, time, // events are then sorted by power level, time,
// and lexically by event_id. // and lexically by event_id.
(-*pl, *ev.origin_server_ts(), ev.event_id()) (-*pl, *ev.origin_server_ts(), ev.event_id().clone())
}) })
} }
@ -526,8 +526,8 @@ impl StateResolution {
tracing::debug!("event to check {:?}", event.event_id().as_str()); tracing::debug!("event to check {:?}", event.event_id().as_str());
let most_recent_prev_event = dbg!(event let most_recent_prev_event = event
.prev_event_ids()) .prev_event_ids()
.iter() .iter()
.filter_map(|id| StateResolution::get_or_load_event(room_id, id, event_map, store)) .filter_map(|id| StateResolution::get_or_load_event(room_id, id, event_map, store))
.next_back(); .next_back();

View File

@ -1,17 +1,15 @@
use std::{collections::BTreeMap, convert::TryFrom}; use std::collections::BTreeMap;
use js_int::UInt; use js_int::UInt;
use ruma::{ use ruma::{
events::{ events::{
from_raw_json_value,
pdu::{EventHash, Pdu, PduStub}, pdu::{EventHash, Pdu, PduStub},
room::member::{MemberEventContent, MembershipState}, room::member::{MemberEventContent, MembershipState},
EventDeHelper, EventType, EventType,
}, },
EventId, RoomId, RoomVersionId, ServerName, UserId, EventId, RoomId, RoomVersionId, ServerName, UserId,
}; };
use serde::{de, Serialize}; use serde::{Serialize, Deserialize};
use serde_json::value::RawValue as RawJsonValue;
use std::time::SystemTime; use std::time::SystemTime;
pub struct Requester<'a> { pub struct Requester<'a> {
@ -22,10 +20,11 @@ pub struct Requester<'a> {
pub sender: &'a UserId, pub sender: &'a UserId,
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum StateEvent { pub enum StateEvent {
Full(Pdu), Full(Pdu),
#[serde(skip_deserializing)]
Sync(PduStub), Sync(PduStub),
} }
@ -119,27 +118,13 @@ impl StateEvent {
}, },
} }
} }
pub fn event_id(&self) -> EventId { pub fn event_id(&self) -> &EventId {
match self { match self {
Self::Full(ev) => match ev { Self::Full(ev) => match ev {
Pdu::RoomV1Pdu(ev) => ev.event_id.clone(), Pdu::RoomV1Pdu(ev) => &ev.event_id,
Pdu::RoomV3Pdu(_) => EventId::try_from(&*format!( Pdu::RoomV3Pdu(ev) => ev.event_id.as_ref().expect("RoomV3Pdu did not have an event id"),
"${}",
ruma::signatures::reference_hash(
&serde_json::to_value(&ev).expect("event is valid, we just created it")
)
.expect("ruma can calculate reference hashes")
))
.expect("ruma's reference hashes are valid event ids"),
}, },
Self::Sync(ev) => EventId::try_from(&*format!( Self::Sync(_ev) => panic!("Stubs don't have an event id"),
"${}",
ruma::signatures::reference_hash(
&serde_json::to_value(&ev).expect("event is valid, we just created it")
)
.expect("ruma can calculate reference hashes")
))
.expect("ruma's reference hashes are valid event ids"),
} }
} }
@ -352,33 +337,3 @@ impl StateEvent {
} }
} }
} }
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)?;
// Determine whether the event is a full or sync
// based on the fields present.
if room_id.is_some() {
Ok(match unsigned {
Some(unsigned) if unsigned.redacted_because.is_some() => {
panic!("TODO deal with redacted events")
}
_ => StateEvent::Full(Pdu::RoomV1Pdu(from_raw_json_value(&json)?)),
})
} else {
Ok(match unsigned {
Some(unsigned) if unsigned.redacted_because.is_some() => {
panic!("TODO deal with redacted events")
}
_ => StateEvent::Sync(from_raw_json_value(&json)?),
})
}
}
}