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

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

55 lines
1.5 KiB
Rust
Raw Normal View History

2024-05-27 03:17:20 +00:00
use std::sync::Arc;
2024-06-28 22:51:39 +00:00
use conduit::{utils, Error, Result};
use database::{Database, Map};
use ruma::api::appservice::Registration;
2024-05-27 03:17:20 +00:00
pub struct Data {
2024-06-28 22:51:39 +00:00
id_appserviceregistrations: Arc<Map>,
}
2024-05-26 21:29:19 +00:00
2024-05-27 03:17:20 +00:00
impl Data {
2024-06-28 22:51:39 +00:00
pub(super) fn new(db: &Arc<Database>) -> Self {
2024-05-27 03:17:20 +00:00
Self {
2024-06-28 22:51:39 +00:00
id_appserviceregistrations: db["id_appserviceregistrations"].clone(),
2024-05-27 03:17:20 +00:00
}
}
2024-05-26 21:29:19 +00:00
/// Registers an appservice and returns the ID to the caller
2024-06-28 23:23:59 +00:00
pub(super) fn register_appservice(&self, yaml: &Registration) -> Result<String> {
2024-05-26 21:29:19 +00:00
let id = yaml.id.as_str();
self.id_appserviceregistrations
.insert(id.as_bytes(), serde_yaml::to_string(&yaml).unwrap().as_bytes())?;
Ok(id.to_owned())
}
/// Remove an appservice registration
///
/// # Arguments
///
/// * `service_name` - the name you send to register the service previously
2024-05-27 03:17:20 +00:00
pub(super) fn unregister_appservice(&self, service_name: &str) -> Result<()> {
2024-05-26 21:29:19 +00:00
self.id_appserviceregistrations
.remove(service_name.as_bytes())?;
Ok(())
}
2024-05-27 03:17:20 +00:00
pub fn get_registration(&self, id: &str) -> Result<Option<Registration>> {
2024-05-26 21:29:19 +00:00
self.id_appserviceregistrations
.get(id.as_bytes())?
.map(|bytes| {
serde_yaml::from_slice(&bytes)
.map_err(|_| Error::bad_database("Invalid registration bytes in id_appserviceregistrations."))
})
.transpose()
}
2024-05-27 03:17:20 +00:00
pub(super) fn iter_ids<'a>(&'a self) -> Result<Box<dyn Iterator<Item = Result<String>> + 'a>> {
2024-05-26 21:29:19 +00:00
Ok(Box::new(self.id_appserviceregistrations.iter().map(|(id, _)| {
utils::string_from_bytes(&id)
.map_err(|_| Error::bad_database("Invalid id bytes in id_appserviceregistrations."))
})))
}
}