From c5508bba58d0305472b6f69154a4f6ef8ea5e624 Mon Sep 17 00:00:00 2001 From: dasha_uwu Date: Mon, 8 Dec 2025 05:43:48 +0500 Subject: [PATCH] Fix appservices not receiving membership events to sender_localpart Simplify sending PDUs to appservices --- src/service/appservice/append.rs | 96 +++++++++++++++----------------- 1 file changed, 44 insertions(+), 52 deletions(-) diff --git a/src/service/appservice/append.rs b/src/service/appservice/append.rs index 980ef28f..5ddf5ead 100644 --- a/src/service/appservice/append.rs +++ b/src/service/appservice/append.rs @@ -8,7 +8,7 @@ use tuwunel_core::{ utils::ReadyExt, }; -use super::{NamespaceRegex, RegistrationInfo}; +use super::RegistrationInfo; /// Called by timeline::append() after accepting new PDU. #[implement(super::Service)] @@ -43,57 +43,7 @@ async fn append_pdu_to( pdu_id: RawPduId, pdu: &Pdu, ) -> Result { - if self - .services - .state_cache - .appservice_in_room(pdu.room_id(), appservice) - .await - { - self.services - .sending - .send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?; - - return Ok(()); - } - - // If the RoomMember event has a non-empty state_key, it is targeted at someone. - // If it is our appservice user, we send this PDU to it. - if *pdu.kind() == TimelineEventType::RoomMember { - if let Some(state_key_uid) = &pdu - .state_key - .as_ref() - .and_then(|state_key| UserId::parse(state_key.as_str()).ok()) - { - let appservice_uid = appservice.registration.sender_localpart.as_str(); - if state_key_uid == &appservice_uid { - self.services - .sending - .send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?; - - return Ok(()); - } - } - } - - let matching_users = |users: &NamespaceRegex| { - appservice.users.is_match(pdu.sender().as_str()) - || *pdu.kind() == TimelineEventType::RoomMember - && pdu - .state_key - .as_ref() - .is_some_and(|state_key| users.is_match(state_key)) - }; - let matching_aliases = |aliases: NamespaceRegex| { - self.services - .alias - .local_aliases_for_room(pdu.room_id()) - .ready_any(move |room_alias| aliases.is_match(room_alias.as_str())) - }; - - if matching_aliases(appservice.aliases.clone()).await - || appservice.rooms.is_match(pdu.room_id().as_str()) - || matching_users(&appservice.users) - { + if self.should_append_to(appservice, pdu).await { self.services .sending .send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?; @@ -101,3 +51,45 @@ async fn append_pdu_to( Ok(()) } + +#[implement(super::Service)] +async fn should_append_to(&self, appservice: &RegistrationInfo, pdu: &Pdu) -> bool { + if self + .services + .state_cache + .appservice_in_room(pdu.room_id(), appservice) + .await + { + return true; + } + + if appservice.is_user_match(pdu.sender()) { + return true; + } + + if *pdu.kind() == TimelineEventType::RoomMember + && pdu + .state_key + .as_ref() + .and_then(|state_key| UserId::parse(state_key.as_str()).ok()) + .is_some_and(|user_id| appservice.is_user_match(user_id)) + { + return true; + } + + if self + .services + .alias + .local_aliases_for_room(pdu.room_id()) + .ready_any(|room_alias| appservice.aliases.is_match(room_alias.as_str())) + .await + { + return true; + } + + if appservice.rooms.is_match(pdu.room_id().as_str()) { + return true; + } + + false +}