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]]
name = "state-res"
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 = [
"itertools",
"js_int",

View File

@ -19,13 +19,12 @@ use ruma::{
EventId, Raw, RoomAliasId, RoomId, UserId,
};
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::{
collections::{BTreeMap, HashMap},
convert::{TryFrom, TryInto},
mem,
result::Result as StdResult,
};
/// The unique identifier of each state group.
@ -67,28 +66,32 @@ pub struct 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
.eventid_pduid
.get(event_id.as_bytes())
.map_err(|e| e.to_string())?
.ok_or_else(|| "PDU via room_id and event_id not found in the db.".to_owned())?;
.map_err(StateError::custom)?
.ok_or_else(|| {
StateError::NotFound("PDU via room_id and event_id not found in the db.".into())
})?;
serde_json::from_slice(
&self
.pduid_pdu
.get(pid)
.map_err(|e| e.to_string())?
.ok_or_else(|| "PDU via pduid not found in db.".to_owned())?,
.map_err(StateError::custom)?
.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| {
// conduit's PDU's always contain a room_id but some
// of ruma's do not so this must be an Option
if pdu.room_id() == Some(room_id) {
Ok(pdu)
} 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
!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
// declared in `state_res` crate easily convert to/from conduit::PduEvent
Requester {
@ -742,6 +753,7 @@ impl Rooms {
state_key: Some(state_key.to_owned()),
sender: &sender,
},
prev_event.as_ref(),
&auth_events
.iter()
.map(|((ty, key), pdu)| {
@ -752,7 +764,8 @@ impl Rooms {
.map_err(|e| {
log::error!("{}", e);
Error::Conflict("Found incoming PDU with invalid data.")
})?,
})?
}
EventType::RoomCreate => prev_events.is_empty(),
// Not allow any of the following events if the sender is not joined.
_ if sender_membership != member::MembershipState::Join => false,