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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user