Add room_version argument and reorg convert_outgoing_federation_event.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-08-27 06:22:49 +00:00
committed by dasha_uwu
parent 0aea56f02a
commit ce6212b161
16 changed files with 122 additions and 100 deletions

View File

@@ -0,0 +1,44 @@
use futures::future::OptionFuture;
use ruma::{CanonicalJsonObject, CanonicalJsonValue, RoomId, RoomVersionId};
use serde_json::value::{RawValue as RawJsonValue, to_raw_value};
use tuwunel_core::{
implement,
matrix::pdu,
utils::{future::TryExtExt, result::FlatOk},
};
/// This does not return a full `Pdu` it is only to satisfy ruma's types.
#[implement(super::Service)]
pub async fn format_pdu_into(
&self,
mut pdu_json: CanonicalJsonObject,
room_version: Option<&RoomVersionId>,
) -> Box<RawJsonValue> {
let room_id = pdu_json
.get("room_id")
.and_then(CanonicalJsonValue::as_str)
.map(RoomId::parse)
.flat_ok();
let query_room_version: OptionFuture<_> = room_id
.and_then(|room_id| {
room_version
.is_none()
.then(|| self.services.state.get_room_version(room_id))
.map(TryExtExt::ok)
})
.into();
if let Some(room_version) = query_room_version
.await
.flatten()
.as_ref()
.or(room_version)
{
pdu_json = pdu::format::into_outgoing_federation(pdu_json, room_version);
} else {
pdu_json.remove("event_id");
}
to_raw_value(&pdu_json).expect("CanonicalJson is valid serde_json::Value")
}

View File

@@ -1,4 +1,5 @@
mod execute;
mod format;
use std::sync::Arc;