Merge branch 'initial_state_keys' into 'master'

Implement From<AnyInitialStateEvent> on PduBuilder

Closes #116

See merge request famedly/conduit!136
next
Timo Kösters 2021-07-21 10:03:08 +00:00
commit cae2b200cc
2 changed files with 20 additions and 7 deletions

View File

@ -233,10 +233,9 @@ pub async fn create_room_route(
// 5. Events listed in initial_state // 5. Events listed in initial_state
for event in &body.initial_state { for event in &body.initial_state {
let pdu_builder = serde_json::from_str::<PduBuilder>( let pdu_builder = PduBuilder::from(event.deserialize().map_err(|_| {
&serde_json::to_string(&event).expect("AnyInitialStateEvent::to_string always works"), Error::BadRequest(ErrorKind::InvalidParam, "Invalid initial state event.")
) })?);
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid initial state event."))?;
// Silently skip encryption events if they are not allowed // Silently skip encryption events if they are not allowed
if pdu_builder.event_type == EventType::RoomEncryption && !db.globals.allow_encryption() { if pdu_builder.event_type == EventType::RoomEncryption && !db.globals.allow_encryption() {

View File

@ -2,9 +2,9 @@ use crate::Error;
use log::error; use log::error;
use ruma::{ use ruma::{
events::{ events::{
pdu::EventHash, room::member::MemberEventContent, AnyEphemeralRoomEvent, AnyRoomEvent, pdu::EventHash, room::member::MemberEventContent, AnyEphemeralRoomEvent,
AnyStateEvent, AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent, EventType, AnyInitialStateEvent, AnyRoomEvent, AnyStateEvent, AnyStrippedStateEvent, AnySyncRoomEvent,
StateEvent, AnySyncStateEvent, EventType, StateEvent,
}, },
serde::{CanonicalJsonObject, CanonicalJsonValue, Raw}, serde::{CanonicalJsonObject, CanonicalJsonValue, Raw},
state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName, state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName,
@ -346,3 +346,17 @@ pub struct PduBuilder {
pub state_key: Option<String>, pub state_key: Option<String>,
pub redacts: Option<EventId>, pub redacts: Option<EventId>,
} }
/// Direct conversion prevents loss of the empty `state_key` that ruma requires.
impl From<AnyInitialStateEvent> for PduBuilder {
fn from(event: AnyInitialStateEvent) -> Self {
Self {
event_type: EventType::from(event.event_type()),
content: serde_json::value::to_value(event.content())
.expect("AnyStateEventContent came from JSON and can thus turn back into JSON."),
unsigned: None,
state_key: Some(event.state_key().to_owned()),
redacts: None,
}
}
}