update ruma appservice Registration type MR

from https://gitlab.com/famedly/conduit/-/merge_requests/583

and fixed panic from blocking async call in timeline/mod.rs

Co-authored-by: strawberry <strawberry@puppygock.gay>
Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
Matthias Ahouansou 2024-03-08 10:50:52 -05:00 committed by June
parent 019a82850d
commit 5ab76a1332
10 changed files with 156 additions and 62 deletions

View file

@ -1,14 +1,16 @@
use std::{collections::HashSet, sync::Arc};
use regex::Regex;
use ruma::{
api::appservice::Registration,
events::{AnyStrippedStateEvent, AnySyncStateEvent},
serde::Raw,
OwnedRoomId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId,
};
use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
use crate::{
database::KeyValueDatabase,
service::{self, appservice::RegistrationInfo},
services, utils, Error, Result,
};
type StrippedStateEventIter<'a> = Box<dyn Iterator<Item = Result<(OwnedRoomId, Vec<Raw<AnyStrippedStateEvent>>)>> + 'a>;
@ -160,19 +162,20 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
}
#[tracing::instrument(skip(self, room_id, appservice))]
fn appservice_in_room(&self, room_id: &RoomId, appservice: &(String, Registration)) -> Result<bool> {
let maybe =
self.appservice_in_room_cache.read().unwrap().get(room_id).and_then(|map| map.get(&appservice.0)).copied();
fn appservice_in_room(&self, room_id: &RoomId, appservice: &RegistrationInfo) -> Result<bool> {
let maybe = self
.appservice_in_room_cache
.read()
.unwrap()
.get(room_id)
.and_then(|map| map.get(&appservice.registration.id))
.copied();
if let Some(b) = maybe {
Ok(b)
} else {
let namespaces = &appservice.1.namespaces;
let users =
namespaces.users.iter().filter_map(|users| Regex::new(users.regex.as_str()).ok()).collect::<Vec<_>>();
let bridge_user_id = UserId::parse_with_server_name(
appservice.1.sender_localpart.as_str(),
appservice.registration.sender_localpart.as_str(),
services().globals.server_name(),
)
.ok();
@ -180,14 +183,14 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
let in_room = bridge_user_id.map_or(false, |id| self.is_joined(&id, room_id).unwrap_or(false))
|| self
.room_members(room_id)
.any(|userid| userid.map_or(false, |userid| users.iter().any(|r| r.is_match(userid.as_str()))));
.any(|userid| userid.map_or(false, |userid| appservice.users.is_match(userid.as_str())));
self.appservice_in_room_cache
.write()
.unwrap()
.entry(room_id.to_owned())
.or_default()
.insert(appservice.0.clone(), in_room);
.insert(appservice.registration.id.clone(), in_room);
Ok(in_room)
}

View file

@ -967,6 +967,14 @@ impl KeyValueDatabase {
);
}
// Inserting registraions into cache
for appservice in services().appservice.all()? {
services().appservice.registration_info.write().await.insert(
appservice.0,
appservice.1.try_into().expect("Should be validated on registration"),
);
}
services().admin.start_handler();
// Set emergency access for the conduit user