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.

51 lines
1.3 KiB
Rust
Raw Normal View History

2024-05-27 03:17:20 +00:00
use std::sync::Arc;
2024-08-08 17:18:30 +00:00
use conduit::{err, utils::stream::TryIgnore, Result};
use database::{Database, Deserialized, Map};
use futures::Stream;
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
2024-08-08 17:18:30 +00:00
.insert(id.as_bytes(), serde_yaml::to_string(&yaml).unwrap().as_bytes());
2024-05-26 21:29:19 +00:00
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
2024-08-08 17:18:30 +00:00
.remove(service_name.as_bytes());
2024-05-26 21:29:19 +00:00
Ok(())
}
2024-08-08 17:18:30 +00:00
pub async fn get_registration(&self, id: &str) -> Result<Registration> {
2024-05-26 21:29:19 +00:00
self.id_appserviceregistrations
2024-08-08 17:18:30 +00:00
.qry(id)
.await
2024-09-28 15:14:48 +00:00
.deserialized()
2024-08-08 17:18:30 +00:00
.map_err(|e| err!(Database("Invalid appservice {id:?} registration: {e:?}")))
2024-05-26 21:29:19 +00:00
}
2024-08-08 17:18:30 +00:00
pub(super) fn iter_ids(&self) -> impl Stream<Item = String> + Send + '_ {
self.id_appserviceregistrations.keys().ignore_err()
2024-05-26 21:29:19 +00:00
}
}