revamp appservice registration to ruma's Registration type

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

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
Matthias Ahouansou 2024-02-07 17:48:01 -05:00 committed by June
parent 0e9eb22ee7
commit 784d307425
13 changed files with 203 additions and 208 deletions

View file

@ -1,10 +1,11 @@
use ruma::api::appservice::Registration;
use crate::{database::KeyValueDatabase, service, utils, Error, Result};
impl service::appservice::Data for KeyValueDatabase {
/// Registers an appservice and returns the ID to the caller
fn register_appservice(&self, yaml: serde_yaml::Value) -> Result<String> {
// TODO: Rumaify
let id = yaml.get("id").unwrap().as_str().unwrap();
fn register_appservice(&self, yaml: Registration) -> Result<String> {
let id = yaml.id.as_str();
self.id_appserviceregistrations.insert(
id.as_bytes(),
serde_yaml::to_string(&yaml).unwrap().as_bytes(),
@ -32,7 +33,7 @@ impl service::appservice::Data for KeyValueDatabase {
Ok(())
}
fn get_registration(&self, id: &str) -> Result<Option<serde_yaml::Value>> {
fn get_registration(&self, id: &str) -> Result<Option<Registration>> {
self.cached_registrations
.read()
.unwrap()
@ -64,7 +65,7 @@ impl service::appservice::Data for KeyValueDatabase {
)))
}
fn all(&self) -> Result<Vec<(String, serde_yaml::Value)>> {
fn all(&self) -> Result<Vec<(String, Registration)>> {
self.iter_ids()?
.filter_map(|id| id.ok())
.map(move |id| {

View file

@ -2,6 +2,7 @@ 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,
@ -193,7 +194,7 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
fn appservice_in_room(
&self,
room_id: &RoomId,
appservice: &(String, serde_yaml::Value),
appservice: &(String, Registration),
) -> Result<bool> {
let maybe = self
.appservice_in_room_cache
@ -205,24 +206,19 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
if let Some(b) = maybe {
Ok(b)
} else if let Some(namespaces) = appservice.1.get("namespaces") {
} else {
let namespaces = &appservice.1.namespaces;
let users = namespaces
.get("users")
.and_then(|users| users.as_sequence())
.map_or_else(Vec::new, |users| {
users
.iter()
.filter_map(|users| Regex::new(users.get("regex")?.as_str()?).ok())
.collect::<Vec<_>>()
});
.users
.iter()
.filter_map(|users| Regex::new(users.regex.as_str()).ok())
.collect::<Vec<_>>();
let bridge_user_id = appservice
.1
.get("sender_localpart")
.and_then(|string| string.as_str())
.and_then(|string| {
UserId::parse_with_server_name(string, services().globals.server_name()).ok()
});
let bridge_user_id = UserId::parse_with_server_name(
appservice.1.sender_localpart.as_str(),
services().globals.server_name(),
)
.ok();
let in_room = bridge_user_id
.map_or(false, |id| self.is_joined(&id, room_id).unwrap_or(false))
@ -240,8 +236,6 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
.insert(appservice.0.clone(), in_room);
Ok(in_room)
} else {
Ok(false)
}
}

View file

@ -11,6 +11,7 @@ use directories::ProjectDirs;
use lru_cache::LruCache;
use rand::thread_rng;
use ruma::{
api::appservice::Registration,
events::{
push_rules::{PushRulesEvent, PushRulesEventContent},
room::message::RoomMessageEventContent,
@ -163,7 +164,7 @@ pub struct KeyValueDatabase {
//pub pusher: pusher::PushData,
pub(super) senderkey_pusher: Arc<dyn KvTree>,
pub(super) cached_registrations: Arc<RwLock<HashMap<String, serde_yaml::Value>>>,
pub(super) cached_registrations: Arc<RwLock<HashMap<String, Registration>>>,
pub(super) pdu_cache: Mutex<LruCache<OwnedEventId, Arc<PduEvent>>>,
pub(super) shorteventid_cache: Mutex<LruCache<u64, Arc<EventId>>>,
pub(super) auth_chain_cache: Mutex<LruCache<Vec<u64>, Arc<HashSet<u64>>>>,