Files
continuwuity/src/service/appservice/mod.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

191 lines
4.7 KiB
Rust
Raw Normal View History

mod namespace_regex;
mod registration_info;
2022-10-08 13:04:55 +02:00
2024-05-09 15:59:08 -07:00
use std::{collections::BTreeMap, sync::Arc};
2024-03-08 10:50:52 -05:00
2024-08-08 17:18:30 +00:00
use async_trait::async_trait;
use conduwuit::{Result, err, utils::stream::TryIgnore};
use database::Map;
2024-08-08 17:18:30 +00:00
use futures::{Future, StreamExt, TryStreamExt};
use ruma::{RoomAliasId, RoomId, UserId, api::appservice::Registration};
2024-03-08 10:50:52 -05:00
use tokio::sync::RwLock;
pub use self::{namespace_regex::NamespaceRegex, registration_info::RegistrationInfo};
use crate::{Dep, sending};
2024-03-08 10:50:52 -05:00
2024-05-09 15:59:08 -07:00
pub struct Service {
2024-03-22 19:21:51 -04:00
registration_info: RwLock<BTreeMap<String, RegistrationInfo>>,
services: Services,
db: Data,
}
2024-07-18 06:37:47 +00:00
struct Services {
sending: Dep<sending::Service>,
}
struct Data {
id_appserviceregistrations: Arc<Map>,
}
2024-08-08 17:18:30 +00:00
#[async_trait]
2024-07-04 03:26:19 +00:00
impl crate::Service for Service {
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
2024-08-08 17:18:30 +00:00
Ok(Arc::new(Self {
registration_info: RwLock::new(BTreeMap::new()),
2024-08-08 17:18:30 +00:00
services: Services {
sending: args.depend::<sending::Service>("sending"),
},
db: Data {
id_appserviceregistrations: args.db["id_appserviceregistrations"].clone(),
},
2024-08-08 17:18:30 +00:00
}))
}
async fn worker(self: Arc<Self>) -> Result<()> {
2024-03-22 19:21:51 -04:00
// Inserting registrations into cache
for appservice in self.iter_db_ids().await? {
2024-08-08 17:18:30 +00:00
self.registration_info.write().await.insert(
2024-03-22 19:21:51 -04:00
appservice.0,
2024-03-25 17:05:11 -04:00
appservice
.1
.try_into()
.expect("Should be validated on registration"),
2024-03-22 19:21:51 -04:00
);
}
2024-08-08 17:18:30 +00:00
Ok(())
2024-03-22 19:21:51 -04:00
}
2024-07-04 03:26:19 +00:00
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
impl Service {
/// Registers an appservice and returns the ID to the caller
pub async fn register_appservice(
&self,
registration: &Registration,
appservice_config_body: &str,
) -> Result {
//TODO: Check for collisions between exclusive appservice namespaces
self.registration_info
2024-03-25 17:05:11 -04:00
.write()
.await
.insert(registration.id.clone(), registration.clone().try_into()?);
2024-03-08 10:50:52 -05:00
self.db
.id_appserviceregistrations
.insert(&registration.id, appservice_config_body);
Ok(())
2024-03-08 10:50:52 -05:00
}
2024-03-05 19:48:54 -05:00
/// Remove an appservice registration
///
/// # Arguments
///
/// * `service_name` - the registration ID of the appservice
pub async fn unregister_appservice(&self, appservice_id: &str) -> Result<()> {
// removes the appservice registration info
self.registration_info
2024-03-25 17:05:11 -04:00
.write()
.await
.remove(appservice_id)
.ok_or_else(|| err!("Appservice not found"))?;
2024-03-08 10:50:52 -05:00
// remove the appservice from the database
self.db.id_appserviceregistrations.del(appservice_id);
// deletes all active requests for the appservice if there are any so we stop
// sending to the URL
2024-07-18 06:37:47 +00:00
self.services
.sending
.cleanup_events(Some(appservice_id), None, None)
.await
}
2024-03-05 19:48:54 -05:00
2024-05-09 15:59:08 -07:00
pub async fn get_registration(&self, id: &str) -> Option<Registration> {
2024-03-25 17:05:11 -04:00
self.registration_info
.read()
.await
.get(id)
.cloned()
.map(|info| info.registration)
2024-03-22 19:21:51 -04:00
}
2024-05-09 15:59:08 -07:00
pub async fn iter_ids(&self) -> Vec<String> {
2024-03-25 17:05:11 -04:00
self.registration_info
.read()
.await
.keys()
.cloned()
.collect()
}
2024-03-05 19:48:54 -05:00
2024-05-09 15:59:08 -07:00
pub async fn find_from_token(&self, token: &str) -> Option<RegistrationInfo> {
2024-03-25 17:05:11 -04:00
self.read()
.await
.values()
.find(|info| info.registration.as_token == token)
.cloned()
2024-03-22 19:21:51 -04:00
}
2024-03-05 19:48:54 -05:00
/// Checks if a given user id matches any exclusive appservice regex
2024-05-09 15:59:08 -07:00
pub async fn is_exclusive_user_id(&self, user_id: &UserId) -> bool {
self.read()
.await
.values()
.any(|info| info.is_exclusive_user_match(user_id))
}
/// Checks if a given room alias matches any exclusive appservice regex
2024-05-09 15:59:08 -07:00
pub async fn is_exclusive_alias(&self, alias: &RoomAliasId) -> bool {
self.read()
.await
.values()
.any(|info| info.aliases.is_exclusive_match(alias.as_str()))
}
/// Checks if a given room id matches any exclusive appservice regex
///
/// TODO: use this?
#[allow(dead_code)]
2024-05-09 15:59:08 -07:00
pub async fn is_exclusive_room_id(&self, room_id: &RoomId) -> bool {
self.read()
.await
.values()
.any(|info| info.rooms.is_exclusive_match(room_id.as_str()))
}
pub fn read(
&self,
) -> impl Future<Output = tokio::sync::RwLockReadGuard<'_, BTreeMap<String, RegistrationInfo>>>
{
2024-03-22 19:21:51 -04:00
self.registration_info.read()
}
2024-06-29 01:19:23 +00:00
#[inline]
pub async fn all(&self) -> Result<Vec<(String, Registration)>> { self.iter_db_ids().await }
pub async fn get_db_registration(&self, id: &str) -> Result<Registration> {
self.db
.id_appserviceregistrations
.get(id)
.await
.and_then(|ref bytes| serde_yaml::from_slice(bytes).map_err(Into::into))
.map_err(|e| err!(Database("Invalid appservice {id:?} registration: {e:?}")))
}
async fn iter_db_ids(&self) -> Result<Vec<(String, Registration)>> {
self.db
.id_appserviceregistrations
.keys()
.ignore_err()
.then(|id: String| async move {
let reg = self.get_db_registration(&id).await?;
Ok((id, reg))
})
.try_collect()
.await
}
2024-06-29 01:19:23 +00:00
}