improvement: don't send pdus to appservices if it isn't interested

TODO: we need to send pdus if a user of the appservice is in the room
but not the appservice user itself
next
Timo Kösters 2020-12-23 19:41:54 +01:00
parent fb9bd34696
commit 2cf6fd57b7
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
4 changed files with 97 additions and 4 deletions

37
Cargo.lock generated
View File

@ -21,6 +21,15 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
[[package]]
name = "aho-corasick"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "arrayref" name = "arrayref"
version = "0.3.6" version = "0.3.6"
@ -190,6 +199,7 @@ dependencies = [
"js_int", "js_int",
"log", "log",
"rand", "rand",
"regex",
"reqwest", "reqwest",
"ring", "ring",
"rocket", "rocket",
@ -1418,6 +1428,24 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "regex"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"thread_local",
]
[[package]]
name = "regex-syntax"
version = "0.6.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
[[package]] [[package]]
name = "remove_dir_all" name = "remove_dir_all"
version = "0.5.3" version = "0.5.3"
@ -2131,6 +2159,15 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "thread_local"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
dependencies = [
"lazy_static",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.2.23" version = "0.2.23"

View File

@ -61,6 +61,8 @@ base64 = "0.13.0"
ring = "0.16.19" ring = "0.16.19"
# Used when querying the SRV record of other servers # Used when querying the SRV record of other servers
trust-dns-resolver = "0.19.6" trust-dns-resolver = "0.19.6"
# Used to find matching events for appservices
regex = "1.4.2"
[features] [features]
default = ["conduit_bin"] default = ["conduit_bin"]

View File

@ -4,6 +4,7 @@ pub use edus::RoomEdus;
use crate::{pdu::PduBuilder, utils, Error, PduEvent, Result}; use crate::{pdu::PduBuilder, utils, Error, PduEvent, Result};
use log::error; use log::error;
use regex::Regex;
use ring::digest; use ring::digest;
use ruma::{ use ruma::{
api::client::error::ErrorKind, api::client::error::ErrorKind,
@ -949,7 +950,63 @@ impl Rooms {
} }
for appservice in appservice.iter_all().filter_map(|r| r.ok()) { for appservice in appservice.iter_all().filter_map(|r| r.ok()) {
sending.send_pdu_appservice(&appservice.0, &pdu_id)?; if let Some(namespaces) = appservice.1.get("namespaces") {
let users = namespaces
.get("users")
.and_then(|users| users.as_sequence())
.map_or_else(
|| Vec::new(),
|users| {
users
.iter()
.map(|users| {
users
.get("regex")
.and_then(|regex| regex.as_str())
.and_then(|regex| Regex::new(regex).ok())
})
.filter_map(|o| o)
.collect::<Vec<_>>()
},
);
let aliases = namespaces
.get("aliases")
.and_then(|users| users.get("regex"))
.and_then(|regex| regex.as_str())
.and_then(|regex| Regex::new(regex).ok());
let rooms = namespaces
.get("rooms")
.and_then(|rooms| rooms.as_sequence());
let room_aliases = self.room_aliases(&room_id);
let bridge_user_id = appservice
.1
.get("sender_localpart")
.and_then(|string| string.as_str())
.and_then(|string| {
UserId::parse_with_server_name(string, globals.server_name()).ok()
});
if bridge_user_id.map_or(false, |bridge_user_id| {
self.is_joined(&bridge_user_id, room_id).unwrap_or(false)
}) || users.iter().any(|users| {
dbg!(
users.is_match(pdu.sender.as_str())
|| pdu.kind == EventType::RoomMember
&& pdu.state_key.as_ref().map_or(false, |state_key| dbg!(
users.is_match(dbg!(&state_key))
))
)
}) || aliases.map_or(false, |aliases| {
room_aliases
.filter_map(|r| r.ok())
.any(|room_alias| aliases.is_match(room_alias.as_str()))
}) || rooms.map_or(false, |rooms| rooms.contains(&room_id.as_str().into()))
{
sending.send_pdu_appservice(&appservice.0, &pdu_id)?;
}
}
} }
Ok(pdu.event_id) Ok(pdu.event_id)

View File

@ -28,9 +28,6 @@ use std::{
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
pub async fn send_request<T: OutgoingRequest>( pub async fn send_request<T: OutgoingRequest>(
globals: &crate::database::globals::Globals, globals: &crate::database::globals::Globals,
destination: Box<ServerName>, destination: Box<ServerName>,