Update state-res crate

next
Devin Ragotzy 2020-08-27 16:10:20 -04:00
parent f46c2d1eec
commit 3b40f3d60e
2 changed files with 44 additions and 31 deletions

2
Cargo.lock generated
View File

@ -2070,7 +2070,7 @@ checksum = "7345c971d1ef21ffdbd103a75990a15eb03604fc8b8852ca8cb418ee1a099028"
[[package]] [[package]]
name = "state-res" name = "state-res"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/ruma/state-res?branch=spec-comp#17958665f6592af3ef478024fd1d75c384a30e7f" source = "git+https://github.com/ruma/state-res?branch=spec-comp#394d26744a6586ccdc01838964bb27dab289eee5"
dependencies = [ dependencies = [
"itertools", "itertools",
"js_int", "js_int",

View File

@ -19,13 +19,12 @@ use ruma::{
EventId, Raw, RoomAliasId, RoomId, UserId, EventId, Raw, RoomAliasId, RoomId, UserId,
}; };
use sled::IVec; use sled::IVec;
use state_res::{event_auth, Requester, StateEvent, StateMap, StateStore}; use state_res::{event_auth, Error as StateError, Requester, StateEvent, StateMap, StateStore};
use std::{ use std::{
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
mem, mem,
result::Result as StdResult,
}; };
/// The unique identifier of each state group. /// The unique identifier of each state group.
@ -67,28 +66,32 @@ pub struct Rooms {
} }
impl StateStore for Rooms { impl StateStore for Rooms {
fn get_event(&self, room_id: &RoomId, event_id: &EventId) -> StdResult<StateEvent, String> { fn get_event(&self, room_id: &RoomId, event_id: &EventId) -> state_res::Result<StateEvent> {
let pid = self let pid = self
.eventid_pduid .eventid_pduid
.get(event_id.as_bytes()) .get(event_id.as_bytes())
.map_err(|e| e.to_string())? .map_err(StateError::custom)?
.ok_or_else(|| "PDU via room_id and event_id not found in the db.".to_owned())?; .ok_or_else(|| {
StateError::NotFound("PDU via room_id and event_id not found in the db.".into())
})?;
serde_json::from_slice( serde_json::from_slice(
&self &self
.pduid_pdu .pduid_pdu
.get(pid) .get(pid)
.map_err(|e| e.to_string())? .map_err(StateError::custom)?
.ok_or_else(|| "PDU via pduid not found in db.".to_owned())?, .ok_or_else(|| StateError::NotFound("PDU via pduid not found in db.".into()))?,
) )
.map_err(|e| e.to_string()) .map_err(Into::into)
.and_then(|pdu: StateEvent| { .and_then(|pdu: StateEvent| {
// conduit's PDU's always contain a room_id but some // conduit's PDU's always contain a room_id but some
// of ruma's do not so this must be an Option // of ruma's do not so this must be an Option
if pdu.room_id() == Some(room_id) { if pdu.room_id() == Some(room_id) {
Ok(pdu) Ok(pdu)
} else { } else {
Err("Found PDU for incorrect room in db.".into()) Err(StateError::NotFound(
"Found PDU for incorrect room in db.".into(),
))
} }
}) })
} }
@ -732,7 +735,15 @@ impl Rooms {
// Don't allow encryption events when it's disabled // Don't allow encryption events when it's disabled
!globals.encryption_disabled() !globals.encryption_disabled()
} }
EventType::RoomMember => event_auth::is_membership_change_allowed( EventType::RoomMember => {
let prev_event = self
.get_pdu(prev_events.iter().next().ok_or(Error::BadRequest(
ErrorKind::Unknown,
"Membership can't be the first event",
))?)?
.map(|pdu| pdu.convert_for_state_res())
.transpose()?;
event_auth::valid_membership_change(
// TODO this is a bit of a hack but not sure how to have a type // TODO this is a bit of a hack but not sure how to have a type
// declared in `state_res` crate easily convert to/from conduit::PduEvent // declared in `state_res` crate easily convert to/from conduit::PduEvent
Requester { Requester {
@ -742,6 +753,7 @@ impl Rooms {
state_key: Some(state_key.to_owned()), state_key: Some(state_key.to_owned()),
sender: &sender, sender: &sender,
}, },
prev_event.as_ref(),
&auth_events &auth_events
.iter() .iter()
.map(|((ty, key), pdu)| { .map(|((ty, key), pdu)| {
@ -752,7 +764,8 @@ impl Rooms {
.map_err(|e| { .map_err(|e| {
log::error!("{}", e); log::error!("{}", e);
Error::Conflict("Found incoming PDU with invalid data.") Error::Conflict("Found incoming PDU with invalid data.")
})?, })?
}
EventType::RoomCreate => prev_events.is_empty(), EventType::RoomCreate => prev_events.is_empty(),
// Not allow any of the following events if the sender is not joined. // Not allow any of the following events if the sender is not joined.
_ if sender_membership != member::MembershipState::Join => false, _ if sender_membership != member::MembershipState::Join => false,