Support v1/v2 prev_events/auth_events outgoing federation format. (#12)
Support v1/v2 prev_events/auth_events when handling outlier pdu. (#12) Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
use ruma::{CanonicalJsonObject, CanonicalJsonValue, RoomVersionId};
|
||||
use ruma::{
|
||||
CanonicalJsonObject, CanonicalJsonValue, EventId, RoomVersionId,
|
||||
room_version_rules::{EventsReferenceFormatVersion, RoomVersionRules},
|
||||
};
|
||||
|
||||
use crate::{is_equal_to, matrix::room_version};
|
||||
use crate::{
|
||||
Result, err, extract_variant, is_equal_to,
|
||||
matrix::{PduEvent, room_version},
|
||||
};
|
||||
|
||||
pub fn into_outgoing_federation(
|
||||
mut pdu_json: CanonicalJsonObject,
|
||||
@@ -35,5 +41,67 @@ pub fn into_outgoing_federation(
|
||||
}
|
||||
}
|
||||
|
||||
if matches!(room_rules.events_reference_format, EventsReferenceFormatVersion::V1) {
|
||||
if let Some(value) = pdu_json.get_mut("auth_events") {
|
||||
mutate_outgoing_reference_format(value);
|
||||
}
|
||||
if let Some(value) = pdu_json.get_mut("prev_events") {
|
||||
mutate_outgoing_reference_format(value);
|
||||
}
|
||||
}
|
||||
|
||||
pdu_json
|
||||
}
|
||||
|
||||
fn mutate_outgoing_reference_format(value: &mut CanonicalJsonValue) {
|
||||
value
|
||||
.as_array_mut()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.for_each(|value| {
|
||||
if let Some(event_id) = value.as_str().map(ToOwned::to_owned) {
|
||||
*value = CanonicalJsonValue::Array(vec![
|
||||
CanonicalJsonValue::String(event_id),
|
||||
CanonicalJsonValue::Object([(String::new(), "".into())].into()),
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn from_incoming_federation(
|
||||
event_id: &EventId,
|
||||
pdu_json: &mut CanonicalJsonObject,
|
||||
room_rules: &RoomVersionRules,
|
||||
) -> Result<PduEvent> {
|
||||
if matches!(room_rules.events_reference_format, EventsReferenceFormatVersion::V1) {
|
||||
if let Some(value) = pdu_json.get_mut("auth_events") {
|
||||
mutate_incoming_reference_format(value);
|
||||
}
|
||||
if let Some(value) = pdu_json.get_mut("prev_events") {
|
||||
mutate_incoming_reference_format(value);
|
||||
}
|
||||
}
|
||||
|
||||
pdu_json.insert("event_id".to_owned(), CanonicalJsonValue::String(event_id.into()));
|
||||
|
||||
serde_json::from_value::<PduEvent>(serde_json::to_value(&pdu_json)?)
|
||||
.map_err(|e| err!(Request(BadJson(debug_warn!("Event is not a valid PDU: {e}")))))
|
||||
}
|
||||
|
||||
fn mutate_incoming_reference_format(value: &mut CanonicalJsonValue) {
|
||||
value
|
||||
.as_array_mut()
|
||||
.into_iter()
|
||||
.flat_map(|vec| vec.iter_mut())
|
||||
.for_each(|value| {
|
||||
let event_id = value
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.find_map(|vec| vec.first())
|
||||
.and_then(|val| extract_variant!(val, CanonicalJsonValue::String))
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
|
||||
*value = CanonicalJsonValue::String(event_id);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use futures::{StreamExt, TryFutureExt};
|
||||
use ruma::{
|
||||
CanonicalJsonObject, CanonicalJsonValue, EventId, RoomId, RoomVersionId, ServerName,
|
||||
events::TimelineEventType,
|
||||
CanonicalJsonObject, EventId, RoomId, RoomVersionId, ServerName, events::TimelineEventType,
|
||||
};
|
||||
use tuwunel_core::{
|
||||
Err, Result, debug, debug_info, err, implement,
|
||||
matrix::{Event, PduEvent, event::TypeExt, room_version},
|
||||
pdu::format::from_incoming_federation,
|
||||
ref_at, state_res, trace,
|
||||
utils::{future::TryExtExt, stream::IterStream},
|
||||
warn,
|
||||
@@ -66,12 +66,11 @@ pub(super) async fn handle_outlier_pdu(
|
||||
},
|
||||
};
|
||||
|
||||
// Now that we have checked the signature and hashes we can add the eventID and
|
||||
// convert to our PduEvent type
|
||||
pdu_json.insert("event_id".to_owned(), CanonicalJsonValue::String(event_id.to_string()));
|
||||
let room_rules = room_version::rules(room_version)?;
|
||||
|
||||
let event = serde_json::from_value::<PduEvent>(serde_json::to_value(&pdu_json)?)
|
||||
.map_err(|e| err!(Request(BadJson(debug_warn!("Event is not a valid PDU: {e}")))))?;
|
||||
// Now that we have checked the signature and hashes we can make mutations and
|
||||
// convert to our PduEvent type.
|
||||
let event = from_incoming_federation(event_id, &mut pdu_json, &room_rules)?;
|
||||
|
||||
check_room_id(room_id, &event)?;
|
||||
|
||||
@@ -89,7 +88,6 @@ pub(super) async fn handle_outlier_pdu(
|
||||
// auth events
|
||||
debug!("Checking based on auth events");
|
||||
|
||||
let room_rules = room_version::rules(room_version)?;
|
||||
let is_hydra = !room_rules
|
||||
.event_format
|
||||
.allow_room_create_in_auth_events;
|
||||
|
||||
Reference in New Issue
Block a user