Refactor admin appservice
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
use futures::{FutureExt, StreamExt, TryFutureExt};
|
use futures::StreamExt;
|
||||||
use tuwunel_core::{Err, Result, checked};
|
use tuwunel_core::{Err, Result, checked, err};
|
||||||
|
|
||||||
use crate::admin_command;
|
use crate::admin_command;
|
||||||
|
|
||||||
#[admin_command]
|
#[admin_command]
|
||||||
pub(super) async fn register(&self) -> Result {
|
pub(super) async fn appservice_register(&self) -> Result {
|
||||||
let body = &self.body;
|
let body = &self.body;
|
||||||
let body_len = self.body.len();
|
let body_len = self.body.len();
|
||||||
if body_len < 2
|
if body_len < 2
|
||||||
@@ -17,64 +17,57 @@ pub(super) async fn register(&self) -> Result {
|
|||||||
let range = 1..checked!(body_len - 1)?;
|
let range = 1..checked!(body_len - 1)?;
|
||||||
let appservice_config_body = body[range].join("\n");
|
let appservice_config_body = body[range].join("\n");
|
||||||
let parsed_config = serde_yaml::from_str(&appservice_config_body);
|
let parsed_config = serde_yaml::from_str(&appservice_config_body);
|
||||||
match parsed_config {
|
|
||||||
| Err(e) => return Err!("Could not parse appservice config as YAML: {e}"),
|
let registration =
|
||||||
| Ok(registration) => match self
|
parsed_config.map_err(|e| err!("Could not parse appservice config as YAML: {e}"))?;
|
||||||
.services
|
|
||||||
.appservice
|
self.services
|
||||||
.register_appservice(®istration, &appservice_config_body)
|
.appservice
|
||||||
.await
|
.register_appservice(®istration, &appservice_config_body)
|
||||||
.map(|()| registration.id)
|
.await
|
||||||
{
|
.map_err(|e| err!("Failed to register appservice: {e}"))?;
|
||||||
| Err(e) => return Err!("Failed to register appservice: {e}"),
|
|
||||||
| Ok(id) => write!(self, "Appservice registered with ID: {id}"),
|
self.write_str(&format!("Appservice registered with ID: {}", registration.id))
|
||||||
},
|
.await
|
||||||
}
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[admin_command]
|
#[admin_command]
|
||||||
pub(super) async fn unregister(&self, appservice_identifier: String) -> Result {
|
pub(super) async fn appservice_unregister(&self, appservice_identifier: String) -> Result {
|
||||||
match self
|
self.services
|
||||||
.services
|
|
||||||
.appservice
|
.appservice
|
||||||
.unregister_appservice(&appservice_identifier)
|
.unregister_appservice(&appservice_identifier)
|
||||||
.await
|
.await
|
||||||
{
|
.map_err(|e| err!("Failed to unregister appservice: {e}"))?;
|
||||||
| Err(e) => return Err!("Failed to unregister appservice: {e}"),
|
|
||||||
| Ok(()) => write!(self, "Appservice unregistered."),
|
self.write_str("Appservice unregistered.").await
|
||||||
}
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[admin_command]
|
#[admin_command]
|
||||||
pub(super) async fn show_appservice_config(&self, appservice_identifier: String) -> Result {
|
pub(super) async fn appservice_show_config(&self, appservice_identifier: String) -> Result {
|
||||||
match self
|
let config = self
|
||||||
.services
|
.services
|
||||||
.appservice
|
.appservice
|
||||||
.get_registration(&appservice_identifier)
|
.get_registration(&appservice_identifier)
|
||||||
.await
|
.await
|
||||||
{
|
.ok_or(err!("Appservice does not exist."))?;
|
||||||
| None => return Err!("Appservice does not exist."),
|
|
||||||
| Some(config) => {
|
let config_str = serde_yaml::to_string(&config)?;
|
||||||
let config_str = serde_yaml::to_string(&config)?;
|
|
||||||
write!(self, "Config for {appservice_identifier}:\n\n```yaml\n{config_str}\n```")
|
self.write_str(&format!("Config for {appservice_identifier}:\n\n```yaml\n{config_str}\n```"))
|
||||||
},
|
.await
|
||||||
}
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[admin_command]
|
#[admin_command]
|
||||||
pub(super) async fn list_registered(&self) -> Result {
|
pub(super) async fn appservice_list(&self) -> Result {
|
||||||
self.services
|
let appservices: Vec<_> = self
|
||||||
|
.services
|
||||||
.appservice
|
.appservice
|
||||||
.iter_ids()
|
.iter_ids()
|
||||||
.collect()
|
.collect()
|
||||||
.map(Ok)
|
.await;
|
||||||
.and_then(|appservices: Vec<_>| {
|
|
||||||
let len = appservices.len();
|
let len = appservices.len();
|
||||||
let list = appservices.join(", ");
|
let list = appservices.join(", ");
|
||||||
write!(self, "Appservices ({len}): {list}")
|
self.write_str(&format!("Appservices ({len}): {list}"))
|
||||||
})
|
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use tuwunel_core::Result;
|
|||||||
use crate::admin_command_dispatch;
|
use crate::admin_command_dispatch;
|
||||||
|
|
||||||
#[derive(Debug, Subcommand)]
|
#[derive(Debug, Subcommand)]
|
||||||
#[admin_command_dispatch]
|
#[admin_command_dispatch(handler_prefix = "appservice")]
|
||||||
pub(super) enum AppserviceCommand {
|
pub(super) enum AppserviceCommand {
|
||||||
/// - Register an appservice using its registration YAML
|
/// - Register an appservice using its registration YAML
|
||||||
///
|
///
|
||||||
@@ -29,12 +29,11 @@ pub(super) enum AppserviceCommand {
|
|||||||
///
|
///
|
||||||
/// You can find the ID using the `list-appservices` command.
|
/// You can find the ID using the `list-appservices` command.
|
||||||
#[clap(alias("show"))]
|
#[clap(alias("show"))]
|
||||||
ShowAppserviceConfig {
|
ShowConfig {
|
||||||
/// The appservice to show
|
/// The appservice to show
|
||||||
appservice_identifier: String,
|
appservice_identifier: String,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// - List all the currently registered appservices
|
/// - List all the currently registered appservices
|
||||||
#[clap(alias("list"))]
|
List,
|
||||||
ListRegistered,
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user