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;

View File

@@ -90,8 +90,8 @@ async fn remote_invite(
room_version: room_version_id.clone(),
event: self
.services
.sending
.convert_to_outgoing_federation_event(pdu_json.clone())
.federation
.format_pdu_into(pdu_json.clone(), Some(&room_version_id))
.await,
invite_room_state: invite_room_state
.into_iter()

View File

@@ -253,8 +253,8 @@ pub async fn join_remote(
omit_members: false,
pdu: self
.services
.sending
.convert_to_outgoing_federation_event(join_event.clone())
.federation
.format_pdu_into(join_event.clone(), Some(&room_version_id))
.await,
};
@@ -742,8 +742,8 @@ pub async fn join_local(
omit_members: false,
pdu: self
.services
.sending
.convert_to_outgoing_federation_event(join_event.clone())
.federation
.format_pdu_into(join_event.clone(), Some(&room_version_id))
.await,
},
)

View File

@@ -329,8 +329,8 @@ pub async fn remote_leave(&self, user_id: &UserId, room_id: &RoomId) -> Result {
event_id,
pdu: self
.services
.sending
.convert_to_outgoing_federation_event(leave_event.clone())
.federation
.format_pdu_into(leave_event.clone(), Some(&room_version_id))
.await,
},
)

View File

@@ -16,8 +16,8 @@ use futures::{
stream::FuturesUnordered,
};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, MilliSecondsSinceUnixEpoch, OwnedRoomId,
OwnedServerName, OwnedUserId, RoomId, ServerName, UInt,
MilliSecondsSinceUnixEpoch, OwnedRoomId, OwnedServerName, OwnedUserId, RoomId, ServerName,
UInt,
api::{
appservice::event::push_events::v1::EphemeralData,
federation::transactions::{
@@ -37,10 +37,8 @@ use ruma::{
serde::Raw,
uint,
};
use serde_json::value::{RawValue as RawJsonValue, to_raw_value};
use tuwunel_core::{
Error, Event, Result, debug, err, error, implement, is_equal_to,
matrix::room_version,
Error, Event, Result, debug, err, error,
result::LogErr,
trace,
utils::{
@@ -883,7 +881,11 @@ impl Service {
.get_pdu_json_from_id(pdu_id)
.ok()
})
.wide_then(|pdu| self.convert_to_outgoing_federation_event(pdu))
.wide_then(|pdu| {
self.services
.federation
.format_pdu_into(pdu, None)
})
.collect()
.await;
@@ -937,64 +939,3 @@ impl Service {
}
}
}
/// This does not return a full `Pdu` it is only to satisfy ruma's types.
#[implement(Service)]
pub async fn convert_to_outgoing_federation_event(
&self,
mut pdu_json: CanonicalJsonObject,
) -> Box<RawJsonValue> {
self.strip_outgoing_federation_event(&mut pdu_json)
.await;
to_raw_value(&pdu_json).expect("CanonicalJson is valid serde_json::Value")
}
#[implement(Service)]
async fn strip_outgoing_federation_event(&self, pdu_json: &mut CanonicalJsonObject) {
if let Some(unsigned) = pdu_json
.get_mut("unsigned")
.and_then(|val| val.as_object_mut())
{
unsigned.remove("transaction_id");
}
let Some(room_id) = pdu_json
.get("room_id")
.and_then(CanonicalJsonValue::as_str)
.map(RoomId::parse)
.transpose()
.ok()
.flatten()
else {
return;
};
let Ok(room_rules) = self
.services
.state
.get_room_version(room_id)
.await
.and_then(|ref ver| room_version::rules(ver))
else {
pdu_json.remove("event_id");
return;
};
if !room_rules.event_format.require_event_id {
pdu_json.remove("event_id");
}
if !room_rules
.event_format
.require_room_create_room_id
{
if pdu_json
.get("type")
.and_then(CanonicalJsonValue::as_str)
.is_some_and(is_equal_to!("m.room.create"))
{
pdu_json.remove("room_id");
}
}
}