mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-23 17:07:32 +00:00
102 lines
2.6 KiB
Rust
102 lines
2.6 KiB
Rust
use conduwuit::{Err, Result, checked};
|
|
use futures::{FutureExt, StreamExt, TryFutureExt};
|
|
|
|
impl crate::Context<'_> {
|
|
pub(super) async fn 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");
|
|
let parsed_config = serde_saphyr::from_str(&appservice_config_body);
|
|
match parsed_config {
|
|
| Err(e) => return Err!("Could not parse appservice config as YAML: {e}"),
|
|
| Ok(registration) => match self
|
|
.services
|
|
.appservice
|
|
.register_appservice(®istration, &appservice_config_body)
|
|
.await
|
|
.map(|()| registration.id)
|
|
{
|
|
| Err(e) => return Err!("Failed to register appservice: {e}"),
|
|
| Ok(id) => write!(self, "Appservice registered with ID: {id}"),
|
|
},
|
|
}
|
|
.await
|
|
}
|
|
|
|
pub(super) async fn unregister(&self, appservice_identifier: String) -> Result {
|
|
match self
|
|
.services
|
|
.appservice
|
|
.unregister_appservice(&appservice_identifier)
|
|
.await
|
|
{
|
|
| Err(e) => return Err!("Failed to unregister appservice: {e}"),
|
|
| Ok(()) => write!(self, "Appservice unregistered."),
|
|
}
|
|
.await
|
|
}
|
|
|
|
pub(super) async fn show_appservice_config(&self, appservice_identifier: String) -> Result {
|
|
match self
|
|
.services
|
|
.appservice
|
|
.get_registration(&appservice_identifier)
|
|
.await
|
|
{
|
|
| None => return Err!("Appservice does not exist."),
|
|
| Some(config) => {
|
|
let config_str = serde_saphyr::to_string(&config)?;
|
|
write!(self, "Config for {appservice_identifier}:\n\n```yaml\n{config_str}\n```")
|
|
},
|
|
}
|
|
.await
|
|
}
|
|
|
|
pub(super) async fn list_registered(&self) -> Result {
|
|
self.services
|
|
.appservice
|
|
.iter_ids()
|
|
.collect()
|
|
.map(Ok)
|
|
.and_then(|appservices: Vec<_>| {
|
|
let len = appservices.len();
|
|
let list = appservices.join(", ");
|
|
write!(self, "Appservices ({len}): {list}")
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub(super) async fn ensure_puppets_active(&self) -> Result {
|
|
self.services
|
|
.users
|
|
.stream_local_users()
|
|
.filter_map(async |user_id| {
|
|
if self.services.appservice.is_user_id(&user_id).await {
|
|
Some(user_id)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.for_each(async |user_id| {
|
|
self.services
|
|
.users
|
|
.convert_to_shadow_account(&user_id)
|
|
.await
|
|
.expect("should be able to convert to shadow");
|
|
})
|
|
.await;
|
|
|
|
write!(self, "All appservice puppets have been marked as active.").await?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|