diff --git a/src/service/appservice/mod.rs b/src/service/appservice/mod.rs index 970af394..e0d69ccb 100644 --- a/src/service/appservice/mod.rs +++ b/src/service/appservice/mod.rs @@ -1,7 +1,11 @@ mod namespace_regex; mod registration_info; -use std::{collections::BTreeMap, iter::IntoIterator, sync::Arc}; +use std::{ + collections::{BTreeMap, HashSet}, + iter::IntoIterator, + sync::Arc, +}; use async_trait::async_trait; use futures::{Future, FutureExt, Stream, StreamExt, TryStreamExt}; @@ -47,6 +51,7 @@ impl crate::Service for Service { async fn worker(self: Arc) -> Result { self.init_registrations().await?; + self.check_registrations().await?; Ok(()) } @@ -88,6 +93,25 @@ impl Service { .await } + #[tracing::instrument(name = "check", skip(self))] + async fn check_registrations(&self) -> Result { + let regs = self.registration_info.read().await; + + let num_as_tokens = regs + .values() + .map(|info| &info.registration.as_token) + .collect::>() + .len(); + + if num_as_tokens < regs.len() { + return Err!( + "Conflicting Appservice registrations: each must have a unique as_token." + ); + } + + Ok(()) + } + /// Registers an appservice and returns the ID to the caller pub async fn register_appservice( &self,