Files
tuwunel/src/admin/appservice/commands.rs

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

76 lines
2.0 KiB
Rust
Raw Normal View History

2026-01-25 03:12:31 +05:00
use futures::StreamExt;
2026-02-23 11:49:16 +05:00
use ruma::api::appservice::Registration;
2026-01-25 03:12:31 +05:00
use tuwunel_core::{Err, Result, checked, err};
use crate::admin_command;
#[admin_command]
2026-01-25 03:12:31 +05:00
pub(super) async fn appservice_register(&self) -> Result {
let body = &self.body;
let body_len = self.body.len();
if body_len < 2
|| !body[0].trim().starts_with("```")
|| body.last().unwrap_or(&"").trim() != "```"
{
return Err!("Expected code block in command body. Add --help for details.");
}
let range = 1..checked!(body_len - 1)?;
let appservice_config_body = body[range].join("\n");
2026-01-25 03:12:31 +05:00
2026-02-23 11:49:16 +05:00
let registration: Registration = serde_yaml::from_str(&appservice_config_body)
.map_err(|e| err!("Could not parse appservice config as YAML: {e}"))?;
let id = registration.id.clone();
2026-01-25 03:12:31 +05:00
self.services
.appservice
2026-02-23 11:49:16 +05:00
.register_appservice(registration)
2026-01-25 03:12:31 +05:00
.await
.map_err(|e| err!("Failed to register appservice: {e}"))?;
2026-02-23 11:49:16 +05:00
self.write_str(&format!("Appservice registered with ID: {}", &id))
2026-01-25 03:12:31 +05:00
.await
}
#[admin_command]
2026-01-25 03:12:31 +05:00
pub(super) async fn appservice_unregister(&self, appservice_identifier: String) -> Result {
self.services
.appservice
.unregister_appservice(&appservice_identifier)
.await
2026-01-25 03:12:31 +05:00
.map_err(|e| err!("Failed to unregister appservice: {e}"))?;
self.write_str("Appservice unregistered.").await
}
#[admin_command]
2026-01-25 03:12:31 +05:00
pub(super) async fn appservice_show_config(&self, appservice_identifier: String) -> Result {
let config = self
.services
.appservice
.get_registration(&appservice_identifier)
.await
2026-01-25 03:12:31 +05:00
.ok_or(err!("Appservice does not exist."))?;
let config_str = serde_yaml::to_string(&config)?;
self.write_str(&format!("Config for {appservice_identifier}:\n\n```yaml\n{config_str}\n```"))
.await
}
#[admin_command]
2026-01-25 03:12:31 +05:00
pub(super) async fn appservice_list(&self) -> Result {
let appservices: Vec<_> = self
.services
.appservice
.iter_ids()
.collect()
2026-01-25 03:12:31 +05:00
.await;
let len = appservices.len();
let list = appservices.join(", ");
self.write_str(&format!("Appservices ({len}): {list}"))
.await
}